简体   繁体   English

无法修复聊天框(div)底部的滚动条

[英]Unable to fix the scroll bar at the bottom of chat box(div)

I'm working on chat application. 我正在研究聊天应用程序。 On loading my PHP file I want to fix the scroll bar at the bottom of div. 在加载我的PHP文件时,我想修复div底部的滚动条。 I have written the script to do this. 我已经写了脚本来做到这一点。 I'm accessing it using CSS Id but I'm unable to make it work. 我正在使用CSS Id访问它,但无法使其正常工作。

Code

//Css part
<style>
.msg-wgt-body {
 height: 300px;
 overflow-y: scroll;}
</style>

<script>
$(".1").scrollTop($('.1').height())
</script>

<div class="container" style="border: 1px solid lightgray;">
<div class="msg-wgt-header">
<a href="#">Kaps</a>
</div>
<div class="msg-wgt-body" id="1">
<table>
<?php
if (!empty($messages)) {
foreach ($messages as $message) {
$msg = htmlentities($message['message'], ENT_NOQUOTES);
$user_name = ucfirst($message['username']);   
$sent =  date("F j, Y, g:i a", strtotime($message['sent_on']));

echo <<<MSG
          <tr class="msg-row-container">
            <td>
              <div class="msg-row">
                <div class="avatar"></div>
                <div class="message">
       <span class="user-label"><a href="#" style="color: #6D84B4;">
    {$user_name}</a> <span class="msg-time">{$sent}</span></span><br/>{$msg}
          </div>
              </div>
            </td>
          </tr>
 MSG;

        }
      }

      else {
        echo '<span style="margin-left: 25px;">No chat messages available!
</span>';
      }


      ?>

    </table>
  </div>

You can do this using your CSS -Class as well. 您也可以使用CSS -Class做到这一点。

<script>
$(document).ready(function(){
        $(".msg-wgt-body").scrollTop($('.msg-wgt-body')[0].scrollHeight);

   return false;
});
</script>

Use this above code and add this at the bottom of your PHP file(You can add this in your JS file as well). 使用上面的代码,并将其添加到PHP文件的底部(也可以将其添加到JS文件中)。 Try this it should work. 试试这个,它应该工作。

I guest you want scroll down to the bottom when the chats loaded, right! 我邀请您,您希望聊天加载后向下滚动到底部,对!

Just replace this 只要更换这个

</style>
      <script> $(".1").scrollTop($('.1').height()) 
</script>

with

<script>
   $("#1").scrollTop($('#1')[0].scrollHeight);
</script>

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

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