234

How do I use jquery to scroll right down to the bottom of an iframe or page?

Adam
  • 9,189
  • 15
  • 46
  • 62

9 Answers9

371

If you want a nice slow animation scroll, for any anchor with href="#bottom" this will scroll you to the bottom:

$("a[href='#bottom']").click(function() {
  $("html, body").animate({ scrollTop: $(document).height() }, "slow");
  return false;
});

Feel free to change the selector.

Eb946207
  • 748
  • 8
  • 26
Mark Ursino
  • 31,209
  • 11
  • 51
  • 83
216

scrollTop() returns the number of pixels that are hidden from view from the scrollable area, so giving it:

$(document).height()

will actually overshoot the bottom of the page. For the scroll to actually 'stop' at the bottom of the page, the current height of the browser window needs subtracting. This will allow the use of easing if required, so it becomes:

$('html, body').animate({ 
   scrollTop: $(document).height()-$(window).height()}, 
   1400, 
   "easeOutQuint"
);
Tom Bates
  • 3,212
  • 2
  • 20
  • 13
  • 30
    This should be the accepted answer, Tom is absolutely right, the accepted answer doesn't work nicely as the scroll will abruptly stop instead of processing the end of the easing. – Christian Jul 05 '11 at 06:02
  • 34
    Note: `easeOutQuint` requires a plugin, jQuery itself only has `linear` and `swing`. – newenglander May 31 '12 at 08:34
  • dude this is good stuff! thanks. even doing this with 'swing' instead of 'easeoutquint' shows noticeable differences'. – Jesse Head Feb 14 '13 at 06:52
  • Not sure if it's bad form, but I edited the accepted answer to add a pointer down to this answer. If it is, then someone can revert it. – xaxxon May 08 '15 at 02:55
  • This solution only works if the page is interpreted in a standard's compliant mode. For example, if you don't include the ` ` on Chrome, Chrome will return the exact same value for the window and document height, in which case `$(document).height()-$(window).height()` will always return 0. See here: http://stackoverflow.com/questions/12103208/jquery-window-height-is-returning-the-document-height – VKK Jan 06 '17 at 03:27
  • Which browser are you using? In each browser I've tried, supplying a number larger than the height just scrolls to the bottom, so there's no need to do all that maths: https://stackoverflow.com/a/44578849/1450294 – Michael Scheper Jun 16 '17 at 00:27
38

For example:

$('html, body').scrollTop($(document).height());
armadadrive
  • 963
  • 2
  • 11
  • 42
Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
15

After this thread didn't work out for me for my specific need (scrolling inside a particular element, in my case a textarea) I found this out in the great beyond, which could prove helpful to someone else reading this discussion:

Alternative on planetcloud

Since I already had a cached version of my jQuery object (the myPanel in the code below is the jQuery object), the code I added to my event handler was simply this:

myPanel.scrollTop(myPanel[0].scrollHeight - myPanel.height());

(thanks Ben)

Greg Pettit
  • 10,749
  • 5
  • 53
  • 72
4

A simple function that jumps (instantly scrolls) to the bottom of the whole page. It uses the built-in .scrollTop(). I haven’t tried to adapt this to work with individual page elements.

function jumpToPageBottom() {
    $('html, body').scrollTop( $(document).height() - $(window).height() );
}
Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
  • 2
    I don't know why, but this didn't work for me on Firefox 26.0, and would scroll to the top when this function was run. This problem was resolved by saying `$(document).scrollTop($(document).height());` – Jacklynn Jan 21 '14 at 20:03
4

If you don't care about animation, then you don't have to get the height of the element. At least in all the browsers I've tried, if you give scrollTop a number that's bigger than the maximum, it'll just scroll to the bottom. So give it the biggest number possible:

$(myScrollingElement).scrollTop(Number.MAX_SAFE_INTEGER);

If you want to scroll the page, rather than some element with a scrollbar, just make myScrollingElement equal to 'body, html'.

Since I need to do this in several places, I've written a quick and dirty jQuery function to make it more convenient, like this:

(function($) {
  $.fn.scrollToBottom = function() {
    return this.each(function (i, element) {
      $(element).scrollTop(Number.MAX_SAFE_INTEGER);
    });
  };
}(jQuery));

So I can do this when I append a buncho' stuff:

$(myScrollingElement).append(lotsOfHtml).scrollToBottom();
Michael Scheper
  • 6,514
  • 7
  • 63
  • 76
2

This one worked for me:

var elem = $('#box');
if (elem[0].scrollHeight - elem.scrollTop() == elem.outerHeight()) {
  // We're at the bottom.
}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
MountainRock
  • 594
  • 1
  • 9
  • 24
1
$('.block').scrollTop($('.block')[0].scrollHeight);

I use this code to scroll the chat when new messages arrive.

0

The scripts mentioned in previous answers, like:

$("body, html").animate({
    scrollTop: $(document).height()
}, 400)

or

$(window).scrollTop($(document).height());

will not work in Chrome and will be jumpy in Safari in case html tag in CSS has overflow: auto; property set. It took me nearly an hour to figure out.

Ilia Ross
  • 13,086
  • 11
  • 53
  • 88