如何在没有事件的情况下获得鼠标位置(不移动鼠标)? [英] How to get the mouse position without events (without moving the mouse)?

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

问题描述

在没有任何鼠标移动事件(没有移动鼠标)的情况下加载页面后是否可以使用JavaScript获取鼠标位置?

Is it possible to get the mouse position with JavaScript after page loads without any mouse movement event (without moving the mouse)?

推荐答案

真实回答:不,这是不可能的。

Real answer: No, it's not possible.

好的,我刚想到了一个方法。使用覆盖整个文档的div覆盖页面。在其中,创建(比方说)2,000 x 2,000 < a> 元素(以便:hover 伪类将在IE 6中工作,参见),每个像素大小。为那些更改属性的< a> 元素创建一个CSS :hover 规则(假设字体家庭)。在您的加载处理程序中,循环浏览每个400万< a> 元素,检查 currentStyle / getComputedStyle(),直到找到具有悬停字体的那个。从该元素推断回来以获得文档中的坐标。

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 <a> 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 <a> elements that changes a property (let's say font-family). In your load handler, cycle through each of the 4 million <a> 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。 不要这样做

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

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