当页面滑动到某一个位置时,需要固定导航栏或者某个元素在顶部位置,向上滑动又会回归原位
onPageScroll: function (e) {
let that = this;
that.setData({
scrollTop:e.scrollTop
})
}
然后再模块上加 class="{{scrollTop>移动距离? ‘大于的样式’ : ‘小于的样式’}}"
当小程序页面刷新时,position:fixed会失效,可以用position:sticky
position:sticky基于用户的滚动位置来定位
粘性定位的元素是依赖于用户的滚动,平常是 position:relative;当页面滚动超出目标区域时是 position:fixed;
top, right, bottom 或 left 四个阈值其中之一,才可使粘性定位生效
例:
div.sticky {
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
background-color: green;
border: 2px solid #4CAF50;
}