Php聊天功能.滚动到'帖子区域'

Php chatfunction. Scroll to the bottom of 'post area'

本文关键字:区域 聊天 功能 Php 滚动      更新时间:2023-09-26

我在制作一个简单的基于php的聊天函数时迷路了。我的问题是用户进入聊天页面后的焦点,因为我在帖子窗口中添加了滚动条,所以焦点在顶部,而我希望它在底部(以跟上最新帖子)。有什么快速解决方案吗?

邮编:

   <?php
   if (file_exists("mld.php")) {
   $file = fopen( "mld.php", "r" );
   echo fread( $file, filesize( "mld.php" ) );
   fclose( $file );
   }
   ?>

写入代码:

  <?php
  if ( isset( $_POST[ 'submit' ] ) ) {
    $com  = $_POST['txt'];
    If ($com === "") {
    echo "<font color=red><b>Please write something!</font></b>";
    die;
 }
    $time = gmdate("M d Y H:i:s");
    $com  = $_POST['txt'];
    $count = strlen($com);
    $com = stripslashes($com);
    $fp = $file = fopen( "mld.php", "a");
    fwrite($file, $time);
    fwrite($file, $com);
    fwrite($file, $count);
    fclose($fp);
   echo '<script type="text/javascript">window.location ="";</script>';
  }
  ?>

js:

  function insertText(elemID, text)
  {
    var elem = document.getElementById(elemID);
    elem.innerHTML += text;
  }

除了加载/重新加载页面后滚动条会自动位于顶部之外,它还能按预期工作。如有任何关于此代码的帮助,我们将不胜感激。

只需使用一些javascript向下滚动即可,非常简单。

    function scrollbot(){
         var d = //select the element you want to scroll here.
                if(d.scrollHeight > d.clientHeight) {
                  d.scrollTop = d.scrollHeight - d.clientHeight;
                }
            }

只要在代码中需要滚动的任何地方运行此函数