页面效果展示

HTMl代码布局页面
| <body> |
| <div class="container"> |
| <div class="showcon" id="showcon"></div> |
| <input type="text" id="msg" /> |
| <button onclick="send()">发送</button> |
| </div> |
| </body> |
复制
CSS样式部分
| <style> |
| .container { |
| width: 600px; |
| text-align: center; |
| padding: 20px 0; |
| margin: 0 auto; |
| background-color: green; |
| } |
| .container .showcon { |
| width: 550px; |
| height: 400px; |
| padding: 10px; |
| overflow: auto; |
| background-color: #fff; |
| margin: 0 auto 10px; |
| } |
| #msg { |
| width: 500px; |
| height: 40px; |
| } |
| .container button { |
| width: 50px; |
| height: 40px; |
| vertical-align: middle; |
| } |
| </style> |
复制
JavaScript部分
| <script> |
| var i = 0; |
| function send() { |
| var _ipt = document.getElementById('msg'); |
| |
| if (_ipt.value === '') { |
| alert('输入内容不能为空'); |
| return; |
| }else{ |
| i++; |
| } |
| |
| var _div = document.querySelector(".showcon"); |
| |
| |
| var r = randColor(); |
| var g = randColor(); |
| var b = randColor(); |
| |
| |
| if (i % 2 === 1) { |
| _div.innerHTML += `<p style='text-align:left;color:rgb(${r},${g},${b})'>${_ipt.value}</p>`; |
| } else { |
| _div.innerHTML += `<p style='text-align:right;color:rgb(${r},${g},${b})'>${_ipt.value}</p>`; |
| } |
| |
| _ipt.value = ''; |
| } |
| |
| |
| function randColor() { |
| return Math.floor(Math.random() * 256); |
| } |
| </script> |
复制