gpt4 book ai didi

javascript - DOM 对象的 jQuery xy 文档坐标

转载 作者:IT王子 更新时间:2023-10-29 03:00:56 25 4
gpt4 key购买 nike

我需要获取 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

alt text

使用 Web Developer工具栏

alt text

关于javascript - DOM 对象的 jQuery xy 文档坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1002934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com