问题描述
我有一个.hover()动作分配给一个元素,以根据其上方的光标隐藏/显示某些内容.我遇到的问题是,当页面加载并且光标在OVER元素上时,它没有注册为结束,因为它没有触发mouseenter事件.
I have a .hover() action assigned to an element to hide/show something depending on the cursor being over it. The problem I am running into is that when the page loads and the cursor is OVER the element it doesn't register as being over because its not firing the mouseenter event.
还有另一种方法来判断光标是否在所需元素上吗?
Is there a another way to tell if the cursor is over a desired element?
推荐答案
使用页面加载时的鼠标位置可以调用
Using the mouse position on page load you could call
var currentElement = document.elementFromPoint(x, y);
然后,如果在该位置找到的元素是发生悬停事件的元素.您可以手动触发该悬停事件
then if the element found at that position is the one with the hover event. You can trigger that hover event manually
$(currentElement).trigger('hover');
这篇关于光标是否位于元素上方(页面加载时)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!