Html+Css+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>

  • 14
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值