使用jQuery在页面加载时检测鼠标悬停 [英] Detect mouse hover on page load with jQuery

查看:118
本文介绍了使用jQuery在页面加载时检测鼠标悬停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在网页加载时检测鼠标是否位于某个元素上。看起来,这是不可能的与jQuery - 鼠标悬停,悬停等需要鼠标移动;就像获取当前的鼠标位置(与元素边界进行比较)一样。

我没有看到这个具体的问题,但看到有人说这个不同的位't possible ...

解决方案

我的解决方案:用悬停伪选取器添加一个新的CSS值,然后测试。然而,这似乎只是有效。



CSS:

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



jQuery:


  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆