本文針對 JavaScript 事件中「事件類型」下的「焦點、滑鼠與滾輪事件」重點進行整理,供大家參考,具體內容如下。
一、焦點事件
這些事件通常會與 document.hasFocus() 方法及 document.activeElement 屬性搭配使用,主要有:
例如:
window.onfocus = function () { console.log('focus'); //focus console.log(document.hasFocus()); //True } window.onblur = function () { console.log('blur'); //blur console.log(document.hasFocus()); //False } 當焦點從頁面中的一個元素轉移到另一個元素時,會依序觸發以下事件:
focusout --> focusin --> blur --> DOMFocusOut --> focus --> DOMFocusIn
二、滑鼠事件
DOM3 級事件定義了 9 個滑鼠事件:
舉例如下:
div.onmouseover = function() { console.log('mouseover'); //在子元素上會觸發 } div.onmouseout = function() { console.log('mouseout'); //在子元素上會觸發 } div.onmouseenter = function() { console.log('mouseenter'); //在子元素上不會觸發 } div.onmouseleave = function() { console.log('mouseleave'); //在子元素上不會觸發 } 只有在同一個元素上先後發生 mousedown 與 mouseup 事件,才會觸發 click 事件;只有觸發兩次 click 事件,才會隨之觸發一次 dblclick 事件。順序如下:
mousedown --> mouseup --> click --> mousedown --> mouseup --> click --> dblclick
IE8 之前的版本有個 bug:在雙擊事件中,會跳過第二個 mousedown 與 click 事件。
三、滾輪事件
1、客戶區座標位置 clientX 與 clientY 屬性,例如:
window.onmousemove = function() { clickX = event.clientX; clickY = event.clientY; var div = document.createElement("img"); div.src = "hhh.gif" div.style.position = "absolute"; div.style.width = '100px'; div.style.left = clickX + "px"; div.style.top = clickY + "px"; document.body.appendChild(div); }; 2、頁面座標位置 pageX 與 pageY:
window.onclick = function() { clickX = event.pageX; clickY = event.pageY; var div = document.createElement("img"); div.src = "ppp.png" div.style.position = "absolute"; div.style.width = '100px'; div.style.left = clickX + "px"; div.style.top = clickY + "px"; document.body.appendChild(div); }; IE8 及更早版本不支援這個頁面座標位置,但可以計算出來,需用到混合模式下 document.body 與標準模式下 document.documentElement 的 scrollLeft 與 scrollTop 屬性:
if (clickX === undefined) { clickX = event.clientX + (document.body.scrollLeft || document.documentElement.scrollLeft); }; if (clickY === undefined) { clickY = event.clientY + (document.body.scrollTop || document.documentElement.scrollTop); }; 3、螢幕座標位置 screenX 與 screenY,透過該屬性可獲得相對於螢幕的座標。
4、修改鍵
修改鍵有 Shift、Ctrl、Alt、Meta(Windows 上的 Windows 鍵、蘋果機上的 Cmd 鍵);對應的修改鍵狀態是 shiftKey、ctrlKey、altKey、metaKey,這些屬性包含的都是布林值——若相應的鍵被按下則為 true,否則為 false。例如:
var array = []; var timing = setTimeout(showArray, 100); window.onclick = function() { if (event.shiftKey) { array.push("shift"); }; if (event.ctrlKey) { array.push("ctrl"); }; if (event.altKey) { array.push("alt"); }; if (event.metaKey) { array.push("meta"); }; }; function showArray() { var str = array.toString(); var newP = document.createElement("p"); newP.appendChild(document.createTextNode(str)); document.body.appendChild(newP); timing = setTimeout(showArray, 1000); } 5、相關元素
event.relatedTarget 與 event.target;
relatedTarget 屬性提供了相關元素的資訊,但只在 mouseover 與 mouseout 事件中才有值,其他事件則為 null;IE8 之前的版本不支援 relatedTarget,在 mouseover 觸發時,IE 的 fromElement 屬性保存了相關元素;在 mouseout 觸發時,IE 的 toElement 屬性保存著相關元素。例如:
var dot = document.getElementById("dot"); dot.onmouseout = function (){ console.log("mouse out from" + event.target.tagName + "to" + event.relatedTarget.tagName); }; 又如:
function getRelatedTarget() { if (event.ralatedTarget) { return event.relatedTarget; } else if (event.toElement) { return event.toElement; } else if (event.fromElement) { return event.fromElement; } else { return null; } } 四、滑鼠按鈕
1、button 屬性
DOM 的 event.button 屬性有三個值:0 為主滑鼠按鈕、1 為中間滑鼠按鈕、2 為次滑鼠按鈕。在常規設定中,主滑鼠按鈕就是滑鼠左鍵,次滑鼠按鈕就是滑鼠右鍵。
IE8 及之前的版本也提供了 button 屬性,但值與 DOM 的 button 屬性有很大差異:
相容版:
function getButton() { if (document.implementation.hasFeature("MouseEvents", "2.0")) { return event.button; } else { switch (event.button) { case 0: case 1: case 3: case 5: case 7: return 0; case 2: case 6: return 2; case 4: return 1; } } } 另外,若要屏蔽滑鼠右鍵,應該使用:
document.oncontextmenu = function(event) { // if (window.event) { // event = window.event; // } // try { // var the = event.srcElement; // if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) { // return false; // } // return true; // } catch (e) { // return false; // } return false; } 這個事件是 HTML5 定義的,以後再討論。
2、更多的事件資訊
detail 屬性
對於滑鼠事件來說,detail 包含一個數值,表示在給定位置上發生了多少次單擊(一次 mousedown 加一次 mouseup),次數從 1 開始計數;若 mousedown 與 mouseup 之間移動了位置,detail 會被重置為 0;若單擊間隔太長,也會被重置為 0。
3、滑鼠滾輪事件
mousewheel 事件與 wheelDelta 屬性
在垂直方向上滾動頁面時就會觸發 mousewheel,event 物件裡的 wheelDelta 屬性表示:當用戶向前滾動滾輪時,wheelDelta 是 120 的倍數;向後滾動時,則是 -120 的倍數。例如:
var clientHeight = document.documentElement.clientHeight; var divs = document.getElementsByTagName("div"); for (var i = 0; i < divs.length; i++) { divs[i].style.height = clientHeight + "px"; }; window.onmousewheel = function () { if (event.wheelDelta <= -120) { window.scrollBy(0,clientHeight); }else if(event.wheelDelta >= 120){ window.scrollBy(0,-clientHeight); }; } 另外,在 Opera 9.5 之前的版本中,wheelDelta 值的正負號是顛倒的。此外,Firefox 支援一個名為 DOMMouseScroll 的類似事件,同樣在滾動滾輪時觸發,相關資訊保存在 detail 屬性中:向前滾動時其值為 -3 的倍數,向後滾動則為 3 的倍數。通用版如下:
function getWheelDelta() { if (event.wheelDelta) { return (client.engine.opera && client.engine.opera < 9.5 ? -event.wheelDelta : event.wheelDelta); } else { return -event.detail * 40; }; } 4、觸控設備
在 iOS 與 Android 設備中:
5、無障礙性問題
以上就是本文的全部內容,希望對大家的學習有所幫助。