如何让用户能够在受限功能后自动向下滚动

Posted

技术标签:

【中文标题】如何让用户能够在受限功能后自动向下滚动【英文标题】:How to give the user the ability to scroll after a restricted functions to auto scroll down 【发布时间】:2021-08-29 16:31:31 【问题描述】:

我正在制作一个基本的聊天网站,我使用引导程序中的一些预定义类作为我的模态表单作为聊天表单与我自己的一些 CSS 相结合,问题是每次用户打开聊天表单模态时,显示消息的表单内的输出(具有溢出的 CSS 属性:滚动;)总是开始显示最旧的消息,并且用户必须向下滚动才能显示最新消息,我从其中一个中找出了一个脚本此处使输出显示最新消息的问题,但现在我无法再滚动到顶部:)

这是 3 个表单输出的脚本

   

 <script>
    
    const messages0 = document.getElementById('output0');
    const messages1 = document.getElementById('output1');
    const messages2 = document.getElementById('output2');
    
    function getMessages() 
        // Prior to getting your messages.
      shouldScroll0 = messages0.scrollTop + messages0.clientHeight === messages0.scrollHeight;
      shouldScroll1 = messages1.scrollTop + messages1.clientHeight === messages1.scrollHeight;
      shouldScroll2 = messages2.scrollTop + messages2.clientHeight === messages2.scrollHeight;
      
      // After getting your messages.
      if (!shouldScroll0) 
        scrollToBottom(messages0);
       
      
      if (!shouldScroll1) 
        scrollToBottom(messages1);
      
      if (!shouldScroll2) 
        scrollToBottom(messages2);
      
    
    
    
    function scrollToBottom() 
      messages0.scrollTop = messages0.scrollHeight;
      messages1.scrollTop = messages1.scrollHeight;
      messages2.scrollTop = messages2.scrollHeight;
    
    
    scrollToBottom();
    
    setInterval(getMessages, 100);
    </script>
*here is the CSS of the output and variables inside of it 


<style>
.message-blue 
    position: relative;
    margin-left: 20px;
    margin-bottom: 10px;
    padding: 10px;
    background-color: #A8DDFD;
    width: 200px;
    height: auto;
    text-align: left;
    font: 400 .9em 'Open Sans', sans-serif;
    border: 1px solid #97C6E3;
    border-radius: 10px;


.message-orange 
    position: relative;
    margin-bottom: 10px;
    margin-left: calc(100% - 240px);
    padding: 10px;
    background-color: #f8e896;
    width: 200px;
    height: auto;
    text-align: left;
    font: 400 .9em 'Open Sans', sans-serif;
    border: 1px solid #dfd087;
    border-radius: 10px;


.message-content 
    padding: 0;
    margin: 0;


.message-timestamp-right 
    position: absolute;
    font-size: .85em;
    font-weight: 300;
    bottom: 5px;
    right: 5px;


.message-timestamp-left 
    position: absolute;
    font-size: .85em;
    font-weight: 300;
    bottom: 5px;
    left: 5px;


.message-blue:after 
    content: '';
    position: absolute;
    width: 0;
    height: 0;
    border-top: 15px solid #A8DDFD;
    border-left: 15px solid transparent;
    border-right: 15px solid transparent;
    top: 0;
    left: -15px;


.message-blue:before 
    content: '';
    position: absolute;
    width: 0;
    height: 0;
    border-top: 17px solid #97C6E3;
    border-left: 16px solid transparent;
    border-right: 16px solid transparent;
    top: -1px;
    left: -17px;


.message-orange:after 
    content: '';
    position: absolute;
    width: 0;
    height: 0;
    border-bottom: 15px solid #f8e896;
    border-left: 15px solid transparent;
    border-right: 15px solid transparent;
    bottom: 0;
    right: -15px;


.message-orange:before 
    content: '';
    position: absolute;
    width: 0;
    height: 0;
    border-bottom: 17px solid #dfd087;
    border-left: 16px solid transparent;
    border-right: 16px solid transparent;
    bottom: -1px;
    right: -17px;

.output
      border: 2px solid gray;
    background-color: #f1f1f1;
    border-radius: 5px;
    padding: 5px;
    margin: 10px 0;
    overflow: scroll;
  
    height: 250px;
  
</style>
and here is the html of 1 modal form output


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Home page chat application</title>
  <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.9/css/bootstrap-dialog.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.9/js/bootstrap-dialog.js"></script>

</head>
<body>
<div id="output0" class="output">
        <?php
---some php related to the db insertion of new messages----
?>
<?php 
include ("ConfigFile.php");
$name=$_SESSION["user"];
$seqo = "SELECT * FROM `adminposts` WHERE `Group_by` = 'Admin' AND `admin_id`='606' or `Group_by` = '606E'";
$resu=$link->query($seqo);

if ($resu->num_rows != 0)

  while($row = $resu->fetch_assoc())
    if($row["Group_by"]=='Admin')
      
      echo "<h3 style='color:red;'>".$row["name"]." --".$row["date"]." "."<br> :<div id='messagess'>".$row["msg"]."</div><br> *Announcement* <br> </h3>";
  
      if($row["name"]=="$name")
        echo "<div class='message-blue'>";
        echo "<h4>".$row["name"]."</h4>";
        echo "<div class = 'message-content' id='messagess'>".$row["msg"]."</div> <br>";
        echo "<div class='message-timestamp-left'>".$row["date"]."</div>";
        echo "</div>";

      if($row["Group_by"] !== 'Admin' && $row["name"]!=="$name")  echo "<div class='message-orange'>";
        echo "<h4>".$row["name"]."</h4>";
        echo "<p class = 'message-content'>".$row["msg"]."</p> <br>";
        echo "<div class='message-timestamp-right'>".$row["date"]."</div>";
        echo "</div>";
      
    
    else 
      echo "no messages were displayed yet.";
    
    $link->close();
    ?>
    </div>
   
          </div>

</body>

请帮助 :') 脚本中的 if 语句可能会给用户滚动选项,并且只有当用户提交消息或用户打开模式时,输出才会自动显示最新的不滚动输出

【问题讨论】:

【参考方案1】:

所以我发现正如我所说,一个小的 if/else 语句就可以解决问题,我测试了几个选项,这对我有用:

else 
$("#output2 #output1 #output0").on('scroll', function()
scrollToBottom=false;);

添加到 (!shouldScroll) 的 if 语句中

所以整个脚本应该是这样的:

<script>

const messages0 = document.getElementById('output0');
const messages1 = document.getElementById('output1');
const messages2 = document.getElementById('output2');

function getMessages() 
    // Prior to getting your messages.
  shouldScroll0 = messages0.scrollTop + messages0.clientHeight === messages0.scrollHeight;
  shouldScroll1 = messages1.scrollTop + messages1.clientHeight === messages1.scrollHeight;
  shouldScroll2 = messages2.scrollTop + messages2.clientHeight === messages2.scrollHeight;
  
  // After getting your messages.
  if (!shouldScroll0) 
    scrollToBottom(messages0.scrollTop);
   
  
  
  if (!shouldScroll1) 
    scrollToBottom(messages1.scrollTop);
  
  
   
  if (!shouldScroll2) 
    scrollToBottom(messages2.scrollTop);
  
  else 
    $("#output2 #output1 #output0").on('scroll', function()
    scrollToBottom=false;
);
  



function scrollToBottom() 
 messages0.scrollTop = messages0.scrollHeight;
  messages1.scrollTop = messages1.scrollHeight;
  messages2.scrollTop = messages2.scrollHeight;


scrollToBottom();

setInterval(getMessages, 100);

</script>

这样,当用户打开聊天表单时,它会自动显示最新消息,提交消息后,它也会显示最新消息,但如果用户滚动到顶部,它会被允许

【讨论】:

以上是关于如何让用户能够在受限功能后自动向下滚动的主要内容,如果未能解决你的问题,请参考以下文章

如何在 django 管理页面上创建锚点以自动从 url 向下滚动到内联或字段

UITableView 在 reloadData 后自动向下滚动

拖放作品 - 自动滚动不

Vuetify <v-menu> 中的自动对焦和键盘导航

如何自动向下滚动html页面?

iOS Safari 自动向下滚动网站