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>
相关推荐
ice851 分钟前
揭秘JavaScript中“一切皆对象”:包装对象机制和原型链继承
javascript·面试
谢尔登3 分钟前
【Three.js】初识 Three.js
开发语言·javascript·ecmascript
Dream耀4 分钟前
解锁JavaScript函数式编程的核心技能
前端·javascript·设计模式
小白luffy6 分钟前
TypeScript枚举与常量枚举:从编译原理到最佳实践
前端·javascript
Jackson__10 分钟前
谈谈 js 小数加法精度问题
前端·javascript·面试
三天不学习18 分钟前
一键实现全站多语言化:translate.js 极简集成指南,支持Vue 、React 框架。
javascript·vue.js·react.js·多语言·translate.js
vvilkim1 小时前
Flutter 导航与路由管理:Navigator 的深入解析与实践
前端·javascript·flutter
小白探索世界欧耶!~2 小时前
react 使用 postcss-px-to-viewport 实现 px 自动转 vw 自适应
前端·javascript·vue.js·程序人生·react.js·postcss
ryipei2 小时前
vue纯前端根据页面或者后台数据,读取本地文档模板,填充数据后并导出
前端·javascript·vue.js
老李笔记3 小时前
VUE element table 列合并
javascript·vue.js·ecmascript