微信小程序锚点跳转(仿京东小程序tab效果)

摘要:
1、锚点按钮data-*的值对应要跳转的锚点的id值2、点击按钮时,将toView的值设置为当前点击的按钮的data-*的值3、给需要跳转的内容外加上scroll-into-view的值为需要跳转到的位置的id值scroll-y:是否为垂直滚动scroll-with-animation:是否加滚动动画效果tips:1、page设置高度为100%;2、必须设置scroll-view的高度以下为点击跳转锚点的完整代码˂!

1、锚点按钮

微信小程序锚点跳转(仿京东小程序tab效果)第1张

data-* 的值对应要跳转的锚点的id值

2、点击按钮时,将toView的值设置为当前点击的按钮的data-*的值一定要用setData方法,不能直接用this.toView=... ,不然页面的值无法改变

微信小程序锚点跳转(仿京东小程序tab效果)第2张

3、给需要跳转的内容外加上<scroll-view ></scroll-view>

微信小程序锚点跳转(仿京东小程序tab效果)第3张

scroll-into-view的值为需要跳转到的位置的id值

scroll-y:是否为垂直滚动

scroll-with-animation:是否加滚动动画效果

tips:

1、page设置高度为100%;

2、必须设置scroll-view的高度(我设置的为设备的高)

以下为点击跳转锚点的完整代码

<!--pages/test/test.wxml-->
<view class="menu">
  <view class='item {{nowstatus == "productBox" ? "on" : ""}}' bindtap="toViewClick"data-hash="productBox">商品</view>
  <view class='item {{nowstatus == "commentBox" ? "on" : ""}}' bindtap="toViewClick"data-hash="commentBox">评论</view>
  <view class='item {{nowstatus == "infoBox" ? "on" : ""}}' bindtap="toViewClick"data-hash="infoBox">详情</view>
</view>
<scroll-view class="box"style="height:{{winHeight}}"scroll-into-view="{{toView}}"scroll-y="true"scroll-with-animation="true">
  <view id="productBox">商品图</view>
  <view id="commentBox">评论部分</view>
  <view id="infoBox">详情部分</view>
</scroll-view>
//pages/test/test.js
Page({
  data: {
    winHeight:'100%',
    toView:'productBox',
    nowstatus:'productBox'},
  onLoad: function(options) {
    let that=this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          winHeight: res.windowHeight - (res.windowWidth * 90 / 750) + 'px'})
      },
    })
  },
  toViewClick:function(e){
    this.setData({
      toView: e.target.dataset.hash,
      nowstatus: e.target.dataset.hash
    })
  }
})
/*pages/test/test.wxss */page{height:100%;
}.box{height:100%;margin-top:60rpx;}.box view{height:1500rpx;text-align:center;
}.menu{display:flex;background:#fff;padding:40rpx 0 10rpx 20%;
}.menu .item{width:20%;line-height:80rpx;color:#333;text-align:center;
}.menu .item.on{color:red;border-bottom:2px solid red;
}
{
  "navigationStyle": "custom"
}

微信小程序锚点跳转(仿京东小程序tab效果)第4张

----------------------------------------- 滑动时相应的tab高亮显示 --------------------------------------------------

1、获取每个部分距顶部的高度

  onLoad: function(options) {
    let that=this;
    let query =wx.createSelectorQuery();
    query.select('#commentBox').boundingClientRect(res => { //获取评论距离页面顶部高度
that.setData({
        commentBoxTop: res.top
      })
    }).exec()
    query.select('#infoBox').boundingClientRect(res => { //获取详情部分距离页面顶部高度
that.setData({
        infoBoxTop: res.top
      })
    }).exec()
  }

2、监听当前滑动

微信小程序锚点跳转(仿京东小程序tab效果)第5张

3、对比当前滑动的距离和每个部分距顶部的高度

  onPageScroll: function(e) {
    var that=this;
    console.log(that.data.infoBoxTop)
    console.log(e.detail.scrollTop)
    if (e.detail.scrollTop <= that.data.commentBoxTop - 80){
      that.setData({
        nowstatus: 'productBox'})
    }
    if (e.detail.scrollTop > that.data.commentBoxTop -80){
      that.setData({
        nowstatus:'commentBox'})
    }
    if (e.detail.scrollTop > that.data.infoBoxTop - 80){
      console.log("true")
      that.setData({
        nowstatus: 'infoBox'})
    }
  }

免责声明:文章转载自《微信小程序锚点跳转(仿京东小程序tab效果)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇QueryWrapper的关键字查询匹配多个字段移动通信最先进的音频编解码器EVS及用好要做的工作下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

微信小程序知识点总结--组件

一、部分常用组件 1、scroll-view可滚动视图区域:   <scroll-view>标签必须设置scroll-x/scroll-y属性,否则不能实现滚动效果 css设置:<scroll-view>标签可以设置white-space:nowrap,子元素设置display:inline-block(如子元素中有文字需要换行,则...

微信小程序设置全局请求URL 封装wx.request请求

app.js: App({ //设置全局请求URL globalData:{ URL: 'https://www.oyhdo.com', }, /** * 封装wx.request请求 * method: 请求方式 * url: 请求地址 * data: 要传递的参数 * callback: 请求...

微信小程序通信录

第一步:phone.wxml中 <view bindlongtap="clickPhone">{{phoneNum}}</view> 第二步:phone.json { "navigationBarTitleText": "联系人" } 第三步:phone.wxss view{ color:blue; padding: 1...

微信小程序腾讯云配置Tomcat https端口

在个人开发微信小程序时,发布之前要配置微信小程序的域名https及域名的SSL证书的申请及安装 我用的是腾讯云,SSL证书申请好之后,点击下载,解压文件夹,会有如图,根据你要配置的服务器是哪种,我是tomcat配置,这时候腾讯云官方有文档,教你4种不同该怎么去云服务器配置,一步步跟着走就行。一般配置完成后会发现,还是运行不了https的网址,我个人用配置T...

微信小程序TodoList

今天刚学了一些微信小程序的皮毛  ,先做一个todolist的小dome试试手~ 先做简单的添加和删除功能  初步了解一下小程序开发 首先是效果图  : 然后是wxml :  然后是js代码 : 注意:   通过wv-index='{{index}}'传递的值在js文件中  通过ES6结构赋值的方式来获取的   添加通过 unshift 的方法  在...

微信小程序自定义tabbar

这个自定义组件需要用到 Vant Weapp 组件库 详情看 https://youzan.github.io/vant-weapp/#/intro 组件 tabbar.json页面 tabbar.wxml页面(这个页面不懂的可以看vant组件库的文档) tabbar.js页面 tabbar.wxss页面 .picker{ display:fle...