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

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

问题描述

我想在加载网页时检测鼠标是否在某个元素上.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')

这篇关于使用 jQuery 在页面加载时检测鼠标悬停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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