简体   繁体   English

垂直对齐聊天页面底部并滚动页面

[英]vertical align to the bottom of chat page and page scroll

I am creating a chat application with AJAX and PHP. 我正在使用AJAX和PHP创建聊天应用程序。 I have successfully been able to get the chats aligned at the bottom of the div in which chats appear. 我已经成功地使聊天在显示聊天的div底部对齐。 When new messages are loaded, the messages are succcessfully added to the bottom of the div and using JS I am getting the scroll bar to the bottom to show new message. 加载新消息时,消息成功添加到div的底部,并使用JS,我将滚动条移至底部以显示新消息。 Just like below: 就像下面这样:

if(document.getElementById("oldmessages").scrollTopMax)
               {
                   if(document.getElementById("oldmessages").scrollTopMax > document.getElementById("oldmessages").clientHeight)
                   {
                   document.getElementById("oldmessages").scrollTop = document.getElementById("oldmessages").scrollTopMax;
                   }
               }

The problem, is that every time new chat is loaded by ajax request which constantly polls the server, at that time this code is fired and every time the scroll bar comes to bottom. 问题在于,每次由不断轮询服务器的ajax请求加载新聊天时,都会触发该代码,并且每次滚动条移至底部时。 Even if the user might be checking messages above the last messages, it still behaves like this , because I am not sure how to rectify this. 即使用户可能正在查看最后一条消息上方的消息,它仍然会像这样,因为我不确定如何解决此问题。 Is there a way to help solve this and not let the scroll bar scroll to bottom when user is checking messages above the last message? 有没有一种方法可以解决此问题,并且当用户检查最后一条消息上方的消息时,不要让滚动条滚动到底部?

Please contribute. 请贡献。

You can use element.scrollTop for get the position of scroll at the time of ajax is fire. 您可以使用element.scrollTop获取在ajax被触发时scrollposition if position of scroll bar is not at the bottom than set the position at old position . 如果scroll bar position不在bottom ,则将该位置设置在old position At first time you can set the position of scroll at the bottom . 第一次,您可以在bottom设置scroll position

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

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