2013-11-20 84 views
4

我想要檢測用戶是否從邊緣滑動或在我打開面板前用$("#panel").panel("open");。下面是代碼在jQuery Mobile中檢測邊緣滑動

$("#main").on("swiperight",function(event){ 
var data = event.originalEvent.touches ? event.originalEvent.touches[0] : event, 
    coords = [data.pageX, data.pageY]; 

console.log(coords); 
}); 

然而coords因爲我的錯誤未返回任何內容:

Uncaught TypeError: Cannot read property 'touches' of undefined

  1. 那麼,有沒有辦法讓刷卡時會發生座標?

  2. 或者有沒有簡單的方法來檢測起始位置是否從 左邊緣?

謝謝。

+0

類似:http://stackoverflow.com/questions/24163202/javascript-touch-movement-tra ck-when-user-swipes-from-edges – trante

回答

0

你可以做這樣的事情。下面

$('#main').on('touchstart', function(e) { 
    var xPos = e.originalEvent.touches[0].pageX; 
    if(xPos == 0) { //Keep in mind the margin of failure from 0, when user touches. 
     //Handle edge swipe 
    } 
}); 
+0

'touchstart'不告訴我滑動是否發生以及滑動的時間。因爲我還設置了'$ .event.special.swipe.horizo​​ntalDistanceThreshold = 120;'以確保滑動足夠長。 –

+0

澄清我回答了你問的兩個問題。要獲得完整的解決方案,您需要結合自己的解決方案來檢測swiperight事件和我的示例以查看觸摸時間。如果觸摸開始不在正確的邊緣位置,則只需簡單地處理邊緣滑動即可。 – Rayf

5

嘗試在JQM 1.4+,工作對我來說,相應地調整邊緣修剪值,50是爲我好

$("div.ui-page").on("swiperight", function(e) { 
    if (e.swipestart.coords[0] <50) { 
    // your logic 
    } 
}); 

這是爲x座標同樣可以從COORDS得到Y [ 1]

3

這會爲任何屏幕尺寸工作:

$("#main").on("swiperight",function(event){ 
    // take 15% of screen good for diffrent screen size 
    var window_width_15p = $(window).width() * 0.15; 
    // check if the swipe right is from 15% of screen (coords[0] means X) 
    if (event.swipestart.coords[0] < window_width_15p) { 
     // open your panel 
     $("#panel").panel("open"); 
    } 
});