开发者

Does the Browser store the calculated coordinates of a div in accessible variables?

If I have a flowing layout (position: static / relative), does the browser store the calculated coordinates (x,y) of a div in p开发者_如何学运维roperties which can be accessed?

Further, it would suffice if the solution worked with Firefox only. JQuery is unfortunately, not an option.


In 'native' JavaScript you can do it like that:

document.getElementById('yourElement').offsetLeft
document.getElementById('yourElement').offsetTop

but you'd probably need to add up few offsets of parent elements depends what position is applied.


It does indeed. Unfortunately it's pretty difficult to get that information out reliably due to browser inconsistencies and general ugliness of raw DOM access.

I suggest jQuery, where you might have code like:

$('#some_div').offset().top

Which will give you the y position of the div from the top left of the document.


No, however using mootools (and probably jquery) you can say $(element).getLeft() or $(element).getTop().

or you could use something like this:

function getLeft(obj) {
    return (obj.offsetParent==null ? obj.offsetLeft : obj.offsetLeft + getLeft(obj.offsetParent));
}

function getTop(obj) {
    return (obj.offsetParent==null ? obj.offsetTop : obj.offsetTop + getTop(obj.offsetParent));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜