微信小程序scroll-view滑动的时候滑动到指定位置

效果实现

1.滑动的时候调动滑动事件bindscroll="scroll"

<view style="position: fixed;z-index: 100;" wx:if="{{content.isShow}}">
    我要在头部定义的标题栏
</view>

<scroll-view  scroll-y="true" bindscroll="scroll"  scroll-with-animation scroll-top="{{scrolltop}}">
  <view>这里面是你的一大堆内容</view>
</scroll-view>

scroll-with-animation 过渡动画

scroll-top="{{scrolltop}}" 距离顶部多少高度

2.在调用scroll里面的事件函数

 data: {
   content: {
      isShow: false
    },
    scrolltop:0,
 }

这里使用的是获取手机屏幕大小的宽度res.screenWidth来自适应头部悬浮的位置的显示与隐藏

opacity:( 1 - e.detail.scrollTop / 100 / 3).toFixed(2) 目的是为了页面上划或者下划的时候实现渐显渐隐效果,但是页面调用的时候卡动,所以就直接去除掉了这个效果.

scroll: function(e:any) {
      const res = wx.getSystemInfoSync()
      if(res.screenWidth <= 330){
        if (e.detail.scrollTop >= 300) {
          if (!this.data.content.isShow) {
            this.setData({
              'content.isShow': true
            })
          }
        } else {
          if(e.detail.deltaY < 0){
            // console.log("向下滑动")
            // opacity:( 1 - e.detail.scrollTop / 100 / 3).toFixed(2)
            this.setData({scrolltop:300})
          }else{
            // console.log("向上滑动")
            this.setData({scrolltop:0})
          }
          if (this.data.content.isShow) {
            this.setData({
              'content.isShow': false
            })
          }
        }
      }else if(res.screenWidth > 330 && res.screenWidth <= 365){
        if (e.detail.scrollTop >= 330) {
          if (!this.data.content.isShow) {
            this.setData({
              'content.isShow': true
            })
          }
        } else {
          if(e.detail.deltaY < 0){
            this.setData({scrolltop:330})
          }else{
            this.setData({scrolltop:0})
          }
          if (this.data.content.isShow) {
            this.setData({
              'content.isShow': false
            })
          }
        }
      }else if(res.screenWidth > 365 && res.screenWidth <= 420){
        if (e.detail.scrollTop >= 377) {
          if (!this.data.content.isShow) {
            this.setData({
              'content.isShow': true
            })
          }
        } else {
          if(e.detail.deltaY < 0){
            this.setData({scrolltop:377})
          }else{
            this.setData({scrolltop:0})
          }
          if (this.data.content.isShow) {
            this.setData({
              'content.isShow': false
            })
          }
        }
      }else{
        if (e.detail.scrollTop >= 395) {
          if (!this.data.content.isShow) {
            this.setData({
              'content.isShow': true
            })
          }
        } else {
          if(e.detail.deltaY < 0){
            this.setData({scrolltop:395})
          }else{
            this.setData({scrolltop:0})
          }
          if (this.data.content.isShow) {
            this.setData({
              'content.isShow': false
            })
          }
        }
      }
    },

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值