2013-04-17 17 views
0

我對'touchstart'事件有疑問。我有一個按鈕,我想觸摸它。所以,我不喜歡這個「touschstart」:JavaScript touchstart

var button = document.getElementById('btn'); 
button.addEventListener('touchstart', function(){something}); 

的問題是,「touchstart」使按鈕點擊一次。當用戶仍然觸摸它時,我希望按鈕始終處於「按下」狀態。怎麼做?

+0

當按鈕被按下時,你想要做什麼?如果你正在拖動一些東西,你可以使用'touchmove',如果你想做一個連續的動作,你可以在'touchstart'上創建一個setInterval,然後在'touchend'結束它。 – Snuffleupagus

回答

1

我相信你必須使用兩個事件。 tocuhstarttouchend

var button = document.getElementById('btn'); 
button.addEventListener('touchstart', function(){// make the button pressed}); 
button.addEventListener('touchend', function(){// make the button unpressed}); 
+0

這不起作用。它不會繼續「按下」狀態。它只是像'點擊'一樣工作,不像'按鼠標按鈕並按住' – Amay

+1

您將該對象設置爲javascript中的「按下」狀態,並在該事件中使用JavaScript刪除「按下」狀態。它不會是魔術。你認爲「被按下」的狀態是什麼? – gbtimmon

+1

@gbtimmon生活的樂趣沒有魔法:( – Goodwine