js实现发送聊天信息并显示至页面

在这里插入图片描述
不是移动端!!不是移动端!!
只是背景放的是一个手机。(素材放到最下面了)
最近被这个卡了好久,昨天刚有了idea就写了个demo测试了一下,其实就是一个动态添加元素
给同样遇到问题的朋友一点启发。
(存在一点小bug,还没有实现气泡自适应和自动定位到新消息)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .clearFloat:after{
            content: '';
            height: 0;
            display: block;
            visibility: hidden;
            clear: both;
        }
        .clearFloat{
            zoom: 1;
        }
        .phone{
            background-image: url(img/phone.png);
            height: 460px;
            background-repeat: no-repeat;
            background-color: #f6f6f6;
            border: 1px solid transparent;
        }
        .screen{
            /* border: 1px solid red; */
            text-align: center;
            height: 340px;
            margin-top: 56px;
            position: relative;
        }
        .chat{
            /* border: 1px solid blue; */
            width: 194px;
            position: absolute;
            left: 18px;
            top: -10px;
            height: 340px;
        }
        .title{
            /* border: 1px solid red; */
            background-color: #ededed;
            font-weight: bold;
            margin-left: 2px;
            margin-top: 2px;
        }
        .content{
            /* border: 2px solid orange; */
            height: 290px;
            overflow-y: auto;
        }
        .bottom{
            width: 194px;
            position: absolute;
            bottom: 0;
            
        }
        .bottom input,.bottom button{
            float: left;
        }
        .bottom input{
            width: 138px;
            margin-right: 2px;
            margin-left: 2px;
            height: 17px;
            outline: none;
        }
        .bottom button{
            background-color: #3CB371;
            color: #fff;
            border-radius: 5px;
            border: 1px solid #3CB371;
            cursor: pointer;
        }
        .message{
            border: 1px solid transparent;
            height: 25px;
            line-height: 25px;
            margin-top: 6px;
        }
        .message img{
            width: 26px;
        }
        .myInfo{
            margin-right: 5px;
        }
        .myInfo p{
            float: right;
        }
        .myInfo p:last-child{
            border-radius: 5px;
            background-color: #87CEFA;
            margin-right: 0px;
            display: block;
            padding: 0 5px;
        }
        .rightTriangle{
            width: 0px;
            height: 0px;
            border-width: 5px;
            border-style: solid;
            border-color: transparent transparent transparent #87CEFA;
            position: relative;
            top: 6px;
        }
        .leftTriangle{
            width: 0px;
            height: 0px;
            border-width: 5px;
            border-style: solid;
            border-color: transparent gold transparent transparent;
            position: relative;
            top: 6px;
        }
        .robotInfo{
            margin-left: 5px;
        }
        .robotInfo p{
            float: left;
        }
        .robotInfo p:last-child{
            border-radius: 5px;
            background-color: gold;
            margin-right: 0px;
            display: block;
            padding: 0 5px;
        }
    </style>
</head>
<body>
    <div class="phone">
        <div class="screen">
            <div class="chat">
                <div class="title">复读机器人</div>
                <div class="content">
                    <!-- <div class="message">
                        <div class="myInfo clearFloat">
                            <p><img src="img/avatar.jpg" alt=""></p>
                            <p class="rightTriangle"></p>
                            <p>你好</p>
                        </div>
                    </div>
                    <div class="message">
                        <div class="robotInfo clearFloat">
                            <p><img src="img/robot.jpg" alt=""></p>
                            <p class="leftTriangle"></p>
                            <p>你好</p>
                        </div>
                    </div> -->
                    
                </div>
                
                <div class="bottom clearFloat">
                    <input type="text">
                    <button>&nbsp;&nbsp;发送&nbsp;&nbsp;</button>
                </div>
            </div>
        </div>
    </div>
    <script>
        var input = document.querySelector('input');
        var button = document.querySelector('button');
        var content = document.getElementsByClassName('content')[0];
        button.onclick = function(){
            // 点击按钮将input输入框的内容发送给机器人
            var value = input.value;
            if(value=='') return
            input.value = ''
            var myMessage = document.createElement('div');
            myMessage.className = 'message';
            myMessage.innerHTML = `
            <div class="myInfo clearFloat">
                <!-- 我发送的消息 -->
                <p><img src="img/avatar.jpg" alt=""></p>
                <p class="rightTriangle"></p>
                <p>${value}</p>
                </div>
            `
            // console.log(myMessage);
            content.appendChild(myMessage);
            
            var robotMessage = document.createElement('div');
            robotMessage.className = 'message'
            robotMessage.innerHTML = `
                <div class="robotInfo clearFloat">
                    <!-- 机器人发送的消息 -->
                    <p><img src="img/robot.jpg" alt=""></p>
                    <p class="leftTriangle"></p>
                    <p>${value}</p>
                </div>
            `
            content.appendChild(robotMessage)
        }
    </script>
</body>
</html>

图片素材
链接:https://pan.baidu.com/s/1WWpxQVlud-719svNJ2jF6A
提取码:dlgy

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值