<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .box {
            width: 100%;
            height: 500px;
            border: 1px solid red;
            overflow: auto;
        }

        .left,
        .right {
            display: inline-block;
            margin-bottom: 20px;
        }

        .left {
            float: left;
            width: 90%;
            /* height: 50px; */
            border: 1px solid green;
        }

        .right {
            float: right;
            width: 90%;
            /* height: 50px; */
            border: 1px solid skyblue;
            text-align: right;
        }

        /* 滚动条里的小方块 */
        .box::-webkit-scrollbar-thumb {
            border-radius: 3px;
            background-color: #eee;
        }

        /* 滚动条整体样式 */
        .box::-webkit-scrollbar {
            width: 5px;
            height: 5px;
            /* //高宽分别对应横竖滚动条的尺寸 */
        }

        /* //滚动条中心部分 */
        .box::-webkit-scrollbar-track {
            /* background-color: pink; */
        }

        /* 去除滚动条显示 */
        .box::-webkit-scrollbar {
            display: none
        }

        .container::after {
            content: "";
            display: table;
            clear: both;
        }

        .img {
            width: 50px;
            height: 50px;
        }
    </style>
</head>

<body>
    <p>
        <button class="return-top">返回顶部</button>
        <button class="delete-all-information">清空聊天记录</button>
    </p>
    <p>
        <input class="custom-inp" type="text">
        <button class="custom-btn">客户发送信息点击</button>
        <input class="custom-file" type="file">
    </p>
    <p>
        <input class="customer-inp" type="text">
        <button class="customer-btn">客服发送信息点击</button>
        <input class="customer-file" type="file">
    </p>
    <div class="box">
        <div class="container">
        </div>
    </div>
    <script>
        const box = document.querySelector('.box') // 外层盒子
        const container = document.querySelector('.container') // 包裹聊天框盒子
        const customBtn = document.querySelector('.custom-btn') // 客户点击发送按钮
        let customInp = document.querySelector('.custom-inp') // 客户信息
        let customFile = document.querySelector('.custom-file') // 客户文件
        const customerBtn = document.querySelector('.customer-btn') // 客服点击发送按钮
        let customerInp = document.querySelector('.customer-inp') // 客服信息
        let customerFile = document.querySelector('.customer-file') // 客户文件
        let returnTop = document.querySelector('.return-top') // 返回顶部按钮
        let deleteAllInformation = document.querySelector('.delete-all-information') // 清空聊天记录
        // 置底滚动条
        function currentDownScroll() {
            box.scrollTo({
                top: container.clientHeight,
                behavior: "smooth"
            })
        }
        currentDownScroll()

        // 返回顶部
        returnTop.onclick = () => {
            box.scrollTo({
                top: 0,
                behavior: "smooth"
            })
        }

        // 清空聊天记录
        deleteAllInformation.onclick = () => {
            document.querySelectorAll('.item').forEach(item => {
                container.removeChild(item)
            })
            document.querySelectorAll('.img').forEach(item => {
                container.removeChild(item)
            })
        }

        // 客户点击发送信息
        customBtn.onclick = () => {
            if (!customInp.value) return
            let div = document.createElement('div')
            div.className = 'left item'
            div.innerHTML = `<p>左</p><h1>${customInp.value}</h1>`
            container.appendChild(div)
            currentDownScroll()
            customInp.value = ''
        }

        // 客户发送图片
        customFile.onchange = () => {
            let div = document.createElement('div')
            div.className = 'left item'
            div.innerHTML = `<p>左</p><img class="img" src="${URL.createObjectURL(customFile.files[0])}">`
            container.appendChild(div)
            currentDownScroll()
        }

        // 客服点击发送信息
        customerBtn.onclick = () => {
            if (!customerInp.value) return
            let div = document.createElement('div')
            div.className = 'right  item'
            div.innerHTML = `<p>右</p><h1>${customerInp.value}</h1>`
            container.appendChild(div)
            currentDownScroll()
            customerInp.value = ''
        }

        // 客服发送图片
        customerFile.onchange = () => {
            let div = document.createElement('div')
            div.className = 'right item'
            div.innerHTML = `<p>右</p><img class="img" src="${URL.createObjectURL(customerFile.files[0])}">`
            container.appendChild(div)
            currentDownScroll()
        }
    </script>
</body>

</html>

原文地址:https://blog.csdn.net/m0_71349739/article/details/134652905

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_788.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注