首先,我想說,除了上面的名字之外,我想不出更好的問題名稱。我的問題當然不僅僅涉及固定輸入。我將在這里詳細解釋我的問題。
基本資訊
所以,我正在制作一個實時聊天網站。該網站將成為body { display: grid }時尚。共有三列:第一列是您擁有的聊天串列,第二列是您選擇的當前聊天中的訊息,第三列顯示您選擇查看的聊天中的所有在線用戶。第一列和第三列各占頁面寬度的 20%。中間的(包含訊息的)將設定為auto寬度(這意味著它占用了剩余的可用空間,在這種情況下,它是頁面的 60%)。我對第一列和第三列沒有任何疑問。我的問題只圍繞第二個/中間一個。
此中間列將有一個聊天區域(所有訊息彈出的地方),底部有一個form帶有文本input和submit按鈕的 。目前,這個中間列不是display flexor display: grid。
我想要的是
當然,在出現大量訊息后div,包含所有這些訊息的overflow. 因此,如果需要,我已經添加overflow: auto到此div以使滾動條出現。由于 thediv和form它的下方由 a 分隔margin,因此滾動條只出現在它div的form下方,它不受影響。
我希望form在底部有 ,div但要設定所有訊息的overflow樣式,以便這些訊息出現在表單下。
如圖所示input,如果用戶進一步向上滾動,則訊息位于 下方。如果需要進一步解釋以進行澄清,我很樂意提供幫助。
我嘗試過的/問題
我嘗試添加position: fixedandbottom: 0以form使其保持在底部。這變成了
,這是期望的結果。到現在為止還挺好。
如果我發送訊息,我希望保留格式,所以我確定了哪個
(忽略用戶名JohnDoe和內容左側的空格;那是用戶組態檔所在的位置)。同樣,一切仍然有效。
Now, for the final test, I wanted to make sure that the message overflow works as intended (with a scrollbar), and if I scroll up, the messages appear to "go under" the form, like the image I showed above. After a few messages, it turns into
, which is intended. And if I were to scroll up,
.
But, that only works for a few messages. After more messages pile in, this doesn't stay.
is what it looks like after a few more messages. If it isn't visible at the first glance, here's an explanation: the input started getting pushed farther and farther down the page until I can only see half of it (hence, only half the input was visible in the image). If I were to keep sending messages, the input would fully go off the page.
My Code
如果您想清楚地檢查錯誤,這是我到目前為止的代碼:
@import url("https://fonts.googleapis.com/css2?family=Ubuntu&display=swap");
* {
font-family: -apple-system, BlinkMacSystemFont, Ubuntu, "Segoe UI", Roboto,
Oxygen, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
padding: 0;
margin: 0;
}
html,
body {
background-color: #36393e;
color: #fff;
height: 100vh;
}
body {
display: grid;
grid-template-columns: 20% auto 20%;
}
.chat-wrapper {
height: 100%;
}
.chat-box {
height: 90%;
overflow-y: auto;
overflow-x: hidden;
padding-right: 8px;
}
.chat-message {
width: 100%;
padding: 5px;
padding-left: 10px;
padding-right: 10px;
display: grid;
grid-template-columns: 65px auto;
}
.chat-message:hover {
background-color: #27292e;
}
.message-time {
height: 100%;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
opacity: 50%;
font-size: 12px;
margin-right: 5px;
margin-top: 0;
margin-bottom: 0;
}
.chat-message:hover > .message-time {
visibility: visible;
}
.message-user {
display: flex;
flex-direction: column;
}
.username {
margin-right: 15px;
color: #50c878;
font-weight: bold;
letter-spacing: 1px;
}
.user-time {
display: flex;
flex-direction: row;
align-items: center;
margin-bottom: 10px;
}
.form-wrapper {
height: 100%;
position: fixed;
width: 60%;
}
.message-form {
padding: 0;
display: flex;
flex-direction: row;
justify-content: center;
background-color: #40444d;
border-radius: 5px;
flex-grow: 2;
height: 40px;
}
.message {
height: 30px;
padding: 5px;
padding-left: 10px;
outline: none;
border: none;
border-radius: 5px;
font-size: 16px;
color: #fff;
flex-grow: 2;
background-color: transparent;
}
.send {
text-align: center;
height: 40px;
min-width: 50px;
border: none;
border-radius: 5px;
margin: 0;
font-size: 16px;
background-color: transparent;
color: #fff;
opacity: 50%;
transition: 0.3s;
}
.send:hover {
cursor: pointer;
opacity: 75%;
}
::-webkit-scrollbar {
width: 5px;
}
::-webkit-scrollbar-track {
background: transparent;
}
::-webkit-scrollbar-thumb {
background: #121214;
border-radius: 10px;
}
<!--
I have premade messages for you to see what the format would look like. If you would like to add more messages, please use the format below (add this code into #chat-box) :
<div >
<div></div>
<div >
<div >
<span id="username" data-username="JohnDoe" >Username</span>
<span >12:00 PM</span>
</div>
<span >Message content</span>
</div>
</div>
It turns out that the username will appear in every message, even when not needed. This is prevented by JavaScript, which isn't added here.
-->
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>Chat</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="chat-list-wrapper"></div>
<div class="chat-wrapper">
<div id="chat-box" class="chat-box">
<div class="chat-message">
<div></div>
<div class="message-user">
<div class="user-time">
<span id="username" data-username="JohnDoe" class="username">JohnDoe</span>
<span class="message-time">1:35 PM</span>
</div>
<span class="message-content">Testing.</span>
</div>
</div>
</div>
<div class="form-wrapper">
<form id="message-form" class="message-form">
<input type="text" name="message" placeholder="Send a message..." class="message" id="message" />
<button type="submit" name="send" class="send" id="send">
<!-- <i ></i> not working on SO-->
Send
</button>
</form>
</div>
</div>
<div class="status-wrapper"></div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
如果您想在這里澄清任何事情,請在下面發表評論。我很樂意提供幫助。
我已經在這個問題上停留了幾天。先感謝您。感謝所有幫助。
uj5u.com熱心網友回復:
將溢位移動到聊天包裝器
@import url("https://fonts.googleapis.com/css2?family=Ubuntu&display=swap");
* {
font-family: -apple-system, BlinkMacSystemFont, Ubuntu, "Segoe UI", Roboto, Oxygen, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
padding: 0;
margin: 0;
}
html,
body {
background-color: #36393e;
color: #fff;
height: 100vh;
}
body {
display: grid;
grid-template-columns: 20% auto 20%;
}
.chat-wrapper {
height: 100%;
overflow-y: auto;
overflow-x: hidden;
}
.chat-box {
height: 90%;
padding-right: 8px;
}
.chat-message {
width: 100%;
padding: 5px;
padding-left: 10px;
padding-right: 10px;
display: grid;
grid-template-columns: 65px auto;
}
.chat-message:hover {
background-color: #27292e;
}
.message-time {
height: 100%;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
opacity: 50%;
font-size: 12px;
margin-right: 5px;
margin-top: 0;
margin-bottom: 0;
}
.chat-message:hover>.message-time {
visibility: visible;
}
.message-user {
display: flex;
flex-direction: column;
}
.username {
margin-right: 15px;
color: #50c878;
font-weight: bold;
letter-spacing: 1px;
}
.user-time {
display: flex;
flex-direction: row;
align-items: center;
margin-bottom: 10px;
}
.form-wrapper {
height: 100%;
position: fixed;
width: 60%;
}
.message-form {
padding: 0;
display: flex;
flex-direction: row;
justify-content: center;
background-color: #40444d;
border-radius: 5px;
flex-grow: 2;
height: 40px;
}
.message {
height: 30px;
padding: 5px;
padding-left: 10px;
outline: none;
border: none;
border-radius: 5px;
font-size: 16px;
color: #fff;
flex-grow: 2;
background-color: transparent;
}
.send {
text-align: center;
height: 40px;
min-width: 50px;
border: none;
border-radius: 5px;
margin: 0;
font-size: 16px;
background-color: transparent;
color: #fff;
opacity: 50%;
transition: 0.3s;
}
.send:hover {
cursor: pointer;
opacity: 75%;
}
::-webkit-scrollbar {
width: 5px;
}
::-webkit-scrollbar-track {
background: transparent;
}
::-webkit-scrollbar-thumb {
background: #121214;
border-radius: 10px;
}
<div class="chat-list-wrapper"></div>
<div class="chat-wrapper">
<div id="chat-box" class="chat-box">
<div class="chat-message">
<div></div>
<div class="message-user">
<div class="user-time">
<span id="username" data-username="JohnDoe" class="username">JohnDoe</span>
<span class="message-time">1:35 PM</span>
</div>
<span class="message-content">Testing.</span>
</div>
</div>
<div class="chat-message">
<div></div>
<div class="message-user">
<div class="user-time">
<span id="username" data-username="JohnDoe" class="username">JohnDoe</span>
<span class="message-time">1:35 PM</span>
</div>
<span class="message-content">Testing.</span>
</div>
</div>
<div class="chat-message">
<div></div>
<div class="message-user">
<div class="user-time">
<span id="username" data-username="JohnDoe" class="username">JohnDoe</span>
<span class="message-time">1:35 PM</span>
</div>
<span class="message-content">Testing.</span>
</div>
</div>
<div class="chat-message">
<div></div>
<div class="message-user">
<div class="user-time">
<span id="username" data-username="JohnDoe" class="username">JohnDoe</span>
<span class="message-time">1:35 PM</span>
</div>
<span class="message-content">Testing.</span>
</div>
</div>
<div class="chat-message">
<div></div>
<div class="message-user">
<div class="user-time">
<span id="username" data-username="JohnDoe" class="username">JohnDoe</span>
<span class="message-time">1:35 PM</span>
</div>
<span class="message-content">Testing.</span>
</div>
</div>
<div class="chat-message">
<div></div>
<div class="message-user">
<div class="user-time">
<span id="username" data-username="JohnDoe" class="username">JohnDoe</span>
<span class="message-time">1:35 PM</span>
</div>
<span class="message-content">Testing.</span>
</div>
</div>
<div class="chat-message">
<div></div>
<div class="message-user">
<div class="user-time">
<span id="username" data-username="JohnDoe" class="username">JohnDoe</span>
<span class="message-time">1:35 PM</span>
</div>
<span class="message-content">Testing.</span>
</div>
</div>
<div class="chat-message">
<div></div>
<div class="message-user">
<div class="user-time">
<span id="username" data-username="JohnDoe" class="username">JohnDoe</span>
<span class="message-time">1:35 PM</span>
</div>
<span class="message-content">Testing.</span>
</div>
</div>
<div class="chat-message">
<div></div>
<div class="message-user">
<div class="user-time">
<span id="username" data-username="JohnDoe" class="username">JohnDoe</span>
<span class="message-time">1:35 PM</span>
</div>
<span class="message-content">Testing.</span>
</div>
</div>
<div class="chat-message">
<div></div>
<div class="message-user">
<div class="user-time">
<span id="username" data-username="JohnDoe" class="username">JohnDoe</span>
<span class="message-time">1:35 PM</span>
</div>
<span class="message-content">Testing.</span>
</div>
</div>
<div class="chat-message">
<div></div>
<div class="message-user">
<div class="user-time">
<span id="username" data-username="JohnDoe" class="username">JohnDoe</span>
<span class="message-time">1:35 PM</span>
</div>
<span class="message-content">Testing.</span>
</div>
</div>
<div class="chat-message">
<div></div>
<div class="message-user">
<div class="user-time">
<span id="username" data-username="JohnDoe" class="username">JohnDoe</span>
<span class="message-time">1:35 PM</span>
</div>
<span class="message-content">Testing.</span>
</div>
</div>
</div>
<div class="form-wrapper">
<form id="message-form" class="message-form">
<input type="text" name="message" placeholder="Send a message..." class="message" id="message" />
<button type="submit" name="send" class="send" id="send">
<!-- <i ></i> not working on SO-->
Send
</button>
</form>
</div>
</div>
<div class="status-wrapper"></div>
uj5u.com熱心網友回復:
我認為,如果您洗掉了表單包裝器的 100% 高度(它似乎并不重要)并添加了一個底部:0 或一些間距(如 10 像素),它會起作用。
.form-wrapper {
position: fixed;
bottom: 0;
width: 60%;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/406169.html
標籤:
