function?move(obj,json,fn){
var?flag=true;
clearInterval(obj.timer);
obj.timer=setInterval(function(){
for(var?attr?in?json){
var?icur=0;
//判斷傳入的是否為opacity
if(attr?==?"opacity"){
icur=Math.round(parseFloat(getStyle(obj,attr))*100);
}else{
icur=parseInt(getStyle(obj,attr));
}
var?speed=(json[attr]-icur)/10;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
//判斷是否全部達到最終形態(tài),如沒有繼續(xù)函數(shù)
if(icur?!=?json[attr]){
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?type="text/javascript">
window.onload=function(){
var?oDiv=document.getElementById('d1'),
as=oDiv.getElementsByTagName('a');
for(var?i=0;i<as.length;i++){
as[i].onmouseover=function(){
var?_this=this.getElementsByTagName("i")[0];
move(_this,{top:-20,opacity:0},function(){
_this.style.top=40+'px';
move(_this,{top:20,opacity:100});
});
};
}
}
</script>
2017-04-24
可以去學(xué)習(xí)下js的閉包。
2017-04-24
看了另外一位同學(xué)的解釋,需要把flag定義在定時器內(nèi),否則flag永遠都是false,所以停不下來。導(dǎo)致這個原因
2017-04-24
在18行后加入
else{
????flag=true;
}
即可