案例1-微信聊天-用开关

<!DOCTYPE html>

<html lang="en">

<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>Document</title>

<style>

*{

margin: 0;

padding: 0;

}

ul{

list-style: none;

}

img{

vertical-align: top;

}

.wrap{

width: 1000px;

height: 800px;

background: url(img/bg.jpg) no-repeat;

position: absolute;

left: 50%;

top: 50%;

margin-top: -400px;

margin-left: -500px;

}

.iphone{

width: 356px;

height: 679px;

background: url(img/iphone.png) no-repeat;

position: absolute;

left: 50%;

top: 50%;

margin-left: -178px;

margin-top: -338.5px;

}

.list{

width: 318px;

height: 498px;

margin: 47px 0 0 19px;

padding: 20px;

overflow: auto;

box-sizing: border-box;

 

}

.list li::after{

content: '';

display: block;

clear: both;

}

.list img{

width: 35px;

height: 40px;

}

.list p{

width: 230px;

box-sizing: border-box;

padding: 10px;

border-radius: 10px;

max-width: 200px;

}

.nav{

height: 60px;

background: #e5e5e5;

width: 318px;

margin-left:19px;

}

.nav::after{

content: '';

display: block;

clear: both;

}

.nav img{

padding: 10px 0 0 16px;

float: left;

}

.nav input{

float: left;

width: 190px;

height: 40px;

border: none;

background: #fff;

border-radius: 10px;

margin: 11px 0 0 13px;

}

.nav button{

width: 64px;

height: 40px;

font: 16px / 40px "微软雅黑";

color: #e15671;

margin-top: 11px;

text-align: center;

border: none;

background: none;

}

.right,.left{

margin-bottom: 20px;

}

.right img{

float: right;

}

.right p{

float: right;

background: #bab3ce;

color: #fff;

margin-right: 13px;

text-align: right;

}

.left img{

float: left;

}

.left p{

float: left;

background: #e5e5e5;

margin-left: 13px;

}

</style>

 

</head>

<body>

<div class="wrap">

<div class="iphone">

<ul class="list">

<li class="right">

<img src="img/monster2.png" alt="">

<p>说:哈?好呀,多少钱我都愿意~~呵呵</p>

</li>

<li class="left">

<img src="img/monster.png" alt="">

<p>说:这个简单,我们去妙味远程课堂学就行啦~~~</p>

</li>

<li class="right">

<img src="img/monster2.png" alt="">

<p>说:好呀~~~你先帮我学会JS再说~~</p>

</li>

<li class="left">

<img src="img/monster.png" alt="">

<p>说:咦?土豪??</p>

</li>

<li class="right">

<img src="img/monster2.png" alt="">

<p>说:大家好,我是土豪~</p>

</li>

</ul>

<div class="nav">

<img id="icon" src="img/monster2.png" alt="">

<input type="text" id="text">

<button id="btn">发送</button>

</div>

</div>

</div>

 

<script>

 

/*

切换发送的头像

1-获取头像的图片的元素

2-绑定点击事件

3-切换图片的src(注意需要判断,应该是哪个图片)

*/

var pic = document.getElementById('icon');

var onOff = true;

pic.onclick = function(){

if(onOff){

pic.src = 'img/monster.png';

onOff = false;

}else{

pic.src = 'img/monster2.png';

onOff = true;

}

};

/*

添加内容进ul

1-获取元素(按钮、头像、文字、list)

2-给按钮绑定点击时间

3-获取头像地址,以及文字内容(如果内容为空不允许发送)

4-扔到list里面去

*/

var btn = document.getElementById('btn');

var txt = document.getElementById('text');

var list = document.querySelector('.list');

btn.onclick = function(){

var value = txt.value;

if(value == ''){

alert('内容为空,不允许发送');

}else{

if(onOff){

list.innerHTML = '<li class="left"><img src="img/monster2.png" alt=""><p>'+value+'</p></li>' + list.innerHTML;

}else{

list.innerHTML = '<li class="right"><img src="img/monster.png" alt=""><p>'+value+'</p></li>' + list.innerHTML;

}

}

};

</script>

</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值