小程序回到顶部有两种方式,分别是:
(学习推荐:编程视频)
一、使用view形式的回到顶部
html:
<image src='../../img/button-top.png' class='gotop' hidden='{{!floorstatus}}' bindtap="gotop"></image>
css:
/* 返回顶部 */.gotop{ height: 80rpx; width: 80rpx; position: fixed; bottom: 50rpx; background: rgba(0,0,0,.3); right: 30rpx; border-radius: 50%;}
js:
// 获取滚动条当前位置 onpagescroll: function (e) { console.log(e) if (e.scrolltop > 100) { this.setdata({ floorstatus: true }); } else { this.setdata({ floorstatus: false }); } }, //回到顶部 gotop: function (e) { // 一键回到顶部 if (wx.pagescrollto) { wx.pagescrollto({ scrolltop: 0 }) } else { wx.showmodal({ title: '提示', content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。' }) } },
二.使用scroll-view形式的回到顶部
<scroll-view scroll-y scroll-top='{{topnum}}' bindscroll="scrolltoupper"><image src='../../img/button-top.png' class='gotop' hidden='{{!floorstatus}}' bindtap="gotop"></image>
css:
/* 返回顶部 */.gotop{ height: 80rpx; width: 80rpx; position: fixed; bottom: 50rpx; background: rgba(0,0,0,.3); right: 30rpx; border-radius: 50%;}
js:
data:{ topnum: 0 } // 获取滚动条当前位置 scrolltoupper:function(e){ console.log(e) let t = e.detail.scrolltop; if (t > 100 && !this.data.floorstatus) { // 避免重复setdata this.setdata({ floorstatus: true }); } if(t <= 100 && this.data.floorstatus){ this.setdata({ floorstatus: false }); } }, //回到顶部 gotop: function (e) { // 一键回到顶部 this.setdata({ topnum: this.data.topnum = 0 }); },
相关推荐:小程序开发教程
以上就是小程序回到顶部有哪两种方式的详细内容。