| : | Javascript Index | : | PHP | : | MySQL | : |
| : | Source | : | : | Explanation | : | : | Example | : | : | Todo | : | : | Feedback | : |
Finds the position of any element / tag anywhere on the screen
Added: 2008-08-18 19:53:07
/******
* You may use and/or modify this script as long as you:
* 1. Keep my name & webpage mentioned
* 2. Don't use it for commercial purposes
*
* If you want to use this script without complying to the rules above, please contact me first at: marty@excudo.net
*
* Author: Martijn Korse
* Website: http://devshed.excudo.net
*
* Date: 2008-08-18 19:53:07
***/
/**
* This function determines the x and y coordinate of the passed element on the screen.
*
* @param DOM object el
* @see calcPositionCorrection
* @return array an array of two values of which the first contains the x coordinate and the second the y coordinate *
*/
function getElementPosition(el)
{
var curLeft = 0;
var curTop = 0;
var corrections = calcPositionCorrection(el);
if (el.offsetParent)
{
do {
curLeft += el.offsetLeft;
curTop += el.offsetTop;
} while (el = el.offsetParent);
return [ (curLeft - corrections[0]), (curTop - corrections[1]) ];
}
}
function calcPositionCorrection(el)
{
var curX = 0;
var curY = 0;
do {
curX += el.scrollLeft;
curY += el.scrollTop;
if (el.nodeName == 'BODY')
break;
} while (el = el.parentNode);
return [curX, curY];
}