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

Javascript 触摸移动被卡住忽略尝试取消触摸移动,javascript,jquery,event-handling,slider,touch,Javascript,Jquery,Event Handling,Slider,Touch,我在触摸滑块上处理触摸事件,不断出现以下错误: 已忽略取消可取消=false的touchmove事件的尝试, 例如,因为滚动正在进行中,无法进行 打断了 我不确定是什么导致了这个问题,我对触摸事件还不熟悉,似乎无法解决这个问题 以下是处理触摸事件的代码: Slider.prototype.isSwipe = function(threshold) { return Math.abs(deltaX) > Math.max(threshold, Math.abs(deltaY)); }

我在触摸滑块上处理触摸事件,不断出现以下错误:

已忽略取消可取消=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));
}
Slider.prototype.isSwipe=函数(阈值){
返回Math.abs(deltaX)>Math.max(阈值,Math.abs(deltaY));
}
Slider.prototype.touchStart=函数(e){
如果(此项)返回false;
触摸移动=真;
deltaX=deltaY=0;
if(e.originalEvent.touchs.length==1){
startX=e.originalEvent.touchs[0].pageX;
startY=e.originalEvent.touchs[0].pageY;
this.$slider.on('touchmove touchcancel',this.touchmove.bind(this)).one('touchend',this.touchend.bind(this));
isFlick=true;
setTimeout(函数(){
isFlick=false;
},flickTimeout);
}
}
Slider.prototype.touchMove=函数(e){
deltaX=startX-e.originalEvent.touchs[0].pageX;
deltaY=startY-e.originalEvent.touchs[0].pageY;
if(此.isSwipe(swipeThreshold)){
e、 预防默认值();
e、 停止传播();
刷卡=正确;
}
如果(刷卡){
此.slide(deltaX/this.\u sliderWidth,true)
}
}
Slider.prototype.touchEnd=函数(e){
var threshold=isFlick?swipeThreshold:这个;
如果(此.isSwipe(阈值)){
deltaX<0?this.prev():this.next();
}
否则{
本幻灯片(0,!deltaX);
}
刷卡=假;
this.$slider.off('touchmove',this.touchmove).one(transitionend,$.proxy)(函数(){
本幻灯片(0,真);
触摸移动=错误;
}这),;
}
您可以找到实际的滑块


如果你足够快地擦过球,它会抛出错误,有时会被卡住。我还是不明白为什么它不起作用。如有任何帮助/见解,将不胜感激。不确定我做错了什么。

我遇到了这个问题,我所要做的就是从touchend返回true,警告消失了。

在你主动滚动时调用
touchmove
上的
preventDefault
在Chrome中不起作用。为了防止性能问题,您不能中断滚动


尝试从
touchstart
调用
preventDefault()
,一切都应该正常。

请删除
e.preventDefault()
,因为
事件。touchmove的可取消
false

因此您不能调用此方法。

事件必须是可取消的。添加
if
语句可以解决此问题

if (e.cancelable) {
   e.preventDefault();
}
在代码中,您应该将其放在此处:

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

我知道这是一个老帖子,但我有很多问题要解决,我终于做到了,我想与大家分享

我的问题是,我在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,所以我的解决方案涉及到设置状态,但我已经将其简化为更通用的解决方案。希望这对某人有帮助

然后我得到:忽略取消touchstart事件的尝试事件侦听器按附件的顺序调用@Curtis也许您正在将
touchstart
事件处理程序添加到调用
preventDefault()
的事件处理程序之前,Chrome已经开始了逻辑(滚动、pinchzoom)它阻止取消
touchstart
/
touchmove
事件。在尝试调用此语句中的
e.preventDefault()
之前,我在if语句中添加了
e.cancelable