2013-03-30 28 views
6

Tôi đang sử dụng thiết bị di động jquery và tôi cần phải ngăn sự kiện vuốt qua phần tử cụ thể. Cần làm điều này là bởi vì tôi đang sử dụng thanh trượt và tôi không muốn sự kiện vuốt được gọi là resp. Tôi muốn nó được ngăn chặn khi người dùng thao tác với thanh trượt. Tôi đã không thể tìm thấy bất kỳ giải pháp vì vậy tôi yêu cầu giúp đỡ ở đây.Ngăn sự kiện vuốt trên thiết bị di động JQuery qua phần tử cụ thể

Đây là mã javascript của tôi:

$(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"); 
     } 
    } 
    }); 
}); 

Cảm ơn bạn.

Trả lời

11

Bạn cần sử dụng cả hai, stopPropagation();preventDefault();.

Đối với .selector, nó có thể là một thẻ #ID hoặc .class, ví dụ [data-role=page]#PageID, div.ui-content ... vv

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

Cũng trong trường hợp của tôi, điều này ngăn ngừa sự trượt sang di chuyển nhưng không phải là p chuyển đổi độ tuổi ... Có thể đạt được điều tương tự với .on ('swipe', ': not (.ui-slider)', transition) không? – IazertyuiopI

+0

@IazertyuiopI bạn có thể sử dụng ': not()' selector tất nhiên. Ví dụ: – Omar

+0

, selector = '" swipeleft "," .foo: not (.ui-slider) ", function'. @IazertyuiopI – Omar

Các vấn đề liên quan