24

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?

Zachary Wright
  • 20,532
  • 9
  • 40
  • 48
  • 6
    Elaborate math? Wouldn't it be [document height] - [scrollTop] - [window height]? – Steve Paulo Nov 15 '10 at 21:21
  • @StevePaulo You probably shouldn't use document height together with window height. That will cause cross-browser chaos. – Alex W Jan 20 '15 at 16:02

8 Answers8

37

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
Stephen
  • 18,089
  • 8
  • 57
  • 97
Nick Craver
  • 594,859
  • 130
  • 1,270
  • 1,139
  • Weird, in my case `$(window).scrollBottom();` returns a negative value: it's `-1 * $(window).scrollTop();` – Abdull Feb 15 '13 at 14:00
  • 2
    I found my problem: I ran into this problem: http://stackoverflow.com/q/12103208/923560 . My HTML file was missing a doctype declaration. The solution was to add ` ` at the very top of the HTML file. – Abdull Feb 15 '13 at 14:48
1

If you want to Scroll Down (Opposite of “scrollTop”), try to use the vertical position on the argument. Like this:

$(document).ready(function(){
    $("button").click(function(){
                           //position on pixeles
        $("body").scrollTop(1300);
    });
});

The top of the body, will be: $("body").scrollTop(0);

1

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

VKolev
  • 865
  • 11
  • 25
0
function scrollBottom()
{
    return $( window ).scrollTop() + $( window ).height();
}

// example usage
$( '#footer' ).css( 'top', scrollBottom() - $( '#footer' ).height() );
rushkeldon
  • 1,174
  • 1
  • 9
  • 12
0

As scrollTop's default behaviour scrolls to 0 when passing a negative value, I did this function that handles scrollTop and simulate a "scrollDown".

If anchor_pos is negative (so it's above my current scroll position), I subtract its value from current scroll position (as it has a negative value, I'm using + sign)

function jumpToAnchor(scrollable_div_selector, anchor_selector)
{
    anchor_pos = $(anchor_selector).position().top;

    //check if negative number
    if (anchor_pos < 0)
    {
        anchor_pos = $(scrollable_div_selector).scrollTop() + anchor_pos; //anchor_pos is negative, so i'm substracting it
    }

    $(scrollable_div_selector).scrollTop(anchor_pos);
}
SaganTheBest
  • 770
  • 13
  • 32
0

This is how I have calculated the distance from the bottom of the element to the bottom of the document:

$(document).height() - ($('#element').offset().top + $('#element').height());
Alex W
  • 33,401
  • 9
  • 92
  • 97
0

You could try scrollTo plugin and do something like:

$.scrollTo( document.height )
Fernando Rybka
  • 228
  • 1
  • 4
  • 13
0

try this:

return this[0].scrollHeight - this.scrollTop() - this.height();
Jirair He
  • 1
  • 1