简体   繁体   English

我似乎无法让jQuery新消息滚动在我的代码中工作

[英]I can't seem to get jquery new message scroll to work in my code

I have looked around at other answers to this but I can't see what i am doing wrong/how I can relate the answers to my question- I apologise in advance if there is a very similar answer out there that I have not used! 我到处都在寻找其他答案,但是我看不到自己在做错什么/如何将答案与我的问题联系起来-如果有一个非常相似的答案我没有用过,我会提前道歉!

So basically I have a basic page and a button that you click to bring up the message bar on the side. 因此,基本上,我有一个基本页面和一个按钮,您可以单击它们以调出侧面的消息栏。 This bar works fine(I have tried to simplify it on code pen) but I can't get the scrollable messages section to scroll down to the bottom automatically when I enter a new message, instead I have to scroll down to see my new message. 此栏可以正常工作(我尝试在代码笔上进行简化),但是当我输入新消息时,我无法使可滚动消息部分自动向下滚动到底部,而是必须向下滚动才能看到我的新消息。

I have this part: 我有这部分:

function displayChatMessage(name, text) {
  $('<li/>').text(text).prepend($('<span/>').text(name+':')).appendTo($('#messagesDiv'));
  $('#messagesDiv')[0].scrollTop = $('#messagesDiv')[0].scrollHeight;

}

Which is from the firebase tutorial. 这是从firebase教程。 In their messages though the part where the messages is written keeps moving down off the page. 在他们的消息中,尽管消息的编写部分不断向下移动。 I have tried fiddling around this and with my div configuration but I end seem to end up generally with all of my text box off the screen. 我已经尝试使用div配置来解决这个问题,但是最终我似乎将所有文本框都从屏幕上移开了。

Any thoughts much appreciated. 任何想法表示赞赏。

Below is a codepen link I hope will work as I can't get the right parts to work in jsfiddle. 下面是一个Codepen链接,我希望它能正常工作,因为我无法在jsfiddle中获得合适的部件。

Ok. 好。 The problem is you're trying to set scroll height of the wrong element. 问题是您试图设置错误元素的滚动高度。

Try this instead $('.messages').scrollTop($('.messages').prop('scrollHeight')); 试试这个代替$('.messages').scrollTop($('.messages').prop('scrollHeight'));

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

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