微信小程序swiper控件卡死的解决方法
发布于 4 年前 作者 laimin 4201 次浏览 来自 分享

微信小程序swiper控件,在使用过程中会偶尔出现卡死现象(不能再左右滑动),跟踪一下,归结原因可能是swiper组件内部问题,当无法响应用户快速翻动动作时,当前页变量current无法变更为正确页码索引,而是被置为0,所以,解决这个问题的思路如下:

swiperchange: function (event) {
    if (event.detail.source == "touch") {      
      //防止swiper控件卡死
      if (this.data.current == 0 && this.data.preIndex>1 ) {//卡死时,重置current为正确索引
        this.setData({ current: this.data.preIndex });
      }
      else {//正常轮转时,记录正确页码索引
        this.setData({ preIndex: this.data.current });
      }
    }
  }
1 回复

很精简的代码却解决了一个很实在的问题~

而且思路很好。必须点赞。

回到顶部