触摸移动卡住忽略尝试取消触摸移动

时间:2014-10-21 03:09:20

标签: javascript jquery event-handling slider touch

我正在搞乱触摸滑块上的触摸事件,我不断收到以下错误:

  

忽略尝试取消touchlable = false的touchmove事件,   例如,因为滚动正在进行而不能   中断。

我不确定是什么导致了这个问题,我不熟悉使用触摸事件,似乎无法解决这个问题。

以下是处理触摸事件的代码:

Slider.prototype.isSwipe = function(threshold) {
    return Math.abs(deltaX) > Math.max(threshold, Math.abs(deltaY));
}


Slider.prototype.touchStart = function(e) {

    if (this._isSliding) return false;

      touchMoving = true;
      deltaX = deltaY = 0;

    if (e.originalEvent.touches.length === 1) {

        startX = e.originalEvent.touches[0].pageX;
        startY = e.originalEvent.touches[0].pageY;

        this._$slider.on('touchmove touchcancel', this.touchMove.bind(this)).one('touchend', this.touchEnd.bind(this));

        isFlick = true;

        window.setTimeout(function() {
            isFlick = false;
        }, flickTimeout);
    }
}


Slider.prototype.touchMove = function(e) {

    deltaX = startX - e.originalEvent.touches[0].pageX;
    deltaY = startY - e.originalEvent.touches[0].pageY;

    if(this.isSwipe(swipeThreshold)) {
        e.preventDefault();
        e.stopPropagation();
        swiping = true;
    }
    if(swiping) {
        this.slide(deltaX / this._sliderWidth, true)
    }
}


Slider.prototype.touchEnd = function(e) {

    var threshold = isFlick ? swipeThreshold : this._sliderWidth / 2;

    if (this.isSwipe(threshold)) {
        deltaX < 0 ? this.prev() : this.next();
    }
    else {
        this.slide(0, !deltaX);
    }

    swiping = false;

    this._$slider.off('touchmove', this.touchMove).one(transitionend, $.proxy(function() {
        this.slide(0, true);
        touchMoving = false;
    }, this));
}

您可以找到实际的滑块 here at this pen

如果你快速滑动它会抛出错误,有时卡在滑动中间。仍然无法解决为什么它不起作用。任何帮助/见解将不胜感激。不确定我做错了什么。

5 个答案:

答案 0 :(得分:10)

我遇到了这个问题,所有我必须做的就是来自touchend的return true并且警告消失了。

答案 1 :(得分:9)

事件必须为cancelable。添加if语句即可解决此问题。

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

在您的代码中,您应该将其放在此处:

if (this.isSwipe(swipeThreshold) && e.cancelable) {
   e.preventDefault();
   e.stopPropagation();
   swiping = true;
}

答案 2 :(得分:5)

当您正在主动滚动时,在preventDefault上呼叫touchmove在Chrome中无效。为防止出现性能问题,您无法中断滚动。

尝试从preventDefault()致电touchstart,一切正常。

答案 3 :(得分:2)

请删除e.preventDefault(),因为touchmove的event.cancelablefalse。 所以你不能调用这个方法。

答案 4 :(得分:1)

我知道这是一篇过时的文章,但是我有很多问题想要解决,最后我还是想分享。

我的问题是我在ontouchstart中添加了一个事件监听器,并在ontouchend函数中将其删除-像这样

function onTouchStart() {
  window.addEventListener("touchmove", handleTouchMove, {
    passive: false
  });
}

function onTouchEnd() {
  window.removeEventListener("touchmove", handleTouchMove, {
    passive: true
  });
}

function handleTouchMove(e) {
  e.preventDefault();
}

由于某种原因,添加它会像这样删除它,导致此事件随机地无法取消。因此,为了解决这个问题,我保持了侦听器的活动状态,并在布尔值上设置了是否阻止事件发生的布尔值-

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;
}

我实际上是在使用React,所以我的解决方案涉及设置状态,但是我将其简化为更通用的解决方案。希望这对某人有帮助!

相关问题