2016-11-28 34 views
1

我可以檢查是否mousedown已被觸發,像這樣:如果事件類型等於touchstart

$(something).on("mousemove touchmove", function(e) { 
    if (e.buttons == 1){ 
     // mouse is down 
    } 
}) 

很簡單的問題,我怎麼能檢查是否touchstart已經在上面被觸發?我試過了:

if (e.type === 'touchstart'){ 
    // touchstart has been triggered 
} 

現在確實可以工作了。有人可以提供關於如何實現這一目標的方向嗎?

回答

1

您的if語句檢查e.type屬性是使用的正確邏輯。你的情況雖然失敗,因爲你鉤到mousemovetouchmove,而不是touchstart

您需要可以包括touchstart在事件列表:

$(something).on("mousemove touchmove touchstart", fn); 

或者檢查type屬性touchmove,如果這是你的意圖:

if (e.type === 'touchmove'){ 
    // touchmove has been triggered 
} 
+0

我的目的是檢查touchstart,因爲我想知道用戶是否在觸摸設備上滑動。我剛剛在Windows手機上檢查過它,它很有用,謝謝! –

+0

沒問題,很樂意幫忙。 –