如何记录 xy 坐标上的点击,无论页面设计如何,该点击都保持可靠
我正在创建网站热图。我知道如何获取单击的 xy 坐标,但在居中 div 的页面上,单击的 xy 坐标根据用户的浏览器窗口宽度而变化。
如何记录一致的 xy,以便以后无论窗口大小如何都可以显示单击的位置?
我可以使用 jQuery 的 offset() 来获取基于某些居中父元素的 xy 。但我将此热图放置在多个站点上。因此,鉴于每个站点的标记都是唯一的,我如何确定应使用哪个顶级“包装器”元素来计算偏移量?
有没有一些我忽略的更简单的方法?
I'm creating a website heat map. I know how to get the xy coordinate of a click, but on pages in centered divs the xy coordinate of the click varies based on the user's browser window width.
How do I log a consistent xy so that I can display the location of the click later regardless of the window size?
I can use jQuery's offset() to get the xy based on some centered parent element. But I'm placing this heat map on multiple sites. So given that each site's markup is unique, how do I determine what top level "wrapper" element should be used to calculate the offset?
Is there some simpler method I'm overlooking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不仅保存点击的
x
/y
坐标,还保存视图的width
/height
怎么样? -港口。这样,您就可以获取相对于视口的点击位置,并计算点击实际发生在内容上的位置:您可以通过仅在视口尺寸发生变化时获取视口尺寸来优化这一点:
更新
以您为中心的页面可以从视口中心获取坐标:
这将导致页面右侧任何内容的正
x
尺寸和页面右侧任何内容的负x
尺寸页面的左侧。这是一个演示: http://jsfiddle.net/aWBFM/
How about saving not only the
x
/y
coordinates of the click but also thewidth
/height
of the view-port. That way you can get the position of the click relative to the view-port and calculate where on the content the click actually occured:You could optimize this by only getting the view-port dimension when they change:
Update
For pages that are centered you could get the coordinate from the center of the view-port:
This will result in positive
x
dimensions for anything on the right side of the page and negativex
dimensions for anything on the left side of the page.Here is a demo: http://jsfiddle.net/aWBFM/
您是否尝试过使用
元素?我知道在 HTML5 中这不是必需的,但我还没有遇到过不使用它的实例(并且涉及点击)。
Have you tried using the
<body>
element? I know in HTML5 it isn't necessary, but I haven't ever come across an instance where it isn't used (and there is clicking involved).好吧,根据 offset() 文档,它检索相对于文档的坐标,这就是您想要的。您需要做的是获取窗口的高度和宽度。然后使用一些数学计算窗口和地图之间的距离。
$(window).width() 和
$(window).height() 将为您提供所需的内容。
Well according to the offset() documentation it retrieves the coordinates relative to the document, which is what you want. What you need to do is get the window height and width. Then using a bit of maths, calculate the distance between the window and the map.
$(window).width() and
$(window).height() will get you what you need.
您应该能够通过查看 event.clientX 和 event.clientY 来获取鼠标相对于页面左上角的位置。
如果您想要鼠标相对于特定对象的位置,可以查看该对象的 offsetTop 和 offsetLeft 属性。 (event.clientX - obj.offsetLeft) 将是鼠标相对于对象的位置。
You should be able to get the position of the mouse relative to the top left corner of the page by looking at event.clientX and event.clientY.
If you want the position of the mouse relative to a particular object, you can look at the offsetTop and offsetLeft properties of that object. (event.clientX - obj.offsetLeft) would be the position of the mouse relative to the object.