【问题标题】:Prevent JQuery Mobile swipe event over specific element防止特定元素上的 JQuery Mobile 滑动事件
【发布时间】:2013-03-30 20:52:29
【问题描述】:

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

这是我的 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" );
      }
    }
  });
});

谢谢。

【问题讨论】:

    标签: jquery-mobile jquery swipe preventdefault


    【解决方案1】:

    您需要同时使用stopPropagation();preventDefault();

    至于.selector,可以是tag#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 )',过渡) ?
    • @Iazertyuiop我当然可以使用:not()选择器。
    • 例如,选择器 = "swipeleft", ".foo:not(.ui-slider)", function。 @IzertyuiopI
    猜你喜欢
    • 2013-02-14
    • 2013-02-09
    • 1970-01-01
    • 2015-09-25
    • 2012-04-03
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多