using javascript to find position of DOM elements
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.
精彩评论