无法阻止被动事件侦听器jQuery Mobile中的Default

时间:2019-04-17 18:25:58

标签: jquery mobile jquery-mobile

我已经使用jQuery Mobile v1.5.0使菜单在滑动时打开都正常,但是此错误始终存在,我尝试添加addEventListener示例:

// open menu on swipe to right
$(document).on('swiperight', function(e) {
    document.addEventListener("swiperight", e.preventDefault(), {passive: false} );
    e.preventDefault()
    $('.menu').addClass('active');
});

但是错误仍然存​​在

Unable to preventDefault inside passive event listener due to target being treated as passive.

代码:

// open menu on swipe to right
$(document).on('swiperight', function(e) {
    e.preventDefault()
    $('.menu').addClass('active');
});

2 个答案:

答案 0 :(得分:1)

仅供参考:就我而言(使用Owl Carousel),对我的解决方法是纯CSS:

.owl-carousel {
    touch-action: manipulation; 
}

答案 1 :(得分:0)

无需添加其他eventListener。可以使用CSS解决此错误。将touch-action: none;添加到要刷卡的容器中。

  

这是 DEMO ,它使用外部JQM面板作为全局导航   菜单:

$(document)
  .on('swiperight', '#page-acura, #page-audi, #page-bmw', function(e) {
    $("#panel").panel("open");
  })
  .ready(function() {
    $("#panel").panel({theme: "a", display: "overlay"}).enhanceWithin();
  });
.ui-page,
.ui-panel,
.ui-panel-wrapper,
.ui-panel-dismiss {
  touch-action: none;
}

h1, h2, h3, h4, h5, h6, p {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JQM 1.5.0 RC1 Panel Menu</title>
    <meta name="HandheldFriendly" content="True">
    <meta name="MobileOptimized" content="320">
    <meta name="viewport" content="user-scalable=no, initial-scale=1.0001, maximum-scale=1.0001, width=device-width, minimal-ui shrink-to-fit=no">
    <meta http-equiv="cleartype" content="on">
    <link rel="stylesheet" href="https://code.jquery.com/mobile/1.5.0-rc1/jquery.mobile-1.5.0-rc1.min.css" />
    <script src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
    <script src="https://code.jquery.com/mobile/1.5.0-rc1/jquery.mobile-1.5.0-rc1.min.js"></script>
  </head>
  <body>
    <div data-role="page" id="page-acura">
      <div class="ui-toolbar-header ui-body-a">
        <h2 class="ui-toolbar-title">Acura</h2>
      </div>
      <div role="main" class="ui-content">
        <p>Acura content goes here.</p>
      </div>
      <div class="ui-toolbar-footer ui-toolbar-footer-fixed ui-body-a">
        <h2 class="ui-toolbar-title">Footer</h2>
      </div>
    </div>
    <div data-role="page" id="page-audi">
      <div class="ui-toolbar-header ui-body-a">
        <h2 class="ui-toolbar-title">Audi</h2>
      </div>
      <div role="main" class="ui-content">
        <p>Audi content goes here.</p>
      </div>
      <div class="ui-toolbar-footer ui-toolbar-footer-fixed ui-body-a">
        <h2 class="ui-toolbar-title">Footer</h2>
      </div>
    </div>
    <div data-role="page" id="page-bmw">
      <div class="ui-toolbar-header ui-body-a">
        <h2 class="ui-toolbar-title">BMW</h2>
      </div>
      <div role="main" class="ui-content">
        <p>BMW content goes here.</p>
      </div>
      <div class="ui-toolbar-footer ui-toolbar-footer-fixed ui-body-a">
        <h2 class="ui-toolbar-title">Footer</h2>
      </div>
    </div>
    <div data-role="panel" id="panel">
      <ul data-role="listview" data-icon="false" data-inset="true">
        <li><a href="#page-acura">Acura</a></li>
        <li><a href="#page-audi">Audi</a></li>
        <li><a href="#page-bmw">BMW</a></li>
      </ul>
    </div>
  </body>
</html>