小程序scroll-view滚动时高亮对应区域的吸顶tab项

这篇博客详细介绍了如何在微信小程序中实现scroll-view组件的滚动时,区域元素出现时对应tab项自动高亮的功能。通过使用Intersection Observer观察器监听滚动位置,并结合wx.createSelectorQuery获取元素位置信息,动态更新activeView状态来实现导航栏的高亮效果。同时,展示了具体的wxml和scss代码示例。
摘要由CSDN通过智能技术生成

记一下scroll-view的使用

滚动时,区域元素出现在屏幕上,对应的tab项目高亮

代码:

wxml:

<view class="sticky-menu" hidden="{{appear}}">
    <view class="item-box">
        <text class="item {{activeView=='chapter'?'active':''}}" bindtap="jumpTo" data-optg="chapter">章节</text>
        <text class="item {{activeView=='teacher'?'active':''}}" bindtap="jumpTo" data-optg="teacher">教师</text>
        <text class="item {{activeView=='school'?'active':''}}" bindtap="jumpTo" data-optg="school">学校</text>
        <text class="item {{activeView=='courses'?'active':''}}" bindtap="jumpTo" data-optg="courses">推荐</text>
    </view>
</view>

<scroll-view class="jump-list {{!appear?'mt':''}}" style="{{loaded?'height: '+scrollViewHeight*2+'rpx':''}}"
    scroll-into-view="{{toView}}" 
    scroll-y="true" 
    bindscroll="scrollTo"
    scroll-with-animation="true">

    <view class=" {{loaded?'':'lock'}}">
    ...
    </view>
    
</scroll-view>

js:

data: {
  toView: 'chapter',
  activeView: 'chapter',
  loaded: 0
},
onLoad: function(options) {
  this._observer = wx.createIntersectionObserver(this);
  this._observer.relativeTo('.jump-list').observe('.course-info', res => {
    this.setData({
      appear: res.intersectionRatio > 0
    });
  });
  setTimeout(() => {
    new Promise(resolve => {
      let query = wx.createSelectorQuery();
      query.select('#chapter').boundingClientRect();
      query.select('#teacher').boundingClientRect();
      query.select('#school').boundingClientRect();
      query.select('#courses').boundingClientRect();
      query.exec(function(res) {
        resolve(res);
      });
    }).then(res => {
      page.setData({
        chapterBoxTop: res[0].top,
        teacherBoxTop: res[1].top,
        schoolBoxTop: res[2].top,
        coursesBoxTop: res[3].top,
        loaded: 1
      });
      Toast.clear();
    });
  }, 600);
},
scrollTo(e) {
  let page = this,
    scrollTop = e.detail.scrollTop;

  if (
    scrollTop >= page.data.chapterBoxTop &&
    scrollTop < page.data.teacherBoxTop
  ) {
    page.setData({
      activeView: 'chapter'
    });
  } else if (scrollTop < page.data.schoolBoxTop) {
    page.setData({
      activeView: 'teacher'
    });
  } else if (scrollTop < page.data.coursesBoxTop) {
    page.setData({
      activeView: 'school'
    });
  } else {
    page.setData({
      activeView: 'courses'
    });
  }
},

scss:

.sticky-menu {
  position: fixed;
  width: 100%;
  z-index: 9;
  top: 0;
  background: #fff;
  font-size: 28rpx;
  padding: 16rpx 0;
  .item-box {
    width: 50%;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    .item {
      color: #757575;
      &.active {
        color: #2dcb6f;
        position: relative;
        &:after {
          content: '';
          display: inline-block;
          width: 32rpx;
          height: 8rpx;
          position: absolute;
          bottom: -6rpx;
          border-radius: 4rpx;
          left: 50%;
          margin-left: -16rpx;
          background: #2dcb6f;
        }
      }
    }
  }
}

.jump-list {
  height: 100%;
  &.mt {
    margin-top: 50rpx;
  }
  .lock {
    height: 100vh !important;
    overflow: hidden;
  }
}

演示效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薛定喵君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值