使用jQuery在页面加载时检测鼠标悬停 [英] Detect mouse hover on page load with jQuery
问题描述
我想在网页加载时检测鼠标是否位于某个元素上。看起来,这是不可能的与jQuery - 鼠标悬停,悬停等需要鼠标移动;就像获取当前的鼠标位置(与元素边界进行比较)一样。
我没有看到这个具体的问题,但看到有人说这个不同的位't possible ...
我的解决方案:用悬停伪选取器添加一个新的CSS值,然后测试。然而,这似乎只是有效。
CSS:
#el:hover {background-color:transparent; }
if($('#el')。css('background-color')=='transparent')
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...
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')
这篇关于使用jQuery在页面加载时检测鼠标悬停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!