【发布时间】:2015-12-22 15:31:45
【问题描述】:
我正在创建一个聊天,一切正常,当我单击“发送”按钮时它会向下滚动,但我希望它在文档准备好时一直向下滚动。我通过将滚动功能添加到 setInterval 来做到这一点,但问题是用户基本上无法向上滚动以查看以前的聊天消息,因为他每 0.1 秒向下滚动一次。我的代码是:
$(function () {
//$("#messages").scrollTop($("#messages").prop("scrollHeight")); Doesnt work at all
function updateChat(){
$("#messages").load('chat/ajaxLoad.php');
//$("#messages").scrollTop($("#messages").prop("scrollHeight")); This works but the user cannot scroll up anymore
}
setInterval(function () {
updateChat();
}, 100);
$("#post").submit(function(){
$.post("chat/ajaxPost.php", $('#post').serialize(), function (data) {
$("#messages").append('<div>'+data+'</div>');
$("#messages").scrollTop($("#messages").prop("scrollHeight")); // This works but only when the user presses the send button
$("#text").val("");
});
return false;
});
});
【问题讨论】:
-
如果您有避免使用 $(document).ready() 的原因?根据我对您想要什么的理解(一旦所有内容加载完毕,它会滚动到底部),这正是您所需要的。
标签: javascript jquery scroll chat