js滚动条自动到底(聊天框效果)

今天同事写个小demo的时候遇到的,太久没有写原生了都快忘了,就随手写一下记录一下大概思路

效果

<!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>Document</title>
    <style>
        #area {
            border: 1px;
            background-color: lightblue;
            width: 200px;
            height: 200px;
            overflow: auto;
        }
    </style>
</head>

<body>
    <div id="area">
    </div>
    <button id="btn">发送</button>
</body>
<script>
    document.getElementById('btn').onclick = function () {
        var text = document.createElement('p')
        text.innerHTML = '11111'
        console.log(text);
        console.log(document.getElementById('area'));
        document.getElementById('area').appendChild(text)
        document.getElementById('area').scrollTop = document.getElementById('area').scrollHeight
    }
</script>

</html>

猜你喜欢

转载自blog.csdn.net/shinjie1210/article/details/121650149
今日推荐