HTML DOM touchmove 事件
HTML DOM touchmove 事件會在觸控式螢幕上移動觸控時觸發。
注意 − 此事件僅適用於觸屏裝置。
以下是其語法 −
在 HTML 中觸發 touchmove 事件 −
ontouchmove = "eventFunction()"
在 JavaScript 中觸發 touchmove 事件 −
eventObject.ontouchmove = eventFunction
注意 − 我們在移動裝置或支援觸控操作的系統上訪問線上 HTML 編輯器時執行觸控事件示例。這樣做是為了讓我們能夠執行觸控操作,例如觸控式螢幕幕 2 秒鐘。
下面讓我們來看一個touchmove 事件屬性的示例 −
示例
<!DOCTYPE html> <html> <head> <title>HTML DOM touchmove event</title> <style> * { padding: 2px; margin:5px; } form { width:70%; margin: 0 auto; text-align: center; } #outer { width:70%; margin: 0 auto; padding: 0; text-align: center; border:1px solid black; height: 105px; background-color: #28a745; } input[type="button"] { border-radius: 10px; } #upper { border-bottom: 1px solid black; height: 40px; margin: 0 0 15px 0; background-color: #DC3545; } #lower { border-top: 1px solid black; height: 40px; margin: 15px 0 0 0; background-color: #DC3545; } </style> </head> <body> <form> <fieldset> <legend>HTML DOM touchmove event</legend> <div id="outer"> <div id="upper"><h2>Danger</h2></div> <div id="lower"><h2>Danger</h2></div> </div> <input type="button" id="start" value="Start" onclick="gameStart()"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById('divDisplay'); var gameDisplay = document.getElementById('outer'); function playGame(event) { var x = event.touches[0].clientX; var y = event.touches[0].clientY; if(y > 95 && y < 110){ divDisplay.textContent = 'Keep Going!'; if(x === 439){ divDisplay.textContent = 'Congrats! You Did it!'; gameDisplay.removeEventListener('touchmove', playGame); } } else{ divDisplay.textContent = 'You moved to DANGER area. You loose!'; gameDisplay.removeEventListener('touchmove', playGame); } } function gameStart(){ gameDisplay.addEventListener('touchmove',playGame); } </script> </body> </html>
輸出
點選“開始”按鈕後,游標位於綠色(安全)區域 −
點選“開始”按鈕後,游標位於綠色(安全)區域的末端 −
點選“開始”按鈕後,游標位於紅色(危險)區域 −
廣告