前端js模拟对话框

前端js代码模拟对话,这也是面试官的一道面试题。需要的拿走~~~

原理:点击顶部头像切换发消息的好友,实现如下对话。。。

实现界面如下:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>模拟聊天</title>
    <style>
        *{
            margin: 0;
            padding:0;
        }
        .container{
            display: flex;
            width: 100%;
            height: 500px;
            justify-content: center;
        }
        #box{
            width: 350px;
            height: 500px;
            background-color: gainsboro;

        }
        .top{
            position: absolute;
            margin-top: 0;
            width: 350px;
            height: 70px;
            background-color: #5eb268;
        }
        .talk{
            position: absolute;
            margin-top: 70px;
            width: 350px;
            height: 360px;
        }
        .bottom{
            position: absolute;
            margin-top:430px ;
            width: 350px;
            height: 70px;
        }

        .showTxt{
            width: auto;
            height: auto;
            max-width: 230px;
            border:0;
            font-size: 15px;
            line-height: 40px;
            border-radius: 2px;
            word-break: break-all;
            list-style: none;
            margin-top: 5px;
            display: list-item;
        }
        .left{
            padding-right: 5px;
            text-align: left;
            margin-left: 5px;
            float: left;
            background-color: #e1e9e1;
        }
        .right{
            padding-left:5px ;
            text-align: right;
            margin-right: 5px;
            float: right;
            background-color: #e1e9e1;
        }
        img{
            width: 50px;
            height: 50px;
            border-radius: 50%;
        }
        .Img{
            margin-left: 40%;
            margin-top: 10px;
            margin-bottom: 10px;
        }
        .Itmg{
            float: left;
            width: 40px;
            height: 40px;
            border-radius: 50%;
        }
        .rtmg{
            float: right;
            width: 40px;
            height: 40px;

        }
        input{
            float: left;
            border-style: none;
            width: 250px;
            height: 45px;
            border-radius: 5%;
            background-color: gainsboro;
        }
        .txt{
            margin-left: 10px;
            background-color: #fdfdff;
        }
        .btn{
            margin-left:7px;
            width: 70px;
            background-color: #5eb268;
            color: white;
        }
    </style>
</head>
<body>
<div class="container">
<div id="box">
    <div class="top">
        <img class="Img" src="demo1.jpeg">
    </div>
    <div class="talk">
        <ul>
           <!--<li class="left showTxt"><img  class="Itmg" src="demo1.jpeg">dsczdc</li>
            <div style="clear: both"></div>
            <li class="right showTxt"><img  class="rtmg" src="demo6.jpg">sdasdfsdfc</li>-->
        </ul>
    </div>
    <div class="bottom"><input type="text" class="txt"><input class="btn" type="button" value="发送"></div>
</div>
</div>
<script>
    var box=document.getElementById('box');
    var top=document.getElementsByClassName('top')[0];
    var talk=document.getElementsByClassName('talk')[0];
    var bottom=document.getElementsByClassName('bottom')[0];
    var txt=document.getElementsByClassName('txt')[0];
    var btn=document.getElementsByClassName('btn')[0];
    var Img=document.getElementsByClassName('Img')[0];
    var UL=document.getElementsByTagName('ul')[0];
    //点击顶部头像切换聊天人
    var num=0;
   Img.onclick=function(){
       num++;
       if(num%2==0){
           Img.src='demo1.jpeg'
       }else{
           Img.src='demo6.jpg'
       }
   };
    //发送的事件
    btn.onclick=function(){
        addcontent();

    };
    function addcontent(){
        var LI=document.createElement('li');
        var newimg=document.createElement('img');
        //判断聊天对象是那边
        var b=document.createElement('p');
        b.style.clear='both';
        UL.appendChild(b)
        if(num%2==0){
            //添加对话框
            LI.innerHTML=txt.value;
            LI.className='left showTxt';
            UL.appendChild(LI);
            txt.value='';
            //添加头像
            newimg.src=Img.src;
            newimg.className='Itmg';
            LI.appendChild(newimg);

            //清除浮动
            var p=document.createElement('p');
            p.style='clear:both';
            UL.appendChild('p');
        }else{
                //添加对话框
                LI.innerHTML=txt.value;
                LI.className='right showTxt';
                UL.appendChild(LI);
                txt.value='';
                //添加头像
                newimg.src=Img.src;
                newimg.className='rtmg';
                LI.appendChild(newimg);

                //清除浮动
                var p=document.createElement('p');
                p.style='clear:both';
                UL.appendChild('p');
        }
    }
</script>
</body>
</html>
发布了79 篇原创文章 · 获赞 36 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/yezi__6/article/details/89429261