DIV CSS 佈局教程網

JavaScript 事件實例大全:滑鼠、鍵盤與事件冒泡
編輯:JavaScript基礎知識     

一、document 是最大的元素

document 位於 html 標籤之上,可以說權力最大。下面的範例中,單擊頁面任何位置都會彈出 "a":

<script>
  document.onclick = function () {
    alert('a');
  };
</script>

二、取得滑鼠位置 clientX / clientY

注意這裡取到的是「可視區」內的座標,不含捲動量。

<script>
  document.onclick = function (ev) {
    if (ev) {
      alert(ev.clientX + ',' + ev.clientY);
    } else {
      alert(event.clientX + ',' + event.clientY);
    }
  };
</script>

更簡潔的相容寫法是用短路運算子取事件物件:

<script>
  document.onclick = function (ev) {
    var oEvent = ev || event;
    alert(oEvent.clientX + ',' + oEvent.clientY);
  };
</script>

三、事件冒泡

層層嵌套的元素綁上事件後會形成冒泡。下面的例子裡,document 的點擊會把 div 剛打開的內容立刻關掉:

<script>
  window.onload = function () {
    var obtn = document.getElementById('btn1');
    var odiv = document.getElementById('div1');

    obtn.onclick = function (ev) {
      var oEvent = ev || event;
      odiv.style.display = 'block';
      oEvent.cancelBubble = true; // 阻止冒泡
    };

    document.onclick = function () {
      odiv.style.display = 'none';
    };
  };
</script>
</head>
<body>
  <input type="button" value="顯示" id="btn1"/>
  <div id="div1" style="width:100px;height:150px;background:#ccc;"></div>
</body>

把 cancelBubble 設為 true 之後,點擊事件不會再往上冒到 document,div 才能保持顯示。標準寫法是 stopPropagation()。

四、滑鼠移動跟隨(僅可視區有效)

<title>滑鼠移動</title>
<script>
  window.onmousemove = function (ev) {
    var oEvent = ev || event;
    var odiv = document.getElementById('div1');
    odiv.style.left = oEvent.clientX + 'px';
    odiv.style.top = oEvent.clientY + 'px';
  };
</script>
</head>
<body>
  <div id="div1" style="width:50px;height:50px;background:blue;position:absolute;"></div>
</body>

記得元素要設 position: absolute,否則 left/top 不會生效。

五、鍵盤控制位置與方向

透過 keyCode 取得按下的鍵值來執行對應操作。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<style>
  #div1 { width:100px; height:100px; background:#CCC; position:absolute; }
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script>
  document.onkeydown = function (ev) {
    var oEvent = ev || event;
    var oDiv = document.getElementById('div1');

    // 左 37    右 39

    if (oEvent.keyCode == 37) {
      oDiv.style.left = oDiv.offsetLeft - 10 + 'px';
    }
    else if (oEvent.keyCode == 39) {
      oDiv.style.left = oDiv.offsetLeft + 10 + 'px';
    }
  };
</script>
</head>
<body>
  <div id="div1"></div>
</body>
</html>

六、滑鼠跟隨小尾巴

用一串 div 依序追著前一個的位置,就形成拖尾效果。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<style>
  div { width:10px; height:10px; background:red; position:absolute; }
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script>
  window.onload = function () {
    var aDiv = document.getElementsByTagName('div');
    var i = 0;

    document.onmousemove = function (ev) {
      var oEvent = ev || event;

      for (i = aDiv.length - 1; i > 0; i--) {
        aDiv[i].style.left = aDiv[i - 1].style.left;
        aDiv[i].style.top  = aDiv[i - 1].style.top;
      }

      aDiv[0].style.left = oEvent.clientX + 'px';
      aDiv[0].style.top  = oEvent.clientY + 'px';
    };
  };
</script>
</head>
<body>
  <div></div><div></div><div></div><div></div>
  <div></div><div></div><div></div><div></div>
  <div></div><div></div><div></div><div></div>
  <div></div><div></div><div></div><div></div>
  <div></div><div></div><div></div><div></div>
  <div></div><div></div><div></div><div></div>
  <div></div><div></div><div></div><div></div>
  <div></div><div></div><div></div><div></div>
  <div></div><div></div><div></div><div></div>
  <div></div><div></div><div></div><div></div>
  <div></div><div></div><div></div><div></div>
</body>
</html>

核心邏輯是從尾端往前搬位置:倒序迴圈讓最後一節去接前一節的座標,避免覆蓋尚未搬移的值。

七、單獨取得 keyCode

<script>
  document.onkeydown = function (ev) {
    var oEvent = ev || event;
    alert(oEvent.keyCode);
  };
</script>

八、ctrlKey 組合鍵送出內容

用 Ctrl + Enter 快速把輸入框內容送進留言區。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script>
  window.onload = function () {
    var oBtn = document.getElementById('btn1');
    var oTxt1 = document.getElementById('txt1');
    var oTxt2 = document.getElementById('txt2');

    oBtn.onclick = function () {
      oTxt1.value += oTxt2.value + '\n';
      oTxt2.value = '';
    };

    oTxt2.onkeydown = function (ev) {
      var oEvent = ev || event;
      if (oEvent.ctrlKey && oEvent.keyCode == 13) {
        oTxt1.value += oTxt2.value + '\n';
        oTxt2.value = '';
      }
    };
  };
</script>
</head>
<body>
  <textarea id="txt1" rows="10" cols="40"></textarea><br />
  <input id="txt2" type="text" />
  <input id="btn1" type="button" value="留言" />
</body>
</html>

這裡把按下按鈕與 Ctrl+Enter 兩條路徑共用同一段附加邏輯,是避免程式碼重複的常見手法。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved