請慕友幫忙看看什么問題,出不來?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>同時運動</title>
<style type="text/css">
ul{list-style: none;margin: 0;padding: 0;}
ul li{width: 200px;height: 100px;background: yellow;margin-bottom: 20px;border:4px solid #000;filter:alpha(opacity:30);opacity: 0.3;}
</style>
<script type="text/javascript">
window.onload=function(){
var oLi=document.getElementById('li1');
oLi.onmouseover=function(){
startMove(oLi,{width:201,height:200,opacity:100});
// startMove(oLi,'width',400);
// startMove(oLi,'height',200);
// 原來的運動框架不能滿足同時運動的效果,要用到JSON
}
oLi.onmouseout=function(){
startMove(oLi,{width:200,height:100,opacity:30});
}
}
// startMove(obj,{attr1:iTarget1,attr2:iTarget2},fn)
function startMove(obj,json,fn){
var flag=true;//假設(shè)所有運動都到達(dá)目標(biāo)值
clearInterval(obj.timer);//一開始要清除下定時器
obj.timer=setInterval(function(){//重新創(chuàng)建一個定時器
for(var attr in json){
//1.取當(dāng)前的值
var icur=0;
if(attr == 'opacity'){
icur=Math.round(parseFloat(getStyle(obj,attr))*100);//判斷當(dāng)前是不是透明度
}
else{
icur=parseInt(getStyle(obj,attr));
}
//2.算速度
var speed=(json[attr]-icur)/8;//速度值等于目標(biāo)值減去當(dāng)前寬度再除一個數(shù)字
speed=speed>0?Math.ceil(speed):Math.floor(speed);//只要是緩沖運動都要判斷
//3.檢測停止
if(icur != json[attr]){//如果當(dāng)前寬度等于目標(biāo)值清除定時器
flag=false;
}
if(attr == 'opacity'){//等號之間的空格不能省
obj.style.filter='alpha(opacity:'+(icur+speed)+')';
obj.style.opacity=(icur+speed)/100;
}
else{
obj.style[attr]=icur+speed+'px';//否則緩沖增加寬度
}
}
if(flag){
clearInterval(obj.timer);
if(fn){
fn();
}
}
},30)
}
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}
else{
return getComputedStyle(obj,false)[attr];
}
}
</script>
<!--<script type="text/javascript">
var json={a:12,b:13};
for(var i in json){
//alert(i);
alert(json[i]);
}
</script>-->
</head>
<body>
<ul>
<li id="li1"></li>
</ul>
</body>
</html>
2016-02-02
調(diào)用函數(shù)的時候沒有參數(shù)fn
2016-02-02