代码有问题,达不到设置的width和height,帮忙看看好吗
<!DOCTYPE html>
<html>
<head>
<title>同时运动</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<style type="text/css">
*{
margin: 0;
padding: 0;
}
ul,li{
list-style: none;
}
ul li{
width:200px;
height:100px;
background: yellow;
margin-bottom: 20px;
filter:alpha(opacity:30);
opacity: 0.3;
border:1px solid #000;
}
</style>
<script type="text/javascript">
window.onload=function(){
var Li = document.getElementById('li1');
Li.onmouseover=function(){
startMove(Li,{width:300,height:200,opacity:100});
}
Li.onmouseout=function(){
startMove(Li,{width:200,height:100,opacity:30});
}
}
function startMove(obj,json,fn){
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var flag = true;
for(var i in json){
//1.取当前的值
var icur = 0;
if(i == 'opacity'){
icur = Math.round(parseFloat(getStyle(obj,i))*100);
}else{
icur = parseInt(getStyle(obj,i));
}
//2.算速度
var speed = (json[i]-icur)/10;
speed = speed >0?Math.ceil(speed):Math.floor(speed);
//3.检测停止
if(icur != json[i]){
flag = false;
//alert(flag);
if(i == 'opacity'){
obj.style.filter = 'alpha(opacity:'+(icur+speed)+')';
obj.style.opacity = (icur+speed)/100;
}else{
obj.style[i] = icur + speed + 'px';
}
}else{
flag = true;
}
}//for结束
if(flag){
clearInterval(obj.timer);
//fn是回调函数
if(fn){
fn();
}
}
},30);
}
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
//return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj,false)[attr];
}
</script>
</head>
<body>
<ul>
<li id='li1'></li>
</ul>
</body>
</html>