DOM 对象的 jQuery x y 文档坐标 [英] jQuery x y document coordinates of DOM object

查看:27
本文介绍了DOM 对象的 jQuery x y 文档坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取 DOM 元素的 X、Y 坐标(相对于文档的顶部/左侧).我找不到任何可以提供这些的插件或 jQuery 属性或方法.我可以获取 DOM 元素的顶部和左侧,但这可以相对于其当前容器/父元素或文档.

解决方案

你可以使用 工具栏

I need to get the X,Y coordinates (relative to the document's top/left) for a DOM element. I can't locate any plugins or jQuery property or method that can give these to me. I can get the top and left of the DOM element, but that can be either relative to its current container/parent or to document.

解决方案

you can use Dimensions plugin [Deprecated... included in 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.

knowing this, then it's easy... (using my little svg project as an example page)

var x = $("#wrapper2").offset().left;
var y = $("#wrapper2").offset().top;

console.log('x: ' + x + ' y: ' + y);

output:

x: 53 y: 177

hope it helps what you're looking for.

here's an image of offset() and position()

using XRay

using Web Developer toolbar

这篇关于DOM 对象的 jQuery x y 文档坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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