2014-03-29 26 views
1

我在我的web應用程序中通過鼠標和觸摸拖動事件(由Hammer.js支持)構建了一個垂直滑動的東西。在拖動結束時(當用戶釋放鼠標按鈕或將他的手指從屏幕上移開時,a.k.a dragend事件),如果上面的一個更靠近中間,則它移動到中間,反之亦然。處理拖動的速度/慣性

事情是,當用戶拖動鼠標從下往上(這意味着上面的一個將移動到中間)時,在%48觸發dragend事件時,如果速度足夠高,下面的一個應該移動到中間。如何計算速度足夠高?

回答

0

我解決了它這樣的:

// y is the drag amount 

// visible height 
var height = $(this).height() 

// difference 
var diff = (Math.abs(y) % height)/height 

// I checked Hammer.js's source. It defines velocity as the change in 16ms. 
// So multiplying it by 62.5 gives you the change in 1s. 
// Not sure why I did that, but works pretty well in most situations 
var inertia = event.gesture.velocityY * 62.5 

if (
    (event.gesture.direction == 'up' && diff + (inertia/height) >= 0.5) || 
    (event.gesture.direction == 'down' && diff - (inertia/height) >= 0.5) 
) { 

    // show the one below 

} else { 

    // show the one above 

} 
+2

注意'swipe'手勢可能是你叫什麼有足夠的velocity_一個_pan – Guglie