微信小程序实现锚点导航栏

这里是使用scroll-view标签控制滚动,前提是scroll-view有固定的高度,给左侧标签添加scroll-y scroll-with-animation属性,给滚动的元素绑定bindscroll方法,滚动时获取滚动位置,赋值固定id

html

<view class='box'>
    <scroll-view scroll-y scroll-with-animation style="width:25%">
        <view class='nav'>
            <view wx:for="{{navList}}" wx:key='index' class="title {{index == active ?'select':''}}"
                data-index='{{index}}' bindtap='activeNav'>{{item}}</view>
        </view>
    </scroll-view>
    <scroll-view scroll-y style="width:75%" scroll-with-animation scroll-into-view="{{selectId}}"
        bindscroll="watchScroll">
        <view class='content'>
            <view id='{{"item"+index}}' class='subtitle' wx:for="{{navList}}" wx:key='index'>{{item}}</view>
        </view>
    </scroll-view>
</view>
 

css

/* pages/home3/home3.wxss */
.box {
    display: flex;
  }
  .nav {
    height: 100%;
    width: 100%;
    background: #F5F5F5;
    box-sizing: border-box;
    flex-wrap: wrap;
  }
  
  .title {
    box-sizing: border-box;
    width: 100%;
    padding: 32rpx;
    font-size: 28rpx;
  }
  
  .select {
      background: #fff;
      border-left: 5rpx solid #eec718;
      box-sizing: border-box;
  }
  
  .content {
      padding: 0 30rpx;
      box-sizing: border-box;
      width: 100%;
      height: 100vh;
  }
  
  .subtitle {
      width: 100%;
      height: 650rpx;
      border-bottom: 10rpx #f5f5f5 solid;
  }
 

js

Page({
  data: {
    heightArr: [],
    distance: 0,
    active: 0,
    selectId: "item0",
    navList: ['全部甜品', '今日甜品系列', '毛巾卷系列', '切块系列', '限时限量系列', '提拉米苏系列']
  },

  onLoad: function (options) {
    this.selectHeight();
  },

  // 选择左侧标签锚点定位
  activeNav(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      active: index,
      selectId: "item" + index
    })
  },

  //计算右侧每个锚点的高度
  selectHeight() {
    var list = []
    var height = 0;
    const query = wx.createSelectorQuery();
    query.selectAll('.subtitle').boundingClientRect()
    query.exec((res) => {
      res[0].forEach((item) => {
        height += item.height;
        list.push(height)
      })
      this.data.heightArr = list
    })
  },

  //监听scroll-view的滚动事件
  watchScroll(e) {
    let scrollTop = e.detail.scrollTop; //获取距离顶部的距离
    let active = this.data.active;
    if (scrollTop >= this.data.distance) {
      if (active + 1 < this.data.heightArr.length && scrollTop >= this.data.heightArr[active]) {
        this.setData({
          active: active + 1
        })
      }
    } else {
      if (active - 1 >= 0 && scrollTop < this.data.heightArr[active - 1]) {
        this.setData({
          active: active - 1
        })
      }
    }
    this.data.distance = scrollTop;
  }
})
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于微信小程序触底后进行锚点跳转,可以通过使用小程序的scroll-view组件和wxs实现。具体步骤如下: 1. 在scroll-view组件中设置scroll-top属性和id属性,id属性为锚点的标记。 2. 在wxs中定义一个函数,用来监听scroll-view组件的滚动事件,当滚动到底部时,获取到锚点的位置并进行跳转。 以下是一个示例代码: ``` <!--wxml文件--> <scroll-view scroll-y="true" scroll-top="{{scrollTop}}"> <view id="anchor1"></view> <!--省略中间的内容--> <view id="anchor2"></view> </scroll-view> <!--wxss文件--> /*设置scroll-view组件的高度*/ scroll-view { height: 100%; } <!--wxs文件--> var anchorTop = 0; module.exports = { //监听scroll-view组件的滚动事件 handleScroll: function (e) { //获取到scroll-view组件的滚动距离 var scrollTop = e.detail.scrollTop; //获取到锚点的位置 var query = wx.createSelectorQuery(); query.select('#anchor1').boundingClientRect(); query.select('#anchor2').boundingClientRect(); query.exec(function (res) { anchorTop = res[1].top;//获取到锚点的位置 //当滚动到底部时,进行锚点跳转 if (scrollTop + 500 >= anchorTop) { wx.pageScrollTo({ scrollTop: anchorTop, duration: 300 }) } }) } } ``` 在以上示例代码中,wxs文件中的handleScroll函数用来监听scroll-view组件的滚动事件,并且获取到锚点的位置。当滚动到底部时,使用wx.pageScrollTo函数进行锚点跳转,将scrollTop设置为锚点的位置,duration为滚动的时间。 需要注意的是,以上示例代码中的锚点位置获取方式仅适用于锚点距离scroll-view组件顶部的距离小于500px的情况,如果锚点距离顶部的距离较大,需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值