wxjs
const app = getApp(); Page({ data:{ // top標簽顯示(默認不顯示) backTopValue:false }, // 監聽滾動條坐標 onPageScroll: function (e) { //console.log(e) var that = this var scrollTop = e.scrollTop var backTopValue = scrollTop > 500 ? true : false that.setData({ backTopValue: backTopValue }) }, // 滾動到頂部 backTop:function(){ // 控制滾動 wx.pageScrollTo({ scrollTop: 0 }) }, })
wxss
/*浮窗返回頂部*/ .backTop{width: 60rpx; height:60rpx;background: #fff;position: fixed; right: 20rpx ; bottom: 130rpx; border-radius: 30rpx;box-shadow: 0px 0px 3px #000; line-height: 60rpx; text-align: center} .backTop text{font-size: 45rpx; }
wxml
<!--浮窗 Top--> <view class="backTop" bindtap='backTop' wx:if="{{backTopValue ==true}}"><text class='iconfont icon-top li-ico'></text></view>
效果:滾動到一定距離后顯示Top樣式