不知道錯在哪了??
<!doctype?html>
<html>
<head>
<meta?charset="utf-8">
<title>hello?world</title>
<style?type="text/css">
#li1{
width:200px;
height:100px;
list-style:none;
background-color:yellow;
border:3px?solid?#000;
opacity:0.3;
filter:alpha(opacity:30);
}?
</style>
<script?src="move.js"></script>
<script?type="text/javascript">
window.onload=function(){
var?li=document.getElementById("li1");
li.onmouseover=function(){
startMove(li,{width:400,height:200});
};
};
</script>
</head>
<body>
<ul>
<li?id="li1"></li>
</ul>
</body>
</html>下面是js
function?startMove(obj,json,fn){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
for(var?attr?in?json){
var?temp=0;
//取當前值
if(attr=="opacity"){
temp=Math.round(parseFloat(getStyle(obj,attr))*100);
}else{
temp=parseInt(getStyle(obj,attr));
}
//計算速度
var?speed=(json[attr]-temp)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
//檢測停止
if(temp==json[attr]){
clearInterval(obj.timer);
if(fn){
fn();
}
}else{
if(attr=="opacity"){
obj.style.filter="alpha(opacity:"+(temp+speed)+")";
obj.style.opacity=(temp+speed)/100;
}else{
obj.style[attr]=temp+speed+"px";
}
}
}
},30);
}
function?getStyle(obj,attr){
if(obj.currentStyle){
return?obj.currentStyle[attr];
}else{
return?getComputedStyle(obj,false)[attr];
}
}為什么沒反應呢??
2017-05-03
測試了一下可以用,看看是不是JS<script?src="move.js"></script>引用路徑有誤,另外需要加Flag來保證只有每個值都達到target才停止動畫,不然透明度到1寬度還沒到400就不動了