//html部分
<style>
*{margin: 0;padding: 0;}
html,body{
width:100%;
height:100%;
}
#box{
width: 100%;
height: 0;
background:#abcdef;
overflow:hidden;
transition:height 2s ease;
}
ul{
list-style:none;
padding:0;
margin:0;
height:100%;
display:flex;
justify-content:space-evenly;
align-items: center;
}
li{
width:320px;
height:200px;
}
li:nth-child(1){
background:url(./images/1.jpg) 100%/320px;
}
li:nth-child(2){
background:url(./images/2.jpg) 100%/320px;
}
li:nth-child(3){
background:url(./images/3.jpg) 100%/320px;
}
</style>
<script>
var btn = document.querySelector('button') //获取按钮
var box = document.querySelector('#box') //获取盒子
btn.onclick = function(){ //给按钮绑定点击事件
if(this.innerText == '打开'){
this.innerText = '关闭'
box.style.height = '200px'
}else{
this.innerText = '打开'
box.style.height = '0'
}
}
var lis = document.querySelectorAll('li') //获取li元素
for(var i = 0;i<lis.length;i++){ //通过循环得到li的索引
lis[i].onclick = function(){ //给每一个li绑定点击事件
//此处的需要用到取行内式以外样式的获取方式
var img = window.getComputedStyle.backgroundImage
document.body.backgroundImage = img +' 100% 100%/100% 100%' //试图片平铺
btn.innerText = '打开'
btn.style.height = '0'
}
}
</script>