1.获取向上滚动了多少
onPageScroll(t) {
this.setData({
scrollTop: t.scrollTop
})
}
2.获取某id距离上方多少
onReady(){
var that = this
wx.createSelectorQuery().select('#nav_id').boundingClientRect((rect)=>{
that.setData({
myTop:rect.top || 280
})
}).exec()
},
3.进行比较
<view class="nav {{scrollTop>myTop ? 'topnav' : ''}}" id="nav_id">
4.样式
.nav {
position: relative;
margin-top: 10rpx;
font-size: 32rpx;
color: #3B3849;
z-index:99
}
/*固定页面中间部分*/
.nav.topnav{
position: fixed;
top: 0;
width: 100%;
left: 0;
margin-top: 0;
background: #fff;
z-index:199
}
网友评论