开发者

using javascript to find position of DOM elements

开发者 https://www.devze.com 2023-04-09 20:53 出处:网络
I want to inspect spatial organization of elements o开发者_如何学Cn a web page. Could I get sample code and pointers to resources.

I want to inspect spatial organization of elements o开发者_如何学Cn a web page.

Could I get sample code and pointers to resources.

Thanks.


Here you go:

function position( elem ) {
    var left = 0,
        top = 0;

    do {
        left += elem.offsetLeft;
        top += elem.offsetTop;
    } while ( elem = elem.offsetParent );

    return [ left, top ];
}

Live demo: http://jsfiddle.net/dDyZF/2/


If you want good, modular code that you can read and easily extract just the bits you want, try David Mark's MyLibrary (which you use to build your library).

I find libraries like jQuery are so intricately bound up in themselves and dependant on their own functionality that trying to track down all the functions and re-mapping of properties is an exercise in frustration. On the other hand, MyLibrary is written to be very modular from the start and provides better cross-browser features.


You didn't ask for jQuery, but you might want to check out the jQuery dimensions plugin.

0

精彩评论

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