一、wxml頁面:catchtap阻止冒泡事件。
- <view class="gotop" hidden='{{!cangotop}}'catchtap="goTop">
- <view>回到頂部</view>
- </view>
二、在scroll-view形式下回到頂部
- <scroll-view scroll-y scroll-top='{{topNum}}' bindscroll="scrolltoupper">
- <view class="gotop" hidden='{{!cangotop}}'catchtap="goTop">
- <view>回到頂部</view>
- </view>
- </scroll-view>
JS頁面代碼段:
- data:{
- topNum: 0
- }
- // 獲取滾動條當前位置
- scrolltoupper:function(e){
- console.log(e)
- if (e.detail.scrollTop > 100) {
- this.setData({
- cangotop: true
- });
- } else {
- this.setData({
- cangotop: false
- });
- }
- },
- //回到頂部
- goTop: function (e) { // 一鍵回到頂部
- this.setData({
- topNum:0
- });
- },