不写Math.round()得到的是整数还有必要写吗
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
*{margin:0;
padding:0;}
div{width:200px;
height:200px;
background:red;
float:left;
margin:10px;
filter:alpha(opacity:30);
opacity:0.3;}
li{width:200px;
height:100px;
background:yellow;
margin:10px;
margin-left:0;
list-style:none;
position:relative;
top:220px;
border:10px solid red;}
</style>
<script>
window.onload=function(){
var div=document.getElementById('div1')
div.onmouseover=function(){
startMove(this,'opacity',100);
}
div.onmouseout=function(){
startMove(this,'opacity',30);
}
}
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}
function startMove(obj,attr,iTarget){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
if(attr=='opacity'){
var iCur=parseFloat(getStyle(obj,attr))*100;
}else{
var iCur=parseInt(getStyle(obj,attr));
}
//alert(iCur);
var speed=(iTarget-iCur)/20
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(iTarget==iCur){
clearInterval(obj.timer)
}else{
if(attr=='opacity'){
obj.style.opacity=(iCur+speed)/100;
}else{
obj.style[attr]=iCur+speed+'px'
}
}
},30)
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>