Last active
February 12, 2016 20:08
-
-
Save hugoduraes/7ee8856c34005e86c09b 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
// document.body.scrollTop alone should do the job but that actually works only in case of Chrome. | |
// With IE and Firefox it also works sometimes (seemingly with very simple pages where you have | |
// only a <pre> or something like that) but I don't know when. This hack seems to work always. | |
var scrollTop = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop; | |
// Grodriguez's fix for scrollHeight: | |
// accounting for cases where html/body are set to height:100% | |
var scrollHeight = (document.documentElement && document.documentElement.scrollHeight) || document.body.scrollHeight; | |
// >= is needed because if the horizontal scrollbar is visible then window.innerHeight includes | |
// it and in that case the left side of the equation is somewhat greater. | |
var scrolledToBottom = (scrollTop + window.innerHeight) >= scrollHeight; | |
// As a bonus: how to scroll to the bottom programmatically by keeping the horizontal scrollpos: | |
// Since window.innerHeight includes the height of the horizontal scrollbar when it is visible | |
// the correct vertical scrollTop would be | |
// scrollHeight-window.innerHeight+sizeof(horizontal_scrollbar) | |
// Since we don't know the visibility/size of the horizontal scrollbar | |
// we scroll to scrollHeight that exceeds the value of the | |
// desired scrollTop but it seems to scroll to the bottom with all browsers | |
// without problems even when the horizontal scrollbar is visible. | |
var scrollLeft = (document.documentElement && document.documentElement.scrollLeft) || document.body.scrollLeft; | |
window.scrollTo(scrollLeft, scrollHeight); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment