jQuery scroll an element to the bottom

2019-04-26 20:22发布

All I can find on the web tells me how to scroll the page to the bottom or to an element.

How is it possible to scroll a specific div which has the style "overflow:auto" to the bottom using jQuery?

标签: jquery scroll
3条回答
手持菜刀,她持情操
2楼-- · 2019-04-26 20:54

demo

var scr = $('#box')[0].scrollHeight;
$('#box').animate({scrollTop: scr},2000);
查看更多
走好不送
3楼-- · 2019-04-26 20:58

You can scroll the top, subtract the window height and add the element height. Your scroll should arrive right at the bottom of the element:

$( 'body' ).animate( {
    scrollTop: $( '#element' + currentElementId ).offset().top 
    + $( '#element' + currentElementId ).height() 
    - $( window ).height()
}, 1000 );
查看更多
▲ chillily
4楼-- · 2019-04-26 21:18

Yes:

$("#container").scrollTop($("#elementToScrollTo").position().top);​

If you want to go smoothly:

$("#container").animate({
    scrollTop: $("#elementToScrollTo").position().top
}, 1000);

Here, have a fiddle: http://jsfiddle.net/adrianonantua/nxHE8/

查看更多
登录 后发表回答