为啥鼠标上下来回滚动时候,li消失,或者无限制的增加,是浏览器运行比较卡,还是代码问我。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<style type="text/css">
*{margin: 0;padding: 0;}
ul{list-style: none;}
li{width: 200px;height: 100px;background: #8B4513;margin-top: 10px;}
</style>
<script type="text/javascript">
window.onload=function(){
var lia=document.getElementsByTagName("li");
for(var i=0;i<lia.length;i++){
lia[i].timer=null;
lia[i].onmouseover=function(){
startMove(this,400);
}
lia[i].onmouseout=function(){
startMove(this,200);
}
}
}
function startMove(obj,target){
clearInterval(obj.timer);
var speed=(target-obj.offsetWidth)/10;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
obj.timer=setInterval(function(){
if(obj.offsetWidth==target){
clearInterval(obj.timer);
}else{
obj.style.width=obj.offsetWidth+speed+'px';
}
},30)
}
</script>
<body>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>