问题描述
我正在使用jquery mobile,并且需要防止在特定元素上发生滑动事件.需要执行此操作是因为我正在使用滑块,并且我不希望分别调用swipe事件.我希望在用户使用滑块进行操作时阻止它.我也找不到任何解决方案,因此我在这里寻求帮助.
I am using jquery mobile and I need to prevent swipe event over specific element. Need of doing this is because I am using slider and I don't want the swipe event to be invoked resp. I want it to be prevented when user is manipulating with slider. I was not able to too find any solution so I am asking for help here.
这是我的JavaScript代码:
This is my javascript code:
$( 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
...等
As for the .selector
, it can be a tag and #ID or .class, e.g. [data-role=page]#PageID
, div.ui-content
...etc
$(document).on('swipeleft swiperight', '.selector', function(event) {
event.stopPropagation();
event.preventDefault();
});
-
这篇关于防止JQuery Mobile滑动事件超过特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!