使用 jQuery 在页面加载时检测鼠标悬停鼠标、加载、页面、jQuery

2023-09-07 22:37:41 作者:逃

我想在加载网页时检测鼠标是否在某个元素上.jQuery 似乎无法做到这一点——鼠标悬停、悬停等需要鼠标移动;获取当前鼠标位置也是如此(与元素边界进行比较).

I wanted to detect whether the mouse was over an element when the web page was loaded. It appears this is not possible with jQuery - mouseover, hover etc. require a mouse move; as does getting the current mouse position (to compare with element bounds).

我没有看到有人问过这个具体的问题,但看到有人说这不可能......

I have not seen this specific question asked, but have seen people saying the various bits of this aren't possible...

推荐答案

我的解决方案:使用悬停伪选择器添加一个新的 CSS 值,然后对其进行测试.然而,这似乎只在某些时候有效.

My solution: add a new CSS value with the hover pseudoselector, then test for that. This seems to only work sometimes, however.

CSS:

#el:hover {background-color: transparent; }

jQuery:

if ($('#el').css('background-color') == 'transparent')