如何在没有鼠标事件的情况下在 jQuery 中获取鼠标位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4517198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:23:16  来源:igfitidea点击:

How to get mouse position in jQuery without mouse-events?

jquerymouseposition

提问by Martin Vseticka

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

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

回答by T.J. Crowder

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

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

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

Butalmost all code, other than setTimeoutcode 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...

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

回答by lonesomeday

You can't read mouse position in jQuery without using an event. Note firstly that the event.pageXand event.pageYproperties exists on any event, so you could do:

不使用事件就无法在 jQuery 中读取鼠标位置。首先请注意,event.pageXevent.pageY属性存在于任何事件中,因此您可以执行以下操作:

$('#myEl').click(function(e) {
    console.log(e.pageX);
});

Your other option is to use a closure to give your whole code access to a variable that is updated by a mousemove handler:

您的另一个选择是使用闭包让您的整个代码访问由 mousemove 处理程序更新的变量:

var mouseX, mouseY;
$(document).mousemove(function(e) {
    mouseX = e.pageX;
    mouseY = e.pageY;
}).mouseover(); // call the handler immediately

// do something with mouseX and mouseY

回答by Nicu Criste

I used this method:

我用了这个方法:

$(document).mousemove(function(e) {
    window.x = e.pageX;
    window.y = e.pageY;
});

function show_popup(str) {
    $("#popup_content").html(str);
    $("#popup").fadeIn("fast");
    $("#popup").css("top", y);
    $("#popup").css("left", x);
}

In this way I'll always have the distance from the top saved in y and the distance from the left saved in x.

通过这种方式,我将始终将与顶部的距离保存在 y 中,并将与左侧的距离保存在 x 中。

回答by Alfishe

Moreover, mousemoveevents are not triggered if you perform drag'n'drop over a browser window. To track mouse coordinates during drag'n'drop you should attach handler for document.ondragoverevent and use it's originalEvent property.

此外,mousemove如果您在浏览器窗口上执行拖放操作,则不会触发事件。要在拖放期间跟踪鼠标坐标,您应该为document.ondragover事件附加处理程序并使用它的 originalEvent 属性。

Example:

例子:

var globalDragOver = function (e)
{
    var original = e.originalEvent;
    if (original)
    {
        window.x = original.pageX;
        window.y = original.pageY;
    }
}

回答by Oleksandr Tsurika

use window.event- it contains last eventand as any eventcontains pageX, pageYetc. Works for Chrome, Safari, IE but not FF.

使用window.event-它包含最新的event和任何event包含pageXpageY等工程为Chrome,Safari浏览器,IE而不是法郎。

回答by Ala Mouhamed

var CurrentMouseXPostion;
var CurrentMouseYPostion;

$(document).mousemove(function(event) {
    CurrentMouseXPostion = event.pageX;
    CurrentMouseYPostion = event.pageY;
});

Make an eventListener on the main object , in my case the document object, to get the mouse coords every frame and store them in global variables, and like that you can read mouse Y & Z whenever youlike , wherever you like.

在主对象(在我的情况下是文档对象)上创建一个 eventListener,以获取每一帧的鼠标坐标并将它们存储在全局变量中,这样您就可以随时随地读取鼠标 Y 和 Z。

回答by Ande Caleb

I came across this, tot it would be nice to share...

我遇到了这个,很高兴分享......

What do you guys think?

你们有什么感想?

$(document).ready(function() {
  window.mousemove = function(e) {
    p = $(e).position();  //remember $(e) - could be any html tag also.. 
    left = e.left;        //retrieving the left position of the div... 
    top = e.top;          //get the top position of the div... 
  }
});

and boom, there we have it..

和繁荣,我们有它..