Touch move getting stuck Ignored attempt to cancel a touchmove

后端 未结 5 1269
耶瑟儿~
耶瑟儿~ 2020-12-02 19:47

I\'m messing around with touch events on a touch slider and I keep getting the following error:

Ignored attempt to cancel a touchmove event with canc

相关标签:
5条回答
  • 2020-12-02 20:13

    Calling preventDefault on touchmove while you're actively scrolling is not working in Chrome. To prevent performance issues, you cannot interrupt a scroll.

    Try to call preventDefault() from touchstart and everything should be ok.

    0 讨论(0)
  • 2020-12-02 20:19

    I know this is an old post but I had a lot of issues trying to solve this and I finally did so I wanted to share.

    My issue was that I was adding an event listener within the ontouchstart and removing it in the ontouchend functions - something like this

    function onTouchStart() {
      window.addEventListener("touchmove", handleTouchMove, {
        passive: false
      });
    }
    
    function onTouchEnd() {
      window.removeEventListener("touchmove", handleTouchMove, {
        passive: true
      });
    }
    
    function handleTouchMove(e) {
      e.preventDefault();
    }
    

    For some reason adding it removing it like this was causing this issue of the event randomly not being cancelable. So to solve this I kept the listener active and toggled a boolean on whether or not it should prevent the event - something like this:

    let stopScrolling = false;
    
    window.addEventListener("touchmove", handleTouchMove, {
      passive: false
    });
    
    function handleTouchMove(e) {
      if (!stopScrolling) {
        return;
      }
      e.preventDefault();
    }
    
    function onTouchStart() {
      stopScrolling = true;
    }
    
    function onTouchEnd() {
      stopScrolling = false;
    }
    

    I was actually using React so my solution involved setting state, but I've simplified it for a more generic solution. Hopefully this helps someone!

    0 讨论(0)
  • 2020-12-02 20:24

    I had this problem and all I had to do is return true from touchend and the warning went away.

    0 讨论(0)
  • 2020-12-02 20:28

    The event must be cancelable. Adding an if statement solves this issue.

    if (e.cancelable) {
       e.preventDefault();
    }
    

    In your code you should put it here:

    if (this.isSwipe(swipeThreshold) && e.cancelable) {
       e.preventDefault();
       e.stopPropagation();
       swiping = true;
    }
    
    0 讨论(0)
  • 2020-12-02 20:38

    Please remove e.preventDefault(), because event.cancelable of touchmove is false. So you can't call this method.

    0 讨论(0)
提交回复
热议问题