【小程序】回到顶部

橙三吉。 提交于 2020-10-25 02:00:35

回到顶部

  1. scroll-view形式下回到顶部
//wxml
<scroll-view scroll-y scroll-top='{{topNum}}' bindscroll="scrolltoupper">
</scroll-view>
<view class="gotop" hidden='{{!cangotop}}' catchtap="goTop">
  <view>回到顶部</view>
</view>
// wxss
.toTop {
  width: 80rpx;
  height: 80rpx;
  border: 1px solid rgb(197, 197, 197);
  position: fixed;
  top: 84%;
  right: 2%;
  text-align: center;
  line-height: 80rpx;
  background: #fff;
  border-radius: 50%;
}
.my-text{
  font-size: 50rpx;
}
// js
 data:{
    topNum: 0
  }
 
  // 获取滚动条当前位置
  scrolltoupper:function(e){
    console.log(e)
    if (e.detail.scrollTop > 100 && !this.data.cangotop) {
      //避免重复设置setData
      this.setData({
        cangotop: true
      });
    } 
    if(e.detail.scrollTop <= 100 && this.data.cangotop){
       this.setData({
	   cangotop: false
       });
    }
  },
  //回到顶部
  goTop: function (e) {  // 一键回到顶部
    this.setData({
      topNum:0
    });
  },
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!