0
Follow
0
View

A problem with snap for IScroll.js

WJKENT 注册会员
2023-02-27 19:41

This answer quotes ChatGPT

On the first question, iscrol.js automatically adjusts the scroll position when using snap to ensure that each scroll element is fully visible. Therefore, when you scroll in portrait, you may cause the current element to scroll automatically to the original position, that is, the first page.

To solve this problem, you can try to add probeType: 3 in the configuration, enable IScroll.js scroll event listening mode, in order to obtain the current scroll position when scrolling, so as to judge and adjust it. The code is as follows:

let myiscroll = new IScroll('.box', {
  startX: -200,
  scrollX: true,
  snap: '.Contentitem',
  probeType: 3, // 启用滚动事件监听模式
  onScroll: function() {
    // 获取当前滚动位置
    let currentX = this.x;
    if (currentX >= -100 && currentX < 100) {
      // 当前滚动到第一页
      this.scrollTo(-200, 0, 0);
    }
  }
});


In the above code, probeType: 3 has the scroll event listening mode enabled, and the onScroll callback function is triggered during scrolling to handle the adjustment of the current scroll position. In this example, when we scroll to the first page, we adjust its position to the set location of the first page on the second page(that is, startX: -200).

Regarding the second question, IScroll.js overrides the manually set transform property when using snap because it needs to control the scroll position itself to ensure that each scroll element is fully visible. If you need to manually control the scroll position, it is recommended not to use snap, but to use the scrollTo method to implement the scroll, for example:


// 向左滚动 200 像素
myiscroll.scrollTo(-200, 0, 500); // 第三个参数为动画时间,单位为毫秒

The scrollTo method can precisely control the scroll position and animation time, and is not restricted by snap.

dqxcz1 注册会员
2023-02-27 19:41

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > the problem may be caused by iScroll enabled by default for the result of the interaction pattern of the scroll bar. When the user drags vertically across the page, iScroll will mistake the user for trying to scroll and automatically trigger a return to the top action.

To address this issue, you can try adding a disableMouse option to iScroll's configuration to disable mouse interaction, leaving only touch interaction in place to avoid unnecessary back to top action. The specific practices are as follows:

let myiscroll = new IScroll('.box', {
  startX: -200,
  scrollX: true,
  snap: '.Contentitem',
  disableMouse: true // 禁用鼠标交互模式
})


Alternatively, you can try using iScroll's preventDefaultException option to specify an element or selector to prevent vertical scrolling on those elements from being intercepted by iScroll. The specific practices are as follows:

let myiscroll = new IScroll('.box', {
  startX: -200,
  scrollX: true,
  snap: '.Contentitem',
  preventDefaultException: { tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/ } // 指定文本框、按钮等元素不被截获
})


Hopefully these methods will help you solve the problem.

dengzhengsha 注册会员
2023-02-27 19:41
< div class = "md_content_show e397 data - v - 3967" = "" >

thank you for your answer, but the problem still not solve the

About the Author

Question Info

Publish Time
2023-02-27 19:41
Update Time
2023-02-27 19:41