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>
相关推荐
江城开朗的豌豆几秒前
解密useEffect:让副作用无所遁形!
前端·javascript·react.js
用户6120414922138 分钟前
springmvc做的学生考勤管理系统
javascript·后端·spring
CODE_RabbitV14 分钟前
【1分钟速通】 HTML快速入门
前端·html
她超甜i26 分钟前
前端通过后端给的webrtc的链接,在前端展示,并更新实时状态
前端·javascript·webrtc
小*-^-*九30 分钟前
php 使用html 生成pdf word wkhtmltopdf 系列1
pdf·html·php
歪歪10032 分钟前
Redux和MobX在React Native状态管理中的优缺点对比
前端·javascript·react native·react.js·架构·前端框架
今天不要写bug1 小时前
基于elementUI实现一个可编辑的表格(简洁版)
前端·javascript·elementui
一只小阿乐1 小时前
vue-router 的实现原理
前端·javascript·vue.js·路由·vue-router
Zz_waiting.1 小时前
案例开发 - 日程管理 - 第七期
开发语言·前端·javascript·vue.js·html·路由
writeone1 小时前
9-10关于JS初学产生的问题
开发语言·javascript·ecmascript