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

$(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

推荐答案

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) {
        // ....
    }
});

But除了setTimeout个代码之类的代码外,几乎所有代码都是响应事件运行的,大多数事件都提供鼠标位置.所以你需要知道鼠标在哪里的代码可能已经可以访问这些信息了...

Jquery相关问答推荐

在添加_renderMenu方法时,是什么原因导致jQuery UI AutoComplete抛出TypeError?

Bootstrap 3模态框和video.js视频未正确调整大小

JQuery:$.get 不是函数

为什么在 jQuery 插件中返回 this.each(function())?

jQuery:获取数据属性

jQuery 上传进度和 AJAX 文件上传

如何使用jquery取消绑定所有事件

为什么要两次声明 jQuery?

jQuery UI 对话框 - 关闭后不打开

具有动态大小图像的马赛克网格 gallery

在 jQuery 中处理多个 ID

将数据传递给 jQuery UI 对话框

JQuery .hasClass 用于 if 语句中的多个值

blueimp 文件上传插件中的 maxFileSize 和 acceptFileTypes 不起作用.为什么?

使用 jQuery Select 焦点文本在 Safari 和 Chrome 中不起作用

获取元素的底部和右侧位置

使用 jQuery DataTables 时禁用第一列的自动排序

捕获在页面任意位置按下的 Enter 键

更改 Eclipse 设置以忽略特定文件上的错误

如何在 jQuery 中 Select this中的元素?