2013-03-30 69 views
6

我正在使用jquery mobile,并且需要防止特定元素上的滑动事件。这样做的需要是因为我正在使用滑块,我不希望刷新事件被调用。我希望在用户使用滑块进行操作时阻止它。我无法找到任何解决方案,所以我在这里寻求帮助。防止特定元素上的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"); 
     } 
    } 
    }); 
}); 

谢谢。

回答

11

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

至于.selector,它可以是一个标签#ID的.class,例如[data-role=page]#PageIDdiv.ui-content ...等

$(document).on('swipeleft swiperight', '.selector', function(event) { 
event.stopPropagation(); 
event.preventDefault(); 
}); 
+0

那么在我的情况下,这样可以防止滑块移动但不是p年龄过渡...是否有可能通过.on('swipe',':not(.ui-slider)',transition)来实现? – IazertyuiopI

+0

@Iazertyuiop我当然可以使用':not()'选择器。 – Omar

+0

例如,selector ='“swipeleft”,“.foo:not(.ui-slider)”,function'。 @IazertyuiopI – Omar