Finding the maximum page scroll position

I made the page body 200% high so that it fits twice on the screen. Using javascript, I make it scroll up or down when scrolling. To do this, I need to find out the lowest page scroll point in any browser or screen size so that it stops when it gets there.

No jQuery, please.
Thanks.

My code: (it is still compiling, so it takes a bit of work)

function getScrollXY() { var x = 0, y = 0; if( typeof( window.pageYOffset ) == 'number' ) { // Netscape x = window.pageXOffset; y = window.pageYOffset; } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) { // DOM x = document.body.scrollLeft; y = document.body.scrollTop; } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) { // IE6 standards compliant mode x = document.documentElement.scrollLeft; y = document.documentElement.scrollTop; } return [x, y]; } function scrollup() { if (xy[1] > 0) { window.scrollBy(0,-100); setTimeout(scrollup,200); } else { null; } } function scrolldown() { if (xy[1] < ) { window.scrollBy(0,100); setTimeout(scrolldown,200); } else { null; } } function dothescroll() { var xy = getScrollXY(); var y = xy[1]; setTimeout(function(){ if (xy[1] > y) { scrollup(); } else { scrolldown(); } },200); } 
+8
javascript scroll
source share
4 answers

This is a cross browser compatible version:

 var limit = Math.max( document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight ); 
+18
source share
 var limit = document.body.offsetHeight - window.innerHeight; // document.body.offsetHeight = computed height of the <body> // window.innerHeight = available vertical space in the window 

Compare xy[1] < limit

Note. You may need to increase the margin and / or body fill value. You can also try using clientHeight instead of offsetHeight .

+6
source share

window.scrollTo (0, document.body.scrollHeight);

it will work.

+1
source share

While this is not part of any specification, you can try window.scrollMaxY .

Returns the maximum number of pixels that can be scrolled vertically. https://developer.mozilla.org/docs/Web/API/Window/scrollMaxY


Refund if unavailable:

 var scrollMaxY = window.scrollMaxY || (document.documentElement.scrollHeight - document.documentElement.clientHeight) 

Note. documentElement not always a scroll element (in some browsers, body used instead). This is a new scrollingElement property that returns a reference to the element that scrolls the document. He is indicated, but still a working draft.

0
source share

All Articles