简体   繁体   English

如何让我的溢出div滚动始终在底部?

[英]How to let my overflow div scroll always at bottom?

I have used 我用过

$('.chatroom-upper-container').scrollTop($('.chatroom-upper-container')[0].scrollHeight);

it only work when i send a new message the scroll will update to bottom but how do when load the page the div scroll will always go down? 它仅在我发送新消息时才会起作用,滚动将更新到底部,但是如何在加载页面时div滚动总是会下降?

https://jsfiddle.net/729jz8me/ https://jsfiddle.net/729jz8me/

Everthing is work fine in JsFiddle 在JsFiddle中,Everthing工作得很好

but in my page the scroll wont go to bottom when loaded the page here is my page 但在我的页面中,滚动不会在加载页面时到达底部这里是我的页面 在此输入图像描述

the scroll will go down when i send a new message so how do i make the page loaded the scroll will go to bottom? 当我发送新消息时,滚动会下降,所以我如何使页面加载滚动将到底?

EDIT Maybe i didn't state the question very clearly. 编辑也许我没有说清楚问题。 So what i want is when i open the page the scroll will be at the bottom. 所以我想要的是当我打开页面时,滚动将位于底部。 Now I can go to bottom of the page when i send new message but when i close it reopen again the scroll of the page will be top again. 现在我可以在发送新消息时转到页面底部但是当我关闭它时再次重新打开页面的滚动将再次排在最前面。 I no sure why my code didn't work on page only work in JsFiddle 我不确定为什么我的代码不能在JsFiddle的页面上工作

Can you please try following jQuery code :- 你可以试试下面的jQuery代码: -

$(document).ready(function(){
   setTimeout(function(){
      $('.chatroom-upper-container').scrollTop($('.chatroom-upper-container')[0].scrollHeight);
   }, 500);
});

It may help you. 它可能会帮助你。

We can combine my idea with yours: 我们可以将您的想法与您的想法结合起来

  • Start the list at the bottom with your code 使用您的代码在底部开始列表

  • Create a button to trigger an event, when the user click the button, the scroll get to the bottom. 创建一个触发事件的按钮,当用户单击按钮时,滚动到达底部。

So, thus when we started the page the first time, the list will be at the bottom, then when you send a message the list will be in bottom too. 因此,当我们第一次启动页面时,列表将位于底部,然后当您发送消息时,列表也将位于底部。

$(document).ready(function(){
   //When page is loaded the first time
   $('.chatroom-upper-container').scrollTop($('.chatroom-upper-container')[0].scrollHeight);
   //Thus when page is loaded, the list started at the top 
   $('.YourSendButton').click(function(){
       $('.chatroom-upper-container').scrollTop($('.chatroom-upper-container')[0].scrollHeight);
   });  
});

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

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