Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// e.g. scrollToTarget(document.getElementById('foo'));
function offset(target){
var left = 0,
top = 0;
while (target && target !== document.body){
if (target.offsetLeft){
left += target.offsetLeft;
top += target.offsetTop;
}
target = target.offsetParent;
}
return {left:left, top:top};
}
function scrollToTarget(target){
var pos = offset(target);
window.scrollTo(pos.left, pos.top);
return pos;
}
// e.g. scrollToY(document.getElementById('foo'));
function offsetY(target){
var top = 0;
while (target && target !== document.body){
if (target.offsetTop){
top += target.offsetTop;
}
target = target.offsetParent;
}
return top;
}
function scrollToY(target){
var top = offsetY(target);
window.scrollTo(window.scrollX, top);
return top;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment