所以,我找到了解决办法。
$(document).on('pageinit', '#page', function() {
$(document).on("swipeleft swiperight", "#page", function(e) {
console.log('swiped!!')
});
});
这是我发布的伪代码。事实证明,console.log 消息在每次滑动时都会被调用,但上面代码中省略的面板打开/关闭调用却没有。
这是完整的旧代码:
$(document).on('pageinit','#page', function(){
$(document).on("swipeleft swiperight", "#page", function(e) {
console.log('swiped!!')
// 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" );
}
}
else if ($.mobile.activePage.jqmData( "panel" ) == "open"){
$( "#left-panel" ).panel( "close" );
$( "#right-panel" ).panel( "close" );
}
});
}
$(document).on("swipeleft swiperight", "#page", function(e) {}
这些更改修复了代码:将选择器从swipeleft swiperight 函数中移除
$(document).on("swipeleft swiperight", function(e) {}
,我添加e.stopPropagation()
了事件。我认为它一定是 JQM 事件传播使 DOM 冒泡并破坏了一切。
$(document).on('pageinit', '#page', function() {
$(document).on("swipeleft swiperight", function(e) {
e.stopPropagation();
console.log('swiped!!')
// 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" );
}
}
else if ($.mobile.activePage.jqmData( "panel" ) == "open"){
$( "#left-panel" ).panel( "close" );
$( "#right-panel" ).panel( "close" );
}
});
}