Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding the maximum scroll position of a page

Tags:

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); } 
like image 844
Neil Philip Whitehead Avatar asked Jul 16 '13 23:07

Neil Philip Whitehead


People also ask

How is scroll position calculated?

To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.

How do I find my scroll length?

To get the height of the scroll bar the offsetHeight of div is subtracted from the clientHeight of div. OffsetHeight = Height of an element + Scrollbar Height. ClientHeight = Height of an element. Height of scrollbar = offsetHeight – clientHeight.

How do you get max scrollLeft?

To get the max value of scrollLeft with JavaScript, we can use the scrollWidth and clientWidth propeties. const maxScrollLeft = element. scrollWidth - element.

How do you find the scroll height of an element?

We use document. getElementById() to get the desired element of DOM, and then we apply scrollHeight , which will return the scroll height of the element. We then print the value returned by scrollHeight on the console.


2 Answers

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 ); 
like image 75
Neil Philip Whitehead Avatar answered Sep 18 '22 10:09

Neil Philip Whitehead


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.

like image 37
yckart Avatar answered Sep 22 '22 10:09

yckart