简体   繁体   English

div上的Javascript自动滚动

[英]Javascript auto scroll on div

I would like to scroll at the end of the div so in my case I can see the last message of chat. 我想滚动到div的末尾,这样我就可以看到聊天的最后一条消息。 Here's what I have but it doesn't work, the scroll is actually on top of the div. 这是我所拥有的,但是它不起作用,滚动实际上位于div的顶部。

<script type="text/javascript">
var divChat = document.getElementById('chat');
divChat.scrollTop = divChat.scrollHeight;
</script>
<div id="chat" style="border-style:groove;overflow-y: auto; height:50%;">
<?php 
    $response = $bdd->query('SELECT * FROM minichat');
    while ($rsp = $response->fetch())
    {
        echo '['.$rsp['date_message'].']'." <strong>".htmlspecialchars($rsp['pseudo'])
        ."</strong>: ".htmlspecialchars($rsp['message'])."<br>";
    }
?>
</div>

Thanks by advance for any help, 在此先感谢您的帮助,

This should work fine: 这应该工作正常:

<div id="chat" style="border-style:groove;overflow-y: auto; height:50%;">
<?php 
    $response = $bdd->query('SELECT * FROM minichat');
    while ($rsp = $response->fetch())
    {
      echo '['.$rsp['date_message'].']'."         <strong>".htmlspecialchars($rsp['pseudo'])
    ."</strong>: ".htmlspecialchars($rsp['message'])."<br>";
    }
?>
</div>
...
<script type="text/javascript">
  var divChat = document.getElementById('chat');
  divChat.scrollTop = divChat.scrollHeight;
</script>
</body>

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

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