ITeye博客

微信小程序--搜索框滚动到顶部时悬浮

hereson · 2018-12-06 14:44 · Javascript · 微信小程序 · Web前端 · Markdown

<scroll-view style="width:100%;height: 100%;" scroll-y="true" bindscroll="scrollTopFun">
    <view class="{{top>130 ? 'topnav' : ''}}">
    <--这里写大于130,表示距离顶部130rpx时固定,可根据需要修改-->
        ...
    </view>
</scroll-view>

 wxss

.topnav{
    position: fixed;
    top: 85rpx;
    z-index:99;
    background: #fff;
    width: 100%;
}

 js

data = {
  top:0
}
//控制回到顶部按钮的显示与消失
scrollTopFun(e){
    let that = this;
    that.top = e.detail.scrollTop;
    that.$apply();
}

 

或者

onPageScroll:function(e){
		let that = this;
		that.setData({top: e.scrollTop });
},

 当页面滚动时导航条到达搜索栏下方时固定,向上滚动到导航条位置时又恢复原样。 

 参考:askjava

评论(1)

  1. #1 楼 · GGGGeek · 2019-05-18 18:39

    第一个函数滚动监听不起作用,onPageScroll可以

评论功能维护中,当前仅展示历史评论。