简体   繁体   English

在页面加载时滚动div顶部,然后将滚动条设置为正常工作

[英]Scroll div top on page load then set scroll to work normally

I have a chat system on my site and defaulted the message div position to always display the last message on page load. 我的网站上有一个聊天系统,默认将消息div位置设置为始终在页面加载时显示最后一条消息。 I accomplished this by using the following line of code: 我通过使用以下代码行完成了此操作:

  msgDiv = document.getElementById('message_row_large');
  msgDiv.scrollTop = msgDiv.scrollHeight;

However, this code sets the scroll position to be equal to the div height at all times, which doesn't allow users to scroll up and see other messages. 但是,此代码始终将滚动位置设置为等于div高度,这不允许用户向上滚动并查看其他消息。

I need to re-enable scroll to its default functionality after the page loads. 页面加载后,我需要重新启用滚动至其默认功能。 ANy help is welcome. 欢迎任何帮助。

Thank you! 谢谢!

PS I am using ajax to load chat messages. PS我正在使用ajax加载聊天消息。 When user clicks on a name on the left hand panel, the chat between him/her and the other person loads on the right hand panel. 当用户单击左侧面板上的名称时,他/她与另一个人之间的聊天会加载到右侧面板上。

Try 尝试

    $display = $('#message_row_large');
    $display.animate({scrollTop: $display[0].scrollHeight }, 'fast');

Working Fiddle: 工作小提琴:

https://jsfiddle.net/cshanno/3bo48dxj/1/ https://jsfiddle.net/cshanno/3bo48dxj/1/

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

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