本文介绍了如何找出鼠标坐标(setTimeout问题)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是说明所有内容的代码:
Here's the code that explains it all:
$('#elem123').mouseenter(function () {
setTimeout(function () {
//what are mouse coords?
}, 650);
});
推荐答案
鼠标坐标 经过650毫秒之后,相对于元素,我猜呢? (改编自 http://docs.jquery.com/Tutorials:Mouse_Position )
Mouse coordinates after the 650 ms have elapsed, and relative to the element, I am guessing? (Adapted from http://docs.jquery.com/Tutorials:Mouse_Position)
See a working demo
var mouseX = 0, mouseY = 0;
$(document).mousemove(function(event) {
mouseX = event.pageX;
mouseY = event.pageY;
});
$('#elem123').mouseenter(function () {
var t = this;
setTimeout(function () {
var localMouseX = mouseX - t.offsetLeft;
var localMouseY = mouseY - t.offsetTop;
}, 650);
});
为什么使用mousemove
和offsetLeft
?原因是只有发生此类事件时鼠标位置才对我们可用(并且仅相对于整个页面). jQuery没有提供更直接的方法来获取它.
Why use mousemove
and offsetLeft
? The reason is that the mouse position is only available to us when such an event happens (and only relative to the entire page). jQuery does not provide a more direct way to obtain it.
这篇关于如何找出鼠标坐标(setTimeout问题)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!