gpt4 book ai didi

javascript - 聊天文本自动向下滚动并从数据库加载信息

转载 作者:太空宇宙 更新时间:2023-11-04 12:01:31 25 4
gpt4 key购买 nike

我希望能够让聊天文本始终滚动到聊天底部。

我看过很多教程并尝试了很多来自堆栈溢出的例子,但没有一个奏效。

目前,当行数过多时,我的聊天文本会一直超出 textarea,即使这样我也有 overflow:hidden

我还希望文本区域只加载数据库中的最后 50 个条目,这样当用户加入时,他们就不必加载聊天中所说的所有内容。

知道如何做这两件事吗?

首页

<?php
session_start();
?>

<!DOCTYPE html>
<html>
<head>
<div id="Holder">
<div id="Header"></div>
<link href="../Style/Style.css" type="text/css" rel="stylesheet"/>

<script type="text/javascript" src="../Js/jquery.js"></script>
<title>Chat Home</title>
<script type="text/javascript">
$(document).ready(function(){

$("#ChatText").keyup(function(e){
//When we press Enter do
if(e.keyCode==13){
var ChatText = $("#ChatText").val();
$.ajax({
type:'POST',
url:'InsertMessage.php',
data:{ChatText:ChatText},
success:function(){
$("#ChatMessages").load("DisplayMessages.php");
$("#ChatText").val("");
}
})
}
});
setInterval(function(){//refreshes every 1500ms
$("#ChatMessages").load("DisplayMessages.php");
},1500);
$("#ChatMessages").load("DisplayMessages.php");

</script>
</head>
<body>
<h2>Welcome <span style="color: green"><?php echo $_SESSION['UserName'] ; ?></span> </h2>
</br></br>

<div id="ChatBig">
<div id="ChatMessages">
</div>
<input type="text" id="ChatText" name="ChatText"></textarea>
</div>
<div id="Footer"></div>
</div>
</body>

</html>

CSS

#ChatBig {
width: 60%;
height: 600px;
margin: auto;
background-color: white;
overflow:hidden;
resize:none;

}
#ChatMessages{
width: 100%;
height: 548px;
border: 1px solid #000;
font-size: 16px;
font-weight: normal;
overflow:hidden;
resize:none;
}
#ChatText{
width: 100%;
height: 50px;
border: 1px solid #000;
overflow:hidden;
resize:none;
}
#ChatText:focus{outline: none;}

body {
background-color: lightgray;
}
#Holder {
width: 100%;
}
.UserNameS{
color: #7CB9E8;
}
.UserNameS:hover{text-decoration:underline; cursor: pointer;}

网站的外观图片

http://gyazo.com/fe71d7d9699b9784fda5ba1c862fad53

最佳答案

如果你不想滚动,也许你可以使用 position: absolute;position: relative;,就像这个例子一样

#ChatBig {
width: 60%;
height: 600px;
margin: auto;
background-color: white;
overflow:hidden;
resize:none;
background: red;
position: relative;
}
#ChatMessages{
width: 100%;
min-height: 498px;
border: 1px solid #000;
font-size: 16px;
font-weight: normal;
overflow:hidden;
resize:none;
position: absolute;
bottom: 50px;
}
#ChatText{
width: 100%;
height: 50px;
border: 1px solid #000;
overflow:hidden;
resize:none;
position: absolute;
bottom: 0;
}
#ChatText:focus{outline: none;}
<div id="ChatBig">
<div id="ChatMessages">
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
asd<br/>
qwe<br/>
</div>
<input type="text" id="ChatText" name="ChatText">
</div>

你需要让containerposition: relative;,然后让child为absolute

首先,ChatBig600pxChatText是50px,所以把ChatMessages改成

min-height: 498px; // don't use height
position: absolute;
bottom: 50px; // because ChatText is 50px

然后对于 ChatText,将其设置为

position: absolute;
bottom: 0;

在那里,你会在底部看到最新的ChatMessages

注意:qwe只是用来标识最后一行

关于javascript - 聊天文本自动向下滚动并从数据库加载信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29744608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com