为了账号安全,请及时绑定邮箱和手机立即绑定

Meteor.js 聊天 - scrollToBottom(); 仅适用于发送选项卡

Meteor.js 聊天 - scrollToBottom(); 仅适用于发送选项卡

守着星空守着你 2021-08-26 15:59:26
我正在关注rdickert 的这个教程chat-tutorial/chat-tutorial-part-4,特别是对于 scrollToBottom 函数。此功能的目的是在聊天中发送新消息时滚动到底部。但它只滚动发送它的人的标签。其他人(选项卡)发送的消息似乎不会为该聊天室中的其他当前用户触发此功能。我在打开两个标签的情况下对其进行了测试,一次在 Chrome 中,第二次我在 Chrome 中使用了第一个标签,在 Firefox 中使用了另一个标签。两种方式的结果相同。这是我存储 scrollToBottom-Function 的地方:进口/api/chat/chat.jsvar autoScrollingIsActive = false; scrollToBottom = function scrollToBottom (duration) {  var messageWindow = $(".chatWindow");  var scrollHeight = messageWindow.prop("scrollHeight");  messageWindow.stop().animate({scrollTop: scrollHeight}, duration || 0);};然后,我想使用ui-part上的功能进口/ui/components/chat/chat.js这是用户实际发送消息的事件。'keypress #text'(event) {  if (event.keyCode == 13) {    event.preventDefault();    const text = document.getElementById('text').value;    Meteor.call('chat.start', text, (error) => {      if (error){        alert(error.error);      }       else {        scrollToBottom(200);//Function triggers after msg is sent      }    });  }}我还尝试使用 onRendered-template 触发 scrollToBottom-Function,但结果相同 Template.message.onRendered(function () {   scrollToBottom(250); });谁能向我解释这种行为背后的原因是什么?谢谢!
查看完整描述

2 回答

?
大话西游666

TA贡献1817条经验 获得超14个赞

我发现了我的错误。我迭代了消息模板中的消息。不像指示那样在 chatWindow-Template 里面。


{{#each messages}}

{{> message}}

{{/each}}


查看完整回答
反对 回复 2021-08-26
?
森栏

TA贡献1810条经验 获得超5个赞

onRendered触发回调每一个新的消息,所以它应该工作。

问题可能来自滚动功能。尝试更换

messageWindow.stop().animate({scrollTop: scrollHeight}, duration || 0);

$('html').stop().animate({scrollTop: scrollHeight}, duration || 0);


查看完整回答
反对 回复 2021-08-26
  • 2 回答
  • 0 关注
  • 291 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信