透過 JavaScript 解釋觸點事件
當用戶與觸控裝置互動時,JavaScript 中的觸點事件會被觸發。
以下是指標事件屬性
事件 | 說明 |
---|---|
touchstart。 | 當觸點被放置在觸控表面上時觸發。 |
touchmove | 當觸點在觸控表面上移動時觸發。 |
touchend | 當觸點從觸控表面移除時觸發。 |
touchcancel | 當觸點被中斷時觸發。 |
以下是 JavaScript 中的觸點事件程式碼 −
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result, .sample { font-size: 18px; color: blueviolet; font-weight: 500; } .sample { color: red; } </style> </head> <body> <h1>Touch events in JavaScript</h1> <div class="sample">Here is some sample text to touch</div> <div class="result"></div> <h3>Touch on the above paragraph to make output in the below paragraph visible</h3> <script> let resEle = document.querySelector(".result"); let sampleEle = document.querySelector(".sample"); sampleEle.addEventListener("touchstart", () => { resEle.innerHTML = "Touch start event has been triggered"; }); </script> </body> </html>
上面的程式碼將輸出以下內容 −
輸出
觸碰段落後 −
廣告