是否有可能获得鼠标位置与JavaScript页面加载后,没有任何鼠标移动事件(不移动鼠标)?
当前回答
编辑2020:这不再工作。看起来是这样,浏览器供应商修补了这个问题。因为大多数浏览器都依赖于chromium,所以它可能位于浏览器的核心。
旧的回答: 您还可以钩子mouseenter(当mousecursor在页面内时,该事件在页面重新加载后触发)。扩展损坏的代码应该做的伎俩:
Var x = null; Var y = null; 文档。addEventListener(' mouemove ', onMouseUpdate, false); 文档。addEventListener('mouseenter', onMouseUpdate, false); 函数onMouseUpdate(e) { x = e.pageX; y = e.pageY; console.log (x, y); } 函数getMouseX() { 返回x; } 函数getMouseY() { 返回y; }
你也可以在mouseleave-event上设置x和y为空。你可以用光标检查用户是否在你的页面上。
其他回答
重复@超新星的回答,这里有一个使用ES6类的方法,它在你的回调中保持正确的上下文:
class Mouse { constructor() { this.x = 0; this.y = 0; this.callbacks = { mouseenter: [], mousemove: [], }; } get xPos() { return this.x; } get yPos() { return this.y; } get position() { return `${this.x},${this.y}`; } addListener(type, callback) { document.addEventListener(type, this); // Pass `this` as the second arg to keep the context correct this.callbacks[type].push(callback); } // `handleEvent` is part of the browser's `EventListener` API. // https://developer.mozilla.org/en-US/docs/Web/API/EventListener/handleEvent handleEvent(event) { const isMousemove = event.type === 'mousemove'; const isMouseenter = event.type === 'mouseenter'; if (isMousemove || isMouseenter) { this.x = event.pageX; this.y = event.pageY; } this.callbacks[event.type].forEach((callback) => { callback(); }); } } const mouse = new Mouse(); mouse.addListener('mouseenter', () => console.log('mouseenter', mouse.position)); mouse.addListener('mousemove', () => console.log('mousemove A', mouse.position)); mouse.addListener('mousemove', () => console.log('mousemove B', mouse.position));
你可以尝试一些类似Tim Down建议的东西——但不是为屏幕上的每个像素设置元素,而是只创建2-4个元素(框),并动态更改它们的位置、宽度和高度,递归地将屏幕上可能的位置除以2-4,从而快速找到鼠标的真实位置。
例如,第一个元素占据屏幕的左右半部分,然后是上下半部分。到目前为止,我们已经知道鼠标位于屏幕的哪四分之一,能够重复—发现这个空间的哪四分之一……
如果你渲染2000 x 2000 <a>个元素,@Tim Down的答案是不性能的:
OK, I have just thought of a way. Overlay your page with a div that covers the whole document. Inside that, create (say) 2,000 x 2,000 elements (so that the :hover pseudo-class will work in IE 6, see), each 1 pixel in size. Create a CSS :hover rule for those elements that changes a property (let's say font-family). In your load handler, cycle through each of the 4 million elements, checking currentStyle / getComputedStyle() until you find the one with the hover font. Extrapolate back from this element to get the co-ordinates within the document. N.B. DON'T DO THIS.
但是你不需要一次呈现400万个元素,而是使用二分搜索。只需使用4 <a>元素即可:
第一步:考虑整个屏幕作为开始搜索区域 步骤2:将搜索区域分割成2 × 2 = 4个矩形<a>个元素 步骤3:使用getComputedStyle()函数确定鼠标悬停在哪个矩形中 步骤4:将搜索区域缩小到该矩形,并重复步骤2。
这样你将需要重复这些步骤最多11次,考虑到你的屏幕不超过2048px。
所以你将生成最多11 x 4 = 44 <a>个元素。
如果你不需要确定鼠标位置精确到一个像素,但说10px的精度是可以的。您将重复这些步骤最多8次,因此您将需要绘制最多8 x 4 = 32 <a>个元素。
生成并销毁<a>元素也不能执行,因为DOM通常很慢。相反,您可以重用初始的4 <a>元素,并在循环执行步骤时调整它们的顶部、左侧、宽度和高度。
现在,创建4 <a>也是多余的。相反,您可以在每个矩形中测试getComputedStyle()时重用相同的<a>元素。因此,不需要将搜索区域分割为2 × 2 <a>元素,只需要重复使用一个<a>元素,使用顶部和左侧样式属性移动它。
所以,所有你需要的是一个<a>元素改变它的宽度和高度最多11次,改变它的顶部和左侧最多44次,你就会有准确的鼠标位置。
我设想,也许您有一个带有计时器的父页面,在一定时间或任务完成后,您将用户转发到一个新页面。现在你想要光标的位置,因为它们在等待,所以它们不一定会碰到鼠标。因此,使用标准事件跟踪父页面上的鼠标,并通过get或post变量将最后一个值传递给新页面。
你可以在你的父页上使用JHarding的代码,这样最新的位置总是在全局变量中可用:
var cursorX;
var cursorY;
document.onmousemove = function(e){
cursorX = e.pageX;
cursorY = e.pageY;
}
这将无法帮助通过父页面以外的方式导航到此页面的用户。
var x = 0;
var y = 0;
document.addEventListener('mousemove', onMouseMove, false)
function onMouseMove(e){
x = e.clientX;
y = e.clientY;
}
function getMouseX() {
return x;
}
function getMouseY() {
return y;
}