模拟app聊天功能

废话不多说,直接上代码。
注意:需要自己建个img目录存放图片。

<html>
<head>
<title>纯css实现的漂亮圆角按钮_网页代码站(www.6a8a.com)</title>
<style type="text/css">
   body,div,ul,li,img{margin: 0;padding: 0;border: 0;}
   body,button{
      font-family: 'Courier New', Courier, monospace;
      font-size: 18px;
   }
   li{
      list-style-type: none;
   }
   .left{
      width: 350px;
   }
   .left img{
      float: left;
      width: 100px;
      height: 100px;
      border-radius: 200px;
   }
   .left p{
      height: 100px;
      line-height: 100px;
      text-align: center;
      background-color: aliceblue;
   }

   .right{
      width: 350px;
   }

   .right img{
      float: right;
      width: 100px;
      height: 100px;
      border-radius: 200px;
   }

   .right p{
      height: 100px;
      line-height: 100px;
      text-align: center;
      background-color: aliceblue;
   }

   #btn{
      border: none;
      height: 40px;
      width: 80px;
      background-color:rgb(190, 132, 152);
      border-radius: 20px;
      color: white;
      font-style: normal;
   }
</style>
</head>
<body>
<div>
   <ul class="ul">
      <li class="left">
         <img src="img/left.jpg"/>
         <p>模仿聊天记录框</p>
      </li>
      <li class="right">
         <img src="img/right.jpg"/>
         <p>点击输入框左边头像更换头像</p>
      </li>

      <li>

      </li>
   </ul>
</div>
<div class="nav">
   <img id="img" src="img/left.jpg" style="width: 100px;height:100px;border-radius: 200px;float: left;"> 
   <input id="txt" style="line-height: 100px;border: none;"/>
   <button id="btn">发送</button>
</div>
<script>
   var img = document.getElementById("img")
   var flag = true
   img.onclick = function(){
      if(flag){
         img.src="img/right.jpg"
         flag = false
      }else{
         img.src="img/left.jpg"
         flag = true
      }
   }

   var txt = document.getElementById("txt")
   var btn = document.getElementById("btn")
   var ul = document.querySelector(".ul")
   btn.onclick = function(){debugger
      var aa = txt.value
      if(aa == ''){
         alert("内容不能为空!")
      }else{
         if(flag){
            console.log(aa)
            ul.innerHTML += `<li class='left'> <img src="img/left.jpg"/><p>`+aa+`</p></li>`
         }else{
            ul.innerHTML += `<li class='right'> <img src="img/right.jpg"/><p>`+aa+`</p></li>`
         }
      }

   }
</script>
</body>
</html>

效果如下:


1.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容