如何在没有鼠标事件的情况下获取jQuery中的鼠标位置? [英] How to get mouse position in jQuery without mouse-events?

查看:86
本文介绍了如何在没有鼠标事件的情况下获取jQuery中的鼠标位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取当前的鼠标位置,但是我不想使用:

I would like to get current mouse position but I don't want to use:

$(document).bind('mousemove',function(e){ 
        $("#log").text("e.pageX: " + e.pageX + ", e.pageY: " + e.pageY); 
}); 

因为我只需要获取职位并处理信息

because I just need to get the position and process the information

推荐答案

我不相信有一种方法可以查询鼠标位置,但是您可以使用仅存储mousemove处理程序信息消失了,因此您可以查询存储的信息.

I don't believe there's a way to query the mouse position, but you can use a mousemove handler that just stores the information away, so you can query the stored information.

jQuery(function($) {
    var currentMousePos = { x: -1, y: -1 };
    $(document).mousemove(function(event) {
        currentMousePos.x = event.pageX;
        currentMousePos.y = event.pageY;
    });

    // ELSEWHERE, your code that needs to know the mouse position without an event
    if (currentMousePos.x < 10) {
        // ....
    }
});

但是,除setTimeout这样的代码外,几乎所有代码都响应事件而运行,并且大多数事件都提供鼠标位置.因此,您需要知道鼠标位置的代码可能已经可以访问该信息...

But almost all code, other than setTimeout code and such, runs in response to an event, and most events provide the mouse position. So your code that needs to know where the mouse is probably already has access to that information...

这篇关于如何在没有鼠标事件的情况下获取jQuery中的鼠标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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