聊天窗口问题

我现在写一个聊天窗口,最后一行总是会被输入框挡住,怎么让滚动条滚到最下面的同时,同时让最后输入(或者是最后显示的历史消息永远在输入框之上,这样我们才能看得见所有内容)

https://img1.mukewang.com/5c304aec0001cef004410795.jpg

慕容3067478
浏览 420回答 1
1回答

一只斗牛犬

假设你的输入框是这样写的:<div class=input>&nbsp; &nbsp; <input/></div>相应的样式是这样的:.input {&nbsp; &nbsp; position: fixed;&nbsp; &nbsp; bottom: 0;&nbsp; &nbsp; left: 0;&nbsp; &nbsp; right: 0;&nbsp; &nbsp; height: 30px;}可以改成这样:首先是DOM:<div class=input-wrapper>&nbsp; &nbsp; <div class=input>&nbsp; &nbsp; &nbsp; &nbsp; <input/>&nbsp; &nbsp; </div></div>其次样式中加一个:.input-wrapper {&nbsp; &nbsp; min-height: 30px;}相当于在底部加了一个Placeholder, 如果你要希望永远在上面的话, 那你就要考虑使用单屏应用了, 也就是让整个page的高度和屏幕高度相同, 然后消息列表的滚动只是滚动自身的内容. 如果这样的话就好办了, 直接flex布局就OK:首先看DOM:<!DOCTYPE html><html><body><div class=chatroom>&nbsp; &nbsp; <div class=messages>&nbsp; &nbsp; &nbsp; &nbsp; <div class=message>...</div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class=input>&nbsp; &nbsp; &nbsp; &nbsp; <input/>&nbsp; &nbsp; </div></div></body></html>然后是样式:html, body, .chatroom {&nbsp; &nbsp; height: 100%;&nbsp; &nbsp; min-height: 100%;}.chatroom {&nbsp; &nbsp; display: flex;&nbsp; &nbsp; flex-direction: column;}.messages {&nbsp; &nbsp; display: flex;&nbsp; &nbsp; overflow: auto;&nbsp; &nbsp; flex-direction: column;&nbsp; &nbsp; flex: 1;}.message {&nbsp; &nbsp; display: flex;&nbsp; &nbsp; flex-shrink: 0;}.input {&nbsp; &nbsp; display: flex;&nbsp; &nbsp; flex-direction: row;&nbsp; &nbsp; height: 30px;&nbsp; &nbsp; align-items: center;&nbsp; &nbsp; justify-content: center;&nbsp; &nbsp; flex-shrink: 0;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript