溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

javascript中mouseenter與mouseover的異同點(diǎn)有哪些

發(fā)布時(shí)間:2021-08-11 15:27:43 來(lái)源:億速云 閱讀:144 作者:小新 欄目:web開(kāi)發(fā)

這篇文章主要介紹javascript中mouseenter與mouseover的異同點(diǎn)有哪些,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

mouseenter與mouseover的異同?

要說(shuō)清楚mouseenter與mouseover有什么不同,也許可以從兩方面去講。

是否支持冒泡 事件的觸發(fā)時(shí)機(jī)

先來(lái)看一張圖,對(duì)這兩個(gè)事件有一個(gè)簡(jiǎn)單直觀的感受。

javascript中mouseenter與mouseover的異同點(diǎn)有哪些 再看看官網(wǎng)對(duì)mouseenter的解釋

mouseenter | onmouseenter event

The event fires only if the mouse pointer is outside the boundaries of the object and the user moves the mouse pointer inside the boundaries of the object. If the mouse pointer is currently inside the boundaries of the object, for the event to fire, the user must move the mouse pointer outside the boundaries of the object and then back inside the boundaries of the object.

大概意思是說(shuō):當(dāng)鼠標(biāo)從元素的邊界之外移入元素的邊界之內(nèi)時(shí),事件被觸發(fā)。而當(dāng)鼠標(biāo)本身在元素邊界內(nèi)時(shí),要觸發(fā)該事件,必須先將鼠標(biāo)移出元素邊界外,再次移入才能觸發(fā)。(英語(yǔ)比較渣:no_mouth:,湊合看哈)

Unlike the onmouseover event, the onmouseenter event does not bubble.

大概意思是:和mouseover不同的是,mouseenter不支持事件冒泡 (英語(yǔ)比較渣:no_mouth:,湊合看哈)

由于mouseenter不支持事件冒泡,導(dǎo)致在一個(gè)元素的子元素上進(jìn)入或離開(kāi)的時(shí)候會(huì)觸發(fā)其mouseover和mouseout事件,但是卻不會(huì)觸發(fā)mouseenter和mouseleave事件

我們用一張動(dòng)圖來(lái)看看他們的區(qū)別(或者點(diǎn)擊該鏈接體驗(yàn))。

javascript中mouseenter與mouseover的異同點(diǎn)有哪些

我們給左右兩邊的ul分別添加了 mouseovermouseenter 事件,當(dāng)鼠標(biāo)進(jìn)入左右兩邊的ul時(shí), mouseovermouseenter 事件都觸發(fā)了,但是當(dāng)移入各自的子元素li的時(shí)候,觸發(fā)了左邊ul上的mouseover事件,然而右邊ul的mouseenter事件沒(méi)有被觸發(fā)。

造成以上現(xiàn)象本質(zhì)上是 mouseenter 事件不支持冒泡所致。

如何模擬mouseenter事件。

可見(jiàn)mouseover事件因其具有冒泡的性質(zhì),在子元素內(nèi)移動(dòng)的時(shí)候,頻繁被觸發(fā),如果我們不希望如此,可以使用mouseenter事件代替之,但是早期只有ie瀏覽器支持該事件,雖然現(xiàn)在大多數(shù)高級(jí)瀏覽器都支持了mouseenter事件,但是難免會(huì)有些兼容問(wèn)題,所以如果可以自己手動(dòng)模擬,那就太好了。

關(guān)鍵因素: relatedTarget 要想手動(dòng)模擬mouseenter事件,需要對(duì)mouseover事件觸發(fā)時(shí)的事件對(duì)象event屬性relatedTarget了解。

relatedTarget事件屬性返回與事件的目標(biāo)節(jié)點(diǎn)相關(guān)的節(jié)點(diǎn)。 對(duì)于mouseover事件來(lái)說(shuō),該屬性是鼠標(biāo)指針移到目標(biāo)節(jié)點(diǎn)上時(shí)所離開(kāi)的那個(gè)節(jié)點(diǎn)。 對(duì)于mouseout事件來(lái)說(shuō),該屬性是離開(kāi)目標(biāo)時(shí),鼠標(biāo)指針進(jìn)入的節(jié)點(diǎn)。 對(duì)于其他類(lèi)型的事件來(lái)說(shuō),這個(gè)屬性沒(méi)有用。

重新回顧一下文章最初的那張圖,根據(jù)上面的解釋?zhuān)瑢?duì)于ul上添加的mouseover事件來(lái)說(shuō),relatedTarget只可能是

ul的父元素wrap(移入ul時(shí),此時(shí)也是觸發(fā)mouseenter事件的時(shí)候, 其實(shí)不一定,后面會(huì)說(shuō)明 ), 或者ul元素本身(在其子元素上移出時(shí)), 又或者是子元素本身(直接從子元素A移動(dòng)到子元素B)。

javascript中mouseenter與mouseover的異同點(diǎn)有哪些

relatedTarget

根據(jù)上面的描述,我們可以對(duì)relatedTarget的值進(jìn)行判斷:如果值不是目標(biāo)元素,也不是目標(biāo)元素的子元素,就說(shuō)明鼠標(biāo)已移入目標(biāo)元素而不是在元素內(nèi)部移動(dòng)。

條件1: 不是目標(biāo)元素 很好判斷 e.relatedTarget !== target(目標(biāo)元素)

條件2:不是目標(biāo)元素的子元素,這個(gè)應(yīng)該怎么判斷呢?

ele.contains

這里需要介紹一個(gè)新的api node.contains(otherNode) , 表示傳入的節(jié)點(diǎn)是否為該節(jié)點(diǎn)的后代節(jié)點(diǎn), 如果 otherNode 是 node 的后代節(jié)點(diǎn)或是 node 節(jié)點(diǎn)本身.則返回true , 否則返回 false

用法案例

<ul class="list">
 <li class="item">1</li>
 <li>2</li>
</ul>
<div class="test"></div>
let $list = document.querySelector('.list')
let $item = document.querySelector('.item')
let $test = document.querySelector('.test')

$list.contains($item) // true
$list.contains($test) // false
$list.contains($list) // true

那么利用contains這個(gè)api我們便可以很方便的驗(yàn)證條件2,接下來(lái)我們封裝一個(gè) contains(parent, node) 函數(shù),專(zhuān)門(mén)用來(lái)判斷 node 是不是 parent 的子節(jié)點(diǎn)

let contains = function (parent, node) {
 return parent !== node && parent.contains(node)
}

用我們封裝過(guò)后的 contains 函數(shù)再去試試上面的例子

contains($list, $item) // true
contains($list, $test) // false
contains($list, $list) // false (主要區(qū)別在這里)

這個(gè)方法很方便地幫助我們解決了模擬mouseenter事件中的條件2,但是悲催的 ode.contains(otherNode) ,具有瀏覽器兼容性,在一些低級(jí)瀏覽器中是不支持的,為了做到兼容我們?cè)賮?lái)改寫(xiě)一下contains方法

let contains = docEle.contains ? function (parent, node) {
 return parent !== node && parent.contains(node)
} : function (parent, node) {
 let result = parent !== node

 if (!result) { // 排除parent與node傳入相同的節(jié)點(diǎn)
 return result
 }

 if (result) {
 while (node && (node = node.parentNode)) {
  if (parent === node) {
  return true
  }
 }
 }

 return false
}

說(shuō)了這么多,我們來(lái)看看用 mouseover 事件模擬 mouseenter 的最終代碼

// callback表示如果執(zhí)行mouseenter事件時(shí)傳入的回調(diào)函數(shù)

let emulateEnterOrLeave = function (callback) {
 return function (e) {
 let relatedTarget = e.relatedTarget
 if (relatedTarget !== this && !contains(this, relatedTarget)) {
  callback.apply(this, arguments)
 }
 }
}

詳細(xì)代碼點(diǎn)擊

代碼示例點(diǎn)擊

好了,我們已經(jīng)通過(guò)mouseove事件完整的模擬了mouseenter事件,但是反過(guò)頭來(lái)看看

對(duì)于ul上添加的mouseover事件來(lái)說(shuō),relatedTarget只可能是

ul的父元素wrap(移入ul時(shí),此時(shí)也是觸發(fā)mouseenter事件的時(shí)候, 其實(shí)不一定,后面會(huì)說(shuō)明 ), 或者ul元素本身(在其子元素上移出時(shí)), 又或者是子元素本身(直接從子元素A移動(dòng)到子元素B)。

我們通過(guò)排查2和3,最后只留下1,也就是mouseenter與mouseover事件一起觸發(fā)的時(shí)機(jī)。既然這樣我們?yōu)槭裁床幌襁@樣判斷呢?

target.addEventListener('mouseover', function (e) {
 if (e.relatedTarget === this.parentNode) {
 // 執(zhí)行mouseenter的回調(diào)要做的事情 
 }
}, false)

這樣不是更加簡(jiǎn)單嗎?,何必要折騰通過(guò)排查2和3來(lái)做?

原因是,target的父元素有一定的占位空間的時(shí)后,我們這樣寫(xiě)是沒(méi)有太大問(wèn)題的,但是反之,這個(gè)時(shí)候 e.relatedTarget 就可能是target元素的父元素,又祖先元素中的某一個(gè)。我們無(wú)法準(zhǔn)確判斷e.relatedTarget到底是哪個(gè)元素。所以通過(guò)排除2和3應(yīng)該是個(gè)更好的選擇。

用mouseout模擬mouseleave事件

當(dāng)mouseout被激活時(shí),relatedTarget表示鼠標(biāo)離開(kāi)目標(biāo)元素時(shí),進(jìn)入了哪個(gè)元素,我們同樣可以對(duì)relatedTarget的值進(jìn)行判斷:如果值不是目標(biāo)元素,也不是目標(biāo)元素的子元素,就說(shuō)明鼠標(biāo)已移出目標(biāo)元素

我們同樣可以用上面封裝的函數(shù)完成

// callback表示如果執(zhí)行mouseenter事件時(shí)傳入的回調(diào)函數(shù)

let emulateEnterOrLeave = function (callback) {
 return function (e) {
 let relatedTarget = e.relatedTarget
 if (relatedTarget !== this && !contains(this, relatedTarget)) {
  callback.apply(this, arguments)
 }
 }
}

以上是“javascript中mouseenter與mouseover的異同點(diǎn)有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI