查找事件的位置(将鼠标悬停在按钮上),适用于 Firefox 扩展
我需要为 Firefox 扩展找到事件的位置(鼠标悬停在按钮上)。 我一直在使用:
var obj = $mb(e.target,doc).offset();
var left = obj.left;
var top = obj.top;
但问题是它返回的值是屏幕中事件的位置而不是页面的位置,也就是说它没有考虑滚动位置{在Firefox中}..(但在Google Chrome中)它的工作)。
然后我使用鼠标位置。
var obj_left = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
但问题是它根据引起事件的鼠标位置返回不同的值(按钮两端的同一按钮有不同的值),但在第一种情况下,即使它返回屏幕位置,它也会返回事件的单个值(两端按钮的单个值)。
我需要的是事件的返回值应该相同,并且应该考虑滚动位置。
I need to find the position of an event (mouse over on a button), for Firefox extension.
I have been using:
var obj = $mb(e.target,doc).offset();
var left = obj.left;
var top = obj.top;
But the problem is the value returned by it is the position of an event in the screen not of the page,that is it doesn't take scroll position into consideration {in Firefox} ..(but in Google Chrome its working).
Then I used mouse position.
var obj_left = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
But the problem is it returns different values according to the mouse position that cause event (different values for same button at the two ends of button), but in the first case even though it returns screen position it returns a single value for the event (a single value for a button at the two ends).
What I need is return value should be same for event and should take scroll position into account.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Firefox 中的鼠标事件同时具有
pageX
和clientX
属性(请参阅 https://developer.mozilla.org/en/DOM/event.pageX 和 https://developer.mozilla.org/en/DOM/event.clientX 分别)。似乎pageX
就是您想要的 - 相对于当前页面的位置,无论滚动如何。pageX
是非标准的,可能未在 Chrome 中实现,但 jQuery 似乎以跨浏览器的方式提供此属性,请参阅 http://api.jquery.com/event.pageX/。Mouse events in Firefox have both a
pageX
and aclientX
property (see documentation at https://developer.mozilla.org/en/DOM/event.pageX and https://developer.mozilla.org/en/DOM/event.clientX respectively). It seems thatpageX
is what you want - position relative to the current page, regardless of scrolling.pageX
is non-standard and probably not implemented in Chrome but jQuery seems to provide this property in a cross-browser fashion, see http://api.jquery.com/event.pageX/.