Javascript 无法在被动事件侦听器中防止默认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42101723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:54:17  来源:igfitidea点击:

Unable to preventDefault inside passive event listener

javascriptjqueryhtml-framework-7

提问by eozzy

I'm using Framework7 sortable listand it works well, just that it doesn't trigger an event when the list is changed.

我正在使用Framework7 可排序列表,它运行良好,只是在列表更改时不会触发事件。

So I'm trying a few built-in events:

所以我正在尝试一些内置事件:

$('.sortable-handler').on('touchstart', function (e) {
    e.preventDefault();
    alert('touchstart');
});

$('.sortable-handler').on('touchmove', function (e) {
    e.preventDefault();
    console.log('touchmove');
});

$('.sortable-handler').on('touchcancel', function (e) {
    e.preventDefault();
    console.log('touchcancel');
});

$('.sortable-handler').mouseleave(function (e) {
    e.preventDefault();
    console.log('mouseleave');
});

.. but all I get is:

..但我得到的是:

Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080

由于目标被视为被动,无法在被动事件侦听器中阻止默认。见 https://www.chromestatus.com/features/5093566007214080

Which event should I look for to get the updated list on every sort?

我应该寻找哪个事件来获取每种类型的更新列表?

采纳答案by Agus Sapurta Sijabat

To handle sortable list in Framework7 when user release currently sorting element in new position, you can use this code:

要在用户在新位置释放当前排序元素时在 Framework7 中处理可排序列表,您可以使用以下代码:

  $$('li').on('sortable:sort',function(event){
    alert("From " + event.detail.startIndex + " to " + event.detail.newIndex);
  });

Fiddle : https://jsfiddle.net/0zf5w4y7/

小提琴:https: //jsfiddle.net/0zf5w4y7/

回答by Rick Byers

See this blog post. If you call preventDefaulton every touchstartthen you should also have a CSS rule to disable touch scrolling like

请参阅此博客文章。如果你调用preventDefaulteverytouchstart那么你也应该有一个 CSS 规则来禁用触摸滚动,比如

.sortable-handler {
  touch-action: none;
}

回答by ranbuch

For me

为了我

document.addEventListener("mousewheel", this.mousewheel.bind(this), { passive: false });

did the trick (the { passive: false }part).

成功了({ passive: false }部分)。

回答by Halfacht

In plain JS add { passive: false }as third argument

在普通 JS 中添加{ passive: false }为第三个参数

document.addEventListener('wheel', function(e) {
    e.preventDefault();
    doStuff(e);
}, { passive: false });

回答by Saurabh Solanki

I am getting this issue when using owl carousal and scrolling the images.

我在使用 owl carousal 和滚动图像时遇到了这个问题。

So get solved just adding below CSS in your page.

因此,只需在页面中添加以下 CSS 即可解决。

.owl-carousel {
-ms-touch-action: pan-y;
touch-action: pan-y;
}

or

或者

.owl-carousel {
-ms-touch-action: none;
touch-action: none;
}