Skip to content

Instantly share code, notes, and snippets.

@cheeaun
Created July 30, 2009 11:24
Show Gist options
  • Save cheeaun/158658 to your computer and use it in GitHub Desktop.
Save cheeaun/158658 to your computer and use it in GitHub Desktop.
Restoring the old getOffsets from 1.2.2
(function(){
Element.implement({
getOffsets: function(){
if (Browser.Engine.trident){
var bound = this.getBoundingClientRect(), html = this.getDocument().documentElement;
var isFixed = styleString(this, 'position') == 'fixed';
return {
x: bound.left + ((isFixed) ? 0 : html.scrollLeft) - html.clientLeft,
y: bound.top + ((isFixed) ? 0 : html.scrollTop) - html.clientTop
};
}
var element = this, position = {x: 0, y: 0};
if (isBody(this)) return position;
while (element && !isBody(element)){
position.x += element.offsetLeft;
position.y += element.offsetTop;
if (Browser.Engine.gecko){
if (!borderBox(element)){
position.x += leftBorder(element);
position.y += topBorder(element);
}
var parent = element.parentNode;
if (parent && styleString(parent, 'overflow') != 'visible'){
position.x += leftBorder(parent);
position.y += topBorder(parent);
}
} else if (element != this && Browser.Engine.webkit){
position.x += leftBorder(element);
position.y += topBorder(element);
}
element = element.offsetParent;
}
if (Browser.Engine.gecko && !borderBox(this)){
position.x -= leftBorder(this);
position.y -= topBorder(this);
}
return position;
}
});
var styleString = Element.getComputedStyle;
function styleNumber(element, style){
return styleString(element, style).toInt() || 0;
};
function borderBox(element){
return styleString(element, '-moz-box-sizing') == 'border-box';
};
function topBorder(element){
return styleNumber(element, 'border-top-width');
};
function leftBorder(element){
return styleNumber(element, 'border-left-width');
};
function isBody(element){
return (/^(?:body|html)$/i).test(element.tagName);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment