Jquery mobile 防止特定元素上的JQuery移动滑动事件

Jquery mobile 防止特定元素上的JQuery移动滑动事件,jquery-mobile,jquery,swipe,preventdefault,Jquery Mobile,Jquery,Swipe,Preventdefault,我正在使用jquery mobile,我需要防止特定元素上的滑动事件。之所以需要这样做,是因为我使用的是slider,我不希望滑动事件被重新调用。我希望当用户使用滑块进行操作时,可以阻止它。我无法找到任何解决方案,所以我在这里寻求帮助 这是我的javascript代码: $( document ).on( "pageinit", "#demo-page", function() { $( document ).on( "swipeleft swiperight", "#demo-page",

我正在使用jquery mobile,我需要防止特定元素上的滑动事件。之所以需要这样做,是因为我使用的是slider,我不希望滑动事件被重新调用。我希望当用户使用滑块进行操作时,可以阻止它。我无法找到任何解决方案,所以我在这里寻求帮助

这是我的javascript代码:

$( document ).on( "pageinit", "#demo-page", function() {
  $( document ).on( "swipeleft swiperight", "#demo-page", function( e ) {

  // We check if there is no open panel on the page because otherwise
  // a swipe to close the left panel would also open the right panel (and v.v.).
  // We do this by checking the data that the framework stores on the page element (panel: open).

   if ( $.mobile.activePage.jqmData( "panel" ) !== "open" ) {
     if ( e.type === "swipeleft"  ) {
        $( "#right-panel" ).panel( "open" );
      } else if ( e.type === "swiperight" ) {
           $( "#left-panel" ).panel( "open" );
      }
    }
  });
});

谢谢。

您需要同时使用这两种方法,
stopPropagation()
preventDefault()

至于
.selector
,它可以是标记和#ID或.class,例如
[data role=page]#PageID
div.ui-content
…等

$(document).on('swipeleft swiperight', '.selector', function(event) {
 event.stopPropagation();
 event.preventDefault();
});
  • 参考:

  • 参考:


在我的例子中,这会阻止滑块移动,但不会阻止页面转换。。。使用.on('swipe',':not(.ui slider),transition)可以实现同样的效果吗?@IazertyuiopI您当然可以使用
:not()
选择器。例如,选择器=
“swipleft”,“.foo:not(.ui slider)”,函数
@IazertyuiopI