28

I have made the body of the page 200% tall so that it fits on a screen twice. Using javascript I am making it keep scrolling to the top or bottom when you scroll. For this, I need to find out the lowest scroll point of the page on any browser or screen size so that it stops when it gets there.

No JQuery please.
Thank you.

My code: (it is still being put together so needs 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);
}
frederick99
  • 1,033
  • 11
  • 18

5 Answers5

47

This is the cross browser compatible version:

var limit = Math.max( document.body.scrollHeight, document.body.offsetHeight, 
                   document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight );
  • 5
    if you then minus limit by window.innerHeight that is the maximum scroll position.x – Neil Philip Whitehead Jul 17 '13 at 11:38
  • 15
    Not so far the truth : var limit = Math.max( document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight) - window.innerHeight; – Jordan Apr 01 '15 at 19:02
  • On Mac Chrome, if there is a x-axis scroll bar, this and you set `window.scrollY` what Jordan suggests, this will be a few pixels from the true bottom of the page – theicfire Oct 22 '19 at 20:15
  • You are an absolute life saver. Been looking far and wide for a solution. – AlanSTACK Apr 17 '22 at 03:21
27

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

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


Fallback if unavailable:

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

Note, documentElement isn't always the scrolling element (some browsers use body instead). The solution is the new scrollingElement property, which returns a reference to the Element that scrolls the document. It is specified, but still a working draft.

yckart
  • 32,460
  • 9
  • 122
  • 129
17
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 might need to increase the value by margin and/or padding of the body. You can also try using clientHeight instead of offsetHeight.

alutom
  • 221
  • 1
  • 4
0

My solution based on the solutions above and what I use in 2022.

AKA scrollMaxY

const scrollMaxValue = () => {
  const body = document.body;
  const html = document.documentElement;

  const documentHeight = Math.max(
    body.scrollHeight,
    body.offsetHeight,
    html.clientHeight,
    html.scrollHeight,
    html.offsetHeight
  );

  const windowHeight = window.innerHeight;

  return documentHeight - windowHeight;
};
scrollMaxValue()
the-teacher
  • 589
  • 8
  • 19
-1

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

this will work..