如何检查光标是否在元素上? [英] How to check if the cursor is over an element?

查看:171
本文介绍了如何检查光标是否在元素上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用JQuery / Javascript在html页面上检查光标是否超过 div

How can I check whether the cursor is over a div on the html page with JQuery/Javascript?

我正在尝试获取光标坐标以查看它们是否在我元素的矩形中。也许有预定义的方法?

I'm trying to get cursor coordinates to see if they are in the rectangle of my element. Maybe there are predefined methods?

UPD,不要说任何关于悬停事件等等我需要一些将为页面上的某个元素返回true / false的方法,如:

UPD, don't say anything about hover events, etc. I need some method which will return true/false for some element at the page, like:

var result = underElement('#someDiv'); // true/false


推荐答案

我不太确定为什么你希望避免如此糟糕地悬停:考虑以下脚本

I'm not really sure why you wish to avoid hover so badly: consider the following script

$(function(){

    $('*').hover(function(){
        $(this).data('hover',1); //store in that element that the mouse is over it
    },
    function(){
        $(this).data('hover',0); //store in that element that the mouse is no longer over it
    });


    window.isHovering = function (selector) {
        return $(selector).data('hover')?true:false; //check element for hover property
    }
});

基本上我的想法是你使用悬停在鼠标悬停在它上面的元素上设置一个标志/不再超过它。然后你编写一个检查该标志的函数。

Basically the idea is that you use hover to set a flag on the element that the mouse is over it/no longer over it. And then you write a function that checks for that flag.

这篇关于如何检查光标是否在元素上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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