JavaScript – 獲取滑鼠座標


要使用 JavaScript 獲取滑鼠座標,程式碼如下 -

示例

 即時演示

<!DOCTYPE html>
<html>
<head>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .sample {
      font-size: 25px;
      font-weight: 500;
   }
</style>
</head>
<body>
<h1>Coordinates of mouse</h1>
<p class="sample"></p>
<h3>Hover the mouse over the screen to get its x and y axis position</h3>
<script>
   let sampleEle = document.querySelector(".sample");
   document.body.addEventListener("mousemove", (event) => {
      sampleEle.innerHTML = "X axis: " + event.x + " Y axis: " + event.y;
   });
</script>
</body>
</html>

輸出

以上程式碼將產生以下輸出


更新日期:07-5-2020

399 個瀏覽量

開啟你的 職業生涯

透過完成課程獲取認證

開始
廣告