简体   繁体   English

Jquery使div总是滚动到底部

[英]Jquery make div always scroll to bottom

I have a div that contains a table. 我有一个包含表格的div。 This table is dynamically updated, and is filling up with data as user is using our website. 此表已动态更新,并在用户使用我们的网站时填写数据。 Is it possible to force this div to always be scrolled to bottom? 是否可以强制此div始终滚动到底部? So that the user will always be able to see the bottom most table row that was added. 这样用户将始终能够看到添加的最底部的表行。 Thank you! 谢谢!

That would be something like 那会是这样的

...onRowAdded = function() {
    $('#myTable').animate({scrollTop: $('#myTable').attr('scrollHeight')});
};

Note, if the rows are added very often, you would want to use setTimeout with blocking variable check at the beginning to prevent scrolling down more often than twice per second or so. 请注意,如果经常添加行,您可能希望在开头使用setTimeout和阻塞变量检查,以防止每秒向下滚动超过两次左右。 This technique is discussed eg here 这里例如讨论该技术

UPDATE UPDATE

Starting with jQuery 1.6 it is better to use .prop() instead of .attr() (see comments to this question): 从jQuery 1.6开始,最好使用.prop()而不是.attr()(请参阅此问题的评论):

onRowAdded = function() {
    $('#myTable').animate({scrollTop: $('#myTable').prop('scrollHeight')});
};

Set the update element as position absolute and bottom:0 将update元素设置为absolute absolute和bottom:0

DEMO http://jsfiddle.net/Mdzmx/ 演示http://jsfiddle.net/Mdzmx/

Try something like it : 尝试类似的东西:

Create a anchor like <a id='bottomDiv'></a> and insert a javascript like this document.getElementById('bottomOfDiv').scrollIntoView(true); 创建一个像<a id='bottomDiv'></a>这样的锚点,并插入一个像这个document.getElementById('bottomOfDiv').scrollIntoView(true);

I made a example here http://jsfiddle.net/PTmpL/1/ 我在这里举了一个例子http://jsfiddle.net/PTmpL/1/

Follow this tutorial 请遵循本教程

http://jqueryfordesigners.com/fixed-floating-elements/ http://jqueryfordesigners.com/fixed-floating-elements/

but use the bottom css property to have the bottom most element, visible on the page 但是使用bottom css属性来获得最底部的元素,在页面上可见

element{ bottom:0; }

I'd recommend using the ScrollTo JQuery plugin . 我建议使用ScrollTo JQuery插件 Just use 只是用

element.scrollTo('max', {axis: 'y'});

or 要么

element.scrollTo('max', duration, {axis: 'y'});

This plugin has the added benifit of having a nice smooth scroll. 这个插件增加了一个很好的平滑滚动的好处。

我认为这可能是简单的解决方案!

element.scrollTo(element.get(0).scrollHeight);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM