js手风琴案例1
演示如图:
css代码:
*{
margin: 0;
padding: 0;
}
html,body{
height: 100%;
width:100%;
background-color: black;
}
body{
position: relative;
}
.main{
position: absolute;
height: 450px;
width: 1114px;
/*background-color: orange;*/
left:50%;
top:50%;
transform: translate(-50%,-50%);
}
.main ul{
list-style: none;
}
.main ul li{
float: left;
width: 100px;
height: 400px;
transition: all 1s;
border: 1px solid #000000;
}
.main ul li .li-active{
width: 500px;
height: 450px;
}
.main ul li:nth-child(1){
background: url("../images/ruili_img1.jpg")no-repeat center/cover;
}
.main ul li:nth-child(2){
background: url("../images/ruili_img2.jpg")no-repeat center/cover;
opacity: 0.8;
}
.main ul li:nth-child(3){
background: url("../images/ruili_img3.jpg")no-repeat center/cover;
opacity: 0.8;
}
.main ul li:nth-child(4){
background: url("../images/ruili_img4.jpg")no-repeat center/cover;
opacity: 0.7;
}
.main ul li:nth-child(5){
background: url("../images/ruili_img5.jpg")no-repeat center/cover;
opacity: 0.7;
}
.main ul li:nth-child(6){
background: url("../images/ruili_img6.jpg")no-repeat center/cover;
opacity: 0.6;
}
.main ul li:nth-child(7){
background: url("../images/ruili_img7.jpg")no-repeat center/cover;
opacity: 0.5;
}
.main ul li div{
width: 100px;
height: 400px;
}
.main ul li p{
width: 80px;
height: 50px;
line-height: 50px;
color: #ffffff;
padding-left: 20px;
background-color: #666666;
opacity: 0.8;
transition: all 1s;
}
.main ul li .p-active{
width: 480px;
height: 50px;
/*transition: all 1s;*/
}
.main ul li.li-active{
width: 500px;
height: 400px;
}
HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>手风琴练习</title>
<link rel="stylesheet" href="css/手风琴练习.css">
<script src="js/手风琴练习.js"></script>
</head>
<body>
<div class="main">
<ul>
<li class="li-active">
<div></div>
<p class="p-active">服饰</p>
</li>
<li>
<div></div>
<p>服饰</p>
</li>
<li>
<div></div>
<p>服饰</p>
</li>
<li>
<div></div>
<p>服饰</p>
</li>
<li>
<div></div>
<p>服饰</p>
</li>
<li>
<div></div>
<p>服饰</p>
</li>
<li>
<div></div>
<p>服饰</p>
</li>
</ul>
</div>
</body>
</html>
js代码:
window.onload = function () {
let lis = document.querySelectorAll(".main li");
let ps = document.querySelectorAll("p");
lis.forEach((item,index)=>{
item.onmouseover = function () {
lis.forEach((el,i)=>{
el.className = "";
ps[i].classList.remove("p-active");
});
this.className = "li-active";
ps[index].classList.add("p-active");
}
});
}