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

发布于 2024-11-18 23:04:20 字数 132 浏览 1 评论 0原文

我想检测加载网页时鼠标是否位于某个元素上。看来这对于 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

再见回来 2024-11-25 23:04:20

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

CSS:

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

jQuery:

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

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')
荒人说梦 2024-11-25 23:04:20

这是我解决它的方法:

/** detect if the mouse was hovering over and element when the page loaded */
function detectHoverOnLoad(options) {
    var sel = options.selector

    if ($(sel + ':hover').length) {
        console.log('loaded while HOVERING over ' + sel)
    } else {
        console.log('loaded, but NOT hovering over ' + sel)
    }
}

然后我这样称呼它:

detectHoverOnLoad({selector: '#headerNav'})

通过更多测试,我注意到有时,如果鼠标在移动,它并不总是检测到悬停,所以我在 else 中添加了一个后备来检测鼠标在元素上移动:

function detectHoverOnLoad(options) {
    var sel = options.selector

    if ($(sel + ':hover').length) {
        console.log('loaded while HOVERING over ' + sel)
    } else {
        console.log('NOT hovering')
        $(sel).one('mousemove', function (e) {
            if ($(e.target).parents(sel).length) {
                console.log('MOUSEMOVEed over ' + sel)
            }
        });
    }
}

Here is how I solved it:

/** detect if the mouse was hovering over and element when the page loaded */
function detectHoverOnLoad(options) {
    var sel = options.selector

    if ($(sel + ':hover').length) {
        console.log('loaded while HOVERING over ' + sel)
    } else {
        console.log('loaded, but NOT hovering over ' + sel)
    }
}

then I called it like so:

detectHoverOnLoad({selector: '#headerNav'})

With more testing, I noticed that at times, if the mouse was moving, it did not always detect the hover, so I added a fallback in the else to detect the mouse moving over the element:

function detectHoverOnLoad(options) {
    var sel = options.selector

    if ($(sel + ':hover').length) {
        console.log('loaded while HOVERING over ' + sel)
    } else {
        console.log('NOT hovering')
        $(sel).one('mousemove', function (e) {
            if ($(e.target).parents(sel).length) {
                console.log('MOUSEMOVEed over ' + sel)
            }
        });
    }
}
等待圉鍢 2024-11-25 23:04:20

编辑:经过测试,我得出的结论是这行不通。节省自己的时间并尝试其他事情。

我认为你实际上可以使元素边界框起作用。

这有点像黑客,但策略是使用 element.offset() 来获取元素相对于文档的坐标,以及 element.width() 和 element.width() 。 element.height() 为鼠标位置创建一个边界框。然后,您可以对照此边界框检查事件的 .pageX 和 .pageY 值。

正如您所说,您需要一个事件来获取这些坐标,但这对您不起作用,因为您立即需要它们。尽管如此,您是否考虑过在文档加载后通过调用 $('#some-element').trigger('click') 来伪造鼠标单击事件?您可以预先注册一个函数来通过 $('#some-element).click() 检查边界框并查看它的作用。

EDIT: after testing, I have concluded this will not work. Save yourself the time and try something else.

I think you can actually make the element bounding box work.

This is a bit of a hack, but the strategy is to use element.offset() to get the coordinates of the element relative to the document, along with element.width() & element.height() to create a bounding box for the mouse position. You can then check an event's .pageX and .pageY values against this bounding box.

As you correctly said, you need an event to get these coordinates, which doesn't work for you since you want them immediately. Nonetheless, have you considered faking a mouse click event by calling $('#some-element').trigger('click') after document load? You could register a function to check the bounding box via $('#some-element).click() beforehand and see what it does.

茶花眉 2024-11-25 23:04:20

为什么不呢! :)

这是我的一个解决方案:

DEMO

演示中的代码:

function handler(ev) {
    var target = $(ev.target);
    var elId = target.attr('id');
    if( target.is(".el") ) {
       alert('The mouse was over'+ elId );
    }
}
$(".el").mouseleave(handler);

Why not! :)

Here is a solution of mine:

DEMO

Code from the demo:

function handler(ev) {
    var target = $(ev.target);
    var elId = target.attr('id');
    if( target.is(".el") ) {
       alert('The mouse was over'+ elId );
    }
}
$(".el").mouseleave(handler);
Saygoodbye 2024-11-25 23:04:20

我找到了世界上最简单的方法!
忘记 JQ 悬停使用它的 .mouseover 和 .mouseleave 并为它们定义函数,它不会在加载时和加载后产生任何问题!

   $('li').mouseover(function () {
       codes
   } 


   $('li').mouseleave(function () {
       codes
   }

I found the easiest way that is possible in the world!
forget about JQ hover use its .mouseover and .mouseleave and define functions for both of them, it does not make any problem on load and after load!

   $('li').mouseover(function () {
       codes
   } 


   $('li').mouseleave(function () {
       codes
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文