微信小程序回到顶部的页面标识相关处理

(一)上滑隐藏,下滑显示
1.检测开始滑动的函数写在哪里?答案是写在会发生滚动的部分,eg:列表的外盒

  // 检测滑动开始
  topMoveStart(e){
    console.log(e.touches[0].pageY)
    this.setData({
      startMove: e.touches[0].pageY
    })
    console.log(this.data.startMove)
  }

滑动的位置可以通过e.touches[0]进行取到,当最终的位置大于初始值,则下滑,则显示,如果滑动的最终位置小于初始值,则是上滑,则隐藏

  // 监视滑动过程
  topIsMoving(e){
    console.log(e.touches[0].pageY)
    let startMove = this.data.startMove, startEnd = e.touches[0].pageY;
    if (startEnd > startMove){
      this.setData({
        showTop:true
      })
    }else{
      this.setData({
        showTop: false
      })    
    }
  }

页面的简单布局

<view class='contein' bindtouchmove='topIsMoving' bindtouchstart='topMoveStart'>
hahhah
</view>
<view wx:if="{{!showTop}}" class='top-mic'></view>

页面样式

.contein{
  height: 8000rpx;
  width: 100%;
}
.top-mic{
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  background: #e4370e;
  position: fixed;
  top: 500rpx;
  left: 20rpx;
}

初始data中的数据

  data: {
    startMove: '',
    showTop: false
  },

(二)实现点击回到顶部

页面

<view wx:if="{{!showTop}}" bindtap='toTop' class='top-mic'></view>

js,回到顶部以后,隐藏回到顶部标识

  toTop(){
    const that = this
    wx.pageScrollTo({
      scrollTop: 0,
    })
    setTimeout(()=>{
      that.setData({
        showTop:true
      })
    },100)
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值