简体   繁体   English

如何让我的滚动条自动修复到div的底部?

[英]How would I make my scroll bar automatically fix itself to to the bottom of a div?

I am making a web chat application and I am in need of some help. 我正在制作一个网络聊天应用程序,我需要一些帮助。 I am appending some paragraphs to a div, and I have set overflow to auto. 我将一些段落附加到div,并将溢出设置为auto。 When the content of the div containing the chat is full, it creates a scroll bar but stays at the top of the div. 当包含聊天的div的内容已满时,它会创建一个滚动条,但仍保留在div的顶部。 I would like the scroll to be fixed to the bottom so that users don't need to scroll each time they receive a message. 我希望滚动固定在底部,这样用户每次收到消息时都不需要滚动。 What should I do? 我该怎么办?

Standard JavaScript solution 标准JavaScript解决方案

Use the scrollTop property. 使用scrollTop属性。 Checkout this demo . 看看这个演示

The JavaScript code JavaScript代码

 container.scrollTop = content.getClientRects()[0].height;

The HTML HTML

<div id="container">
  <div id="content">Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!
   </div>
 </div>

Here is the documentation on scrollTop and getClientRects 这是关于scrollTopgetClientRects的文档

Use scrollTop and scrollHeight (not reliable across all browsers), or an inner DIV instead of the latter so you could reliably get the actual scrollable height. 使用scrollTop和scrollHeight(在所有浏览器中不可靠),或者使用内部DIV而不是后者,这样您就可以可靠地获得实际的可滚动高度。 Don't forget to deduct the height of your container. 不要忘记扣除容器的高度。

Well, this seems like a cheap hack, but you can put an empty placeholder at the bottom and jump to it using window.location 好吧,这看起来像是一个廉价的黑客,但你可以在底部放一个空占位符并使用window.location跳转到它

$('<div>').attr('id','placeholder').appendTo('#content');
window.location.hash = '#placeholder';
$('#placeholder').remove();

DEMO DEMO

You can scroll to the last added element using jQuery. 您可以使用jQuery滚动到最后添加的元素。 http://jsfiddle.net/94LBc/1/ http://jsfiddle.net/94LBc/1/

$("#btn-addPara").click(function(e){
  $("#container").append('<p>New message</p>');

  $('#container').animate({
    scrollTop: $("#container").children(':last').offset().top
  }, 1000);
});

Another person asked a question just like this. 另一个人问了这样一个问题。 You can find their soultion, which worked, here: 你可以在这里找到他们的灵魂,

Make the scrollbar fixed to bottom even while appending content to it 即使向其添加内容,也要将滚动条固定到底部

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

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