![](/img/trans.png)
[英]browser events: mouseover doesn't happen when you make element visible (and mouse is on it)
[英]Is there a mouseover fix for when the browser renders the element under the mouse?
在大多數(所有?)瀏覽器中,如果瀏覽器使用鼠標下方的mouseEnter / mouseOver偵聽器呈現元素,則不會觸發該事件。 因此,現在我們將鼠標懸停在元素上,而代碼的作用不像是將鼠標懸停在元素上。
有一個簡單的解決辦法嗎?
您可以使用MutationObserver
監視所有HTML節點的更改,這些更改可能會將頁面上的元素移動到光標下方。 當發生任何此類更改時,您可以使用Document.elementFromPoint
查找懸停的元素,然后在該元素上手動觸發 mouseover
事件:
function dispatchArtificialMouseover() {
const elementMouseIsOver = document.elementFromPoint(
event.clientX,
event.clientY
);
const mouseoverEvent = new MouseEvent("mouseover");
elementMouseIsOver.dispatchEvent(mouseoverEvent);
}
const observerConfig = {
attributes: true,
childList: true,
characterData: true,
subtree: true
};
const observer = new MutationObserver(dispatchArtificialMouseover);
const rootElement = document.body;
observer.observe(rootElement, observerConfig);
但是,我想如果在任何頻繁更改的情況下(例如,當用戶在輸入中鍵入文本時),偵聽頁面上的所有更改將使頁面變慢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.