微信小程序tab导航顶部吸附效果

1,542 阅读1分钟

具体思路如下,我们首先要拿到需要吸附元素的据顶部距离,然后再去判断,如果超过了,我们通过判断添加样式即可,如在有不理解的,可以私信小编。

wxml

<view style="height:100%;position:fixed;width:100%;">
  <scroll-view scroll-y="false"  bindscroll="scroll" style="height:100%;">
    <view class="page-bottom-content">
      <text>{{text}}</text>
    </view>
    <view class="page-banner">
      banner
    </view>
    <view class="page-group {{scrollTop > 360 ? 'page-group-position' : ''}}">
      <view class="page-nav-list"><text>首页</text></view>
      <view class="page-nav-list"><text>活动</text></view>
      <view class="page-nav-list"><text>菜单</text></view>
      <view class="page-nav-list"><text>我的</text></view>
    </view>
    <view class="goods-list">
      goods-list
    </view>
  </scroll-view>
</view>

js

Page({
  data: {
    scrollTop: null
  },
  //滚动条监听
  scroll: function (e) {
    this.setData({ scrollTop: e.detail.scrollTop })
  },
})

wxss

.page-banner{height: 500rpx;background-color: greenyellow;padding: 20rpx;color:#fff;}
.page-group{
  display: table;
  background-color: blueviolet;
  width: 100%;
  table-layout: fixed;
  position: relative;
  top: 0;
  left: 0;
}
.page-group-position{
  position: fixed;
}
.page-nav-list{
  padding:30rpx 0 ;
  display: table-cell;
  text-align: center;
  color: #fff;
}
.goods-list{
  height: 2000rpx;
  background-color: green;
  padding: 20rpx;
  color:#fff;
}

喜欢下方小程序,需要源码的,添加微信私聊小编.