Html+Css+JavaScript实现聊天框通信功能

javascript 复制代码
<!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>
相关推荐
Moment15 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
爱敲代码的小鱼16 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax
铅笔侠_小龙虾18 小时前
Flutter 实战: 计算器
开发语言·javascript·flutter
大模型玩家七七18 小时前
梯度累积真的省显存吗?它换走的是什么成本
java·javascript·数据库·人工智能·深度学习
2501_9447114318 小时前
JS 对象遍历全解析
开发语言·前端·javascript
发现一只大呆瓜19 小时前
虚拟列表:支持“向上加载”的历史消息(Vue 3 & React 双版本)
前端·javascript·面试
阔皮大师19 小时前
INote轻量文本编辑器
java·javascript·python·c#
lbb 小魔仙19 小时前
【HarmonyOS实战】React Native 表单实战:自定义 useReactHookForm 高性能验证
javascript·react native·react.js
_codemonster19 小时前
Vue的三种使用方式对比
前端·javascript·vue.js
全栈前端老曹20 小时前
【MongoDB】Node.js 集成 —— Mongoose ORM、Schema 设计、Model 操作
前端·javascript·数据库·mongodb·node.js·nosql·全栈