javascript 实现手风琴特效

时间:2022-08-10 15:07:46
 <!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 media="screen">
* {
margin: 0;
padding: 0;
}
div {
width: 1150px;
height: 400px;
margin: 50px auto;
border: 1px solid #ddd;
overflow: hidden;
}
ul {
list-style: none;
}
div ul {
width: 1300px;
}
div li {
width: 240px;
height: 400px;
float: left;
cursor: pointer;
}
</style>
</head>
<body>
<div>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div> <script src="animate.js" charset="utf-8"></script>
<script type="text/javascript">
var div = document.getElementsByTagName("div")[0];
var liArr = div.getElementsByTagName("li");
//给li添加背景颜色
var colorArr = ["mistyrose", "lemonchiffon", "gold", "orange", "tomato"];
for (var i = 0; i < colorArr.length; i++) {
liArr[i].style.backgroundColor = colorArr[i]; //绑定onmouseover事件,鼠标放入到li中该盒子变宽,其他的盒子变窄
liArr[i].onmouseover = function(){
//排他思想
for (var j = 0; j < liArr.length; j++) {
//引用框架实现宽度变窄
animate(liArr[j],{"width": 100});
}
//剩下自己
animate(this,{"width": 800});
}
}
//移开大盒子,回复原样
div.onmouseout = function(){
for (var j = 0; j < liArr.length; j++) {
//引用框架实现宽度变窄
animate(liArr[j],{"width": 240});
}
}
</script>
</body>
</html>