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?
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.
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 .
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With