Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opposite of "scrollTop" in jQuery

Tags:

jQuery has a function called scrollTop which can be used to find the number of pixels hidden above the current page view.

I'm not really sure why, but there is no scrollBottom function which returns the number of pixels below the current page view.

Is there a jQuery plugin which adds this functionality? Or is it going to require some elaborate math with the window/document height and the scrollTop value?

like image 679
Zachary Wright Avatar asked Nov 15 '10 21:11

Zachary Wright


People also ask

What is jQuery scrollTop?

jQuery scrollTop() Method The scrollTop() method sets or returns the vertical scrollbar position for the selected elements. Tip: When the scrollbar is on the top, the position is 0. When used to return the position: This method returns the vertical position of the scrollbar for the FIRST matched element.

What does scrollTop do?

scrollTop property gets or sets the number of pixels that an element's content is scrolled vertically. An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content. When an element's content does not generate a vertical scrollbar, then its scrollTop value is 0 .


2 Answers

You could make a pretty simple plugin for this:

$.fn.scrollBottom = function() {    return $(document).height() - this.scrollTop() - this.height();  }; 

Then call it on whatever element you wanted, for example:

$(window).scrollBottom();  //how many pixels below current view $("#elem").scrollBottom(); //how many pixels below element 
like image 198
Nick Craver Avatar answered Oct 05 '22 03:10

Nick Craver


Perhaps this will help how to tell jquery to scroll to bottom, because there is really no opposite function. Same is the problem with scrollLeft - there is no scrollRight

like image 22
VKolev Avatar answered Oct 05 '22 03:10

VKolev