简体   繁体   English

ScrollTop在动态生成的元素上不起作用

[英]ScrollTop doesn't work on dynamically generated elements

I am trying to create a chat system (like Facebook)'s where a user clicks the name of the online user and a chat box will appear. 我正在尝试创建一个聊天系统(如Facebook),在该系统中,用户单击在线用户的名称,然后将出现一个聊天框。 _chatbox is generated dynamically using javascript. _chatbox使用javascript动态生成。 It will scroll down to the last message. 它将向下滚动到最后一条消息。 The chat box and messages load/display successfully but the scroll down function is not working... why? 聊天框和消息加载/显示成功,但是向下滚动功能不起作用...为什么?

 //displays the popups. Displays based on the maximum number of popups that can be displayed on the current viewport width function display_popups() { //code for popup here } //creates markup for a new popup. Adds the id to popups array. function register_popup(id, name) { var element = '<div class="popup-box chat-popup" id="' + id + '">'; element = element + '<div class="popup-head">'; element = element + '<div class="popup-head-left">' + name + '</div>'; element = element + '<div class="popup-head-right"><a href="javascript:close_popup(\\'' + id + '\\');">&#10005;</a></div>'; element = element + '<div style="clear: both"></div></div><div class="popup-messages"></div></div>'; document.getElementsByTagName("body")[0].innerHTML = document.getElementsByTagName("body")[0].innerHTML + element; calculate_popups(); scrollDown(id); } //scroll down to the last message function scrollDown(id) { var messages = document.getElementById(id); messages.scrollTop = messages.scrollHeight; } //calculate the total number of popups suitable and then populate the toatal_popups variable. function calculate_popups() { //calculate popups here } 
 <div class="sidebar-name"> <a href="javascript:register_popup('ind', 'Indiana Pacers');"> <img width="30" height="30" src="img/ind.png" /> <span>Indiana Pacers</span> </a> </div> 

It works, but you seemed to select the wrong element. 它有效,但是您似乎选择了错误的元素。 I adapted your code so that i can demonstrate what is going on. 我修改了您的代码,以便可以演示发生了什么。 Your scrollable element here is the message container (class ".popup-messages"), not the popup itself. 这里的可滚动元素是消息容器(类“ .popup-messages”),而不是弹出窗口本身。

You need to adapt this code though, as i selected directly the .popup-messages element, but you might want to select the one that is specifically inside your popup. 但是,由于我直接选择了.popup-messages元素,因此您需要调整此代码,但您可能希望选择专门位于弹出窗口中的代码。

 //displays the popups. Displays based on the maximum number of popups that can be displayed on the current viewport width function display_popups() { //code for popup here } //creates markup for a new popup. Adds the id to popups array. function register_popup(id, name) { var element = '<div class="popup-box chat-popup" id="'+ id +'">'; element = element + '<div class="popup-head">'; element = element + '<div class="popup-head-left">'+ name +'</div>'; element = element + '<div class="popup-head-right"><a href="javascript:close_popup(\\''+ id +'\\');">&#10005;</a></div>'; element = element + '<div style="clear: both"></div></div><div class="popup-messages"><p>message</p><p>message</p><p>message</p><p>message</p><p>message</p><p>message</p><p>message</p><p>message</p><p>message</p><p>message</p><p>message</p><p>last message</p></div></div>'; document.getElementsByTagName("body")[0].innerHTML = document.getElementsByTagName("body")[0].innerHTML + element; calculate_popups(); scrollDown(id); } //scroll down to the last message function scrollDown(id) { var messages = document.getElementsByClassName('popup-messages')[0]; messages.scrollTop = messages.scrollHeight; } //calculate the total number of popups suitable and then populate the toatal_popups variable. function calculate_popups() { //calculate popups here } window.onload = function(){ register_popup('test', 'testName'); }; 
 .popup-messages { height: 100px; overflow-y: scroll; } 
 <div class="sidebar-name"> <a href="javascript:register_popup('ind', 'Indiana Pacers');"> <img width="30" height="30" src="img/ind.png" /> <span>Indiana Pacers</span> </a> </div> 

Why do you use innerHTML instead of creating an element? 为什么要使用innerHTML而不是创建元素?

Can you try creating the element with Javascript and adding it to DOM manually like: 您是否可以尝试使用Javascript创建元素并将其手动添加到DOM中,例如:

var popupBox = document.createElement("div");
// Set properties
var popupHead = document.createElement("div");
// Set properties
popupBox.appendChild(popupHead);
// etc. etc

// Then add it to DOM
document.getElementsByTagName("body")[0].appendChild(element)

// This should work now
calculate_popups();
scrollDown(id);

I think setting innerHtml and converting it to DOM is processed later than your function call. 我认为设置innerHtml并将其转换为DOM的处理要比函数调用晚。

Read up on creating elements with Javascript here: https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement 在此处阅读有关使用Javascript创建元素的信息: https : //developer.mozilla.org/zh-CN/docs/Web/API/Document/createElement

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

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