Created
September 19, 2012 21:23
-
-
Save premasagar/3752365 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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