我需要获取 DOM 元素的 X、Y 坐标(相对于文档的顶部/左侧)。我找不到任何可以给我这些的插件或 jQuery 属性或方法。我可以获得 DOM 元素的顶部和左侧,但这可以是相对于其当前容器/父级或文档的。
最佳答案
你可以使用Dimensions插件 [弃用...包含在 jQuery 1.3.2+ 中]
offset()
Get the current offset of the first matched element, in pixels, relative to the document.position()
Gets the top and left position of an element relative to its offset parent.
知道了这一点,就很容易了...(使用我的小 svg project 作为 example page )
var x = $("#wrapper2").offset().left;
var y = $("#wrapper2").offset().top;
console.log('x: ' + x + ' y: ' + y);
输出:
x: 53 y: 177
希望对您有所帮助。
这是 offset() 和 position() 的图像
使用 XRay
使用 Web Developer工具栏
关于javascript - DOM 对象的 jQuery xy 文档坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1002934/