透過 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>

上面的程式碼將輸出以下內容 −

輸出

觸碰段落後 −


更新日期:16-7-2020

5 千人次瀏覽

開啟您的 職業生涯

完成課程,獲得認證

開始學習
廣告