首页 > 解决方案 > 我无法将我的 div 内容完美地滚动到底部

问题描述

我打算让我的自动滚动 div 内容滚动到底部,但不知何故它在最后一行之前至少滚动了 1 行。这个 div 应该像 textarea,但我不想使用 textarea,因为它不会格式化任何 html。

我试图让我的滚动条件减一,滚动事件加一

我有这样的javascript

var chatLog = document.getElementById('chatlog');
var logLength = chatLog.scrollHeight - chatLog.offsetHeight;

chatLog.innerHTML += msg;

if (chatLog.scrollTop < logLength - 1) {
    chatLog.scrollTop = logLength + 1;
}

和这样的CSS

.chatlog {
    width: 70%;
    height: 200px;
    word-wrap: break-word;
    flex-direction: column-reverse;
    background-color: #FFFFFF;
    padding: 10px;
    flex: 1;
    overflow: auto;
    font-size: 14px;
    margin: 20px auto;
    border-radius: 6px;
    border: 0;
}

我希望输出将是最后一行,但实际输出是最后一行之前的一行。

标签: javascriptcsshtml

解决方案


推荐阅读