布局 思路
- 需求:
- 两个用户聊天会切换头像
- 点击发送会将用户输入的信息输出到界面上
- 消息页面布局
- 顶部的bar
- 内容区域
- 展示自己和对方的发送的结果信息
- 信息高度到达最大高度,实现内容当前信息向上滚动展示
三大区域
*切换用户头像技巧
声明一个标识为tag的变量,根据tag的值来确定是自己的头像还是对方的图片
var tag = 0;
if(tag==0){
img.src=arr[1];
tag=1;
}else{
img.src=arr[0];
tag=0;
}
*图片的地址或位置也可以存入数组中
var arr=['./images/1.png','./images/2.png'];
var times = document.getElementsByClassName(‘r’); console.log(times[0].children[0].innerHTML);
具体实例请参考如下
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>微信</title>
<style>
*{
padding: 0;
margin: 0;
list-style: none;
font-family: '微软雅黑';
}
#container{
width: 450px;
height: 600px;
background-color: #eee;
margin: 10px auto;
position: relative;
box-shadow: 0 0 16px #999;
}
.header{
height: 40px;
background: black;
color: white;
line-height: 40px;
font-size: 20px;
padding: 0 10px;
}
.footer{
width: 430px;
height: 40px;
background-color: #999;
position: absolute;
bottom: 0;
padding: 10px;
}
.footer input{
width: 300px;
height: 38px;
outline: none;
font-size: 16px;
text-indent: 10px;
position: absolute;
border-radius: 6px;
right: 80px;
}
.footer span{
display: inline=block;
width: 62px;
background-color: #ccc;
font-weight: 900;
line-height: 38px;
cursor: pointer;
text-align: center;
position: absolute;
right: 10px;
top: 14px;
}
.footer span:hover{
color: #777;
background-color: #ddd;
}
.icon{
display: inline-block;
background-color: red;
width: 50px;
height: 50px;
border-radius: 50%;
position: absolute;
left: 10px;
bottom: 3px;
cursor: pointer;
overflow: hidden;
}
img{
width: 60px;
height: 60px;
border-radius: 8px;
}
#scr{
position: relative;
height: 500px;
overflow: hidden;
}
.content{
position: absolute;
font-size: 20px;
width: 435px;
overflow: auto;
padding: 5px;
background-color: #eee;
}
.content li{
margin-top: 10px;
padding-left: 10px;
width: 412;
display: block;
clear: both;
overflow: hidden;
}
.content li img{
float: left;
}
.content li span{
background-color: #7cfc00;
padding: 10px;
border-radius: 10px;
float: left;
margin: 6px 10px 0 10px;
max-width: 310px;
border: 1px solid #ccc;
box-shadow: 0 0 3px #ccc;
}
.content li img.imgLeft{
float: left;
}
.content li img.imgRight{
float: right;
}
.content li span.spanLeft{
float: left;
}
.content li span.spanRight{
float: right;
}
</style>
</head>
<body>
<div id="container">
<div class="header r">
<span style="float: right;">20:30</span>
<span style="float: left;">小妹客服聊天中</span>
</div>
<div id="scr">
<ul class="content">
</ul>
</div>
<div class="footer">
<div class="icon">
<img src="images/1.png" id="icon" >
</div>
<input id="text" type="text" placeholder="晴说点什么?"/>
<span id="btn">发送</span>
</div>
</div>
</body>
<script type="text/javascript">
var img = document.getElementById('icon');
var arr=['./images/1.png','./images/2.png'];
var tag = 0;
var btn = document.getElementById('btn');
var num=-1;
btn.onclick=function(){
var text = document.getElementById('text').value;
if(text==''){
alert('聊天内容不能为空哦~~');
return;
}else{
var contents = document.getElementById('container').children[1].children[0];
contents.innerHTML+='<li><img src='+arr[tag]+'><span>'+text+'</span></li>';
}
if(tag==0){
img.src=arr[1];
tag=1;
}else{
img.src=arr[0];
tag=0;
}
var userIcon = contents.getElementsByTagName('img');
var usserspan = contents.getElementsByTagName('span');
num++;
console.log(userIcon[num]);
if(tag==0){
userIcon[num].className='imgLeft';
usserspan[num].className='spanLeft';
}else{
userIcon[num].className='imgRight';
usserspan[num].className='spanRight';
}
document.getElementById('text').value='';
checkTheHeight(contents);
}
function checkTheHeight(contents){
var scr = document.getElementById('scr');
var len = contents.children.length-1;
if(len){
if(contents.offsetHeight>scr.clientHeight){
contents.style.top=contents.offsetTop-contents.children[len].offsetHeight-10+'px';
}
}
}
setInterval(function(){
var date = new Date();
var day = date.getDay();
var month = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var times = document.getElementsByClassName('r');
times[0].children[0].innerHTML=hour+':'+minute;
},1000);
</script>
</html>
|