ChatGPT解决这个技术问题 Extra ChatGPT

jQuery Scroll to bottom of page/iframe

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


E
Eb946207

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.


This works in Firefox 3.5.7 but not in Chrome 4.0.295.0. The following works in Chrome: $('html, body').animate({scrollTop: $(document).height()}, 'slow');
@Tom, I can't spot the difference between your solution and Mark's !
@MuhammadGelbana check the edit date. It appears Mark updated his answer to include Tom's fix.
There's actually no need to get the element's height: stackoverflow.com/a/44578849/1450294
T
Tom Bates

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"
);

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.
Note: easeOutQuint requires a plugin, jQuery itself only has linear and swing.
dude this is good stuff! thanks. even doing this with 'swing' instead of 'easeoutquint' shows noticeable differences'.
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.
This solution only works if the page is interpreted in a standard's compliant mode. For example, if you don't include the <!DOCTYPE html> 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: stackoverflow.com/questions/12103208/…
a
armadadrive

For example:

$('html, body').scrollTop($(document).height());

I cannot seem to get that to work. Doesn't do anything at all.
@adam What version of jQuery are you using?
G
Greg Pettit

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)


Worked for me with a div: var d = $('#mydiv'); d.scrollTop (d[0].scrollHeight - d.height ());
Do you actually have to do the maths? stackoverflow.com/a/44578849/1450294
R
Rory O'Kane

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() );
}

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());
M
Michael Scheper

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();

B
Bo Persson

This one worked for me:

var elem = $('#box');
if (elem[0].scrollHeight - elem.scrollTop() == elem.outerHeight()) {
  // We're at the bottom.
}

This doesn't answer the question
А
Александр Лиссов
$('.block').scrollTop($('.block')[0].scrollHeight);

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


Please include some explanation for your code, especially when you copy from your own code base, using CSS classes that are not relevant for other people ;)
Take any block with a scroll bar;) or the entire document.
I
Ilia

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.