废话不多说直接上代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>手风琴效果</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
list-style: none;
}
body{
height: 100vh;
background: url(img/1.jpg);
background-size: cover;
background-position: center center;
display: flex;
justify-content: center;
align-items: center;
}
ul{
width: 800px;
height: 400px;
}
li{
width: 80px;
height: 100%;
float: left;
background-size: cover;
background-position:center center ;
/* 过渡属性 */
transition: .3s;
}
li p{
width: 5rem;
height: 100%;
background-color:rgba(255,255,255,.5);
color: white;
font-size: 45px;
}
.open{
width: 480px;
}
</style>
</head>
<body>
<ul>
<li class="open" style="background-image: url(img/1.jpg);">
<p>关帅帅</p>
</li>
<li style="background-image: url(img/2.jpg);">
<p>关小帅</p>
</li>
<li style="background-image: url(img/3.jpg);">
<p>关大帅</p>
</li>
<li style="background-image: url(img/4.jpg);">
<p>关太帅</p>
</li>
<li style="background-image: url(img/5.jpg);">
<p>关帅飞</p>
</li>
</ul>
<script type="text/javascript">
//获取li
var lis = document.getElementsByTagName("li");
for(var i=0; i<lis.length; i++){
lis[i].onclick=function(){
// 收起来已经展开的li
//1.使用for循环全部去掉open
for(var j=0;j<lis.length;j++){
lis[j].className="";
}
//展开点击的li(this)
this.className="open";
// 修改背景图
document.body.style.backgroundImage=this.style.backgroundImage;
}
}
</script>
</body>
</html>
运行效果: