getXY function
Posted by Anonymous
highlighted in
ERB
- ActionScript
- ActionScript 3
- Bash
- Brainfuck
- C
- C#
- C++
- CSS
- Diff
- Django/Jinja
- ERB
- Erlang
- Genshi
- Genshi Text
- Gettext Catalog
- HTML
- INI
- Java
- JavaScript
- Lua
- Mako
- Myghty
- MySQL
- Objective-C
- Perl
- PHP
- Plaintext
- Python
- Python 3
- Python console session
- Python Traceback
- RHTML
- Ruby
- Ruby irb session
- Smarty
- SQL
- VB.net
- XML
- XSLT
/**
* Returns the absolute X and Y positions of an object.
* @param {HTMLObject} obj HTML Object.
* @return {Object} Returns an accessor with .x and .y values.
*/
function getXY(obj)
{
var curleft = 0;
var curtop = obj.offsetHeight + 5;
var border;
if (obj.offsetParent)
{
do
{
// XXX: If the element is position: relative we have to add borderWidth
if (getStyle(obj, 'position') == 'relative')
{
if (border = _pub.getStyle(obj, 'border-top-width')) curtop += parseInt(border);
if (border = _pub.getStyle(obj, 'border-left-width')) curleft += parseInt(border);
}
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
}
while (obj = obj.offsetParent)
}
else if (obj.x)
{
curleft += obj.x;
curtop += obj.y;
}
return {'x': curleft, 'y': curtop};
}
/**
* Returns the specified computed style on an object.
* @param {HTMLObject} obj HTML Object
* @param {String} styleProp Property name.
* @return {Mixed} Computed style on object.
*/
function getStyle(obj, styleProp)
{
if (obj.currentStyle)
return obj.currentStyle[styleProp];
else if (window.getComputedStyle)
return document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleProp);
}