window.onload?=?function(){
????var?li?=?document.getElementById('li1');
????li.onmouseover?=?function(){
????????startMove(li,{'width':400,'height':200})
????}
????li.onmouseout?=?function(){
????????startMove(li,{'width':200,'height':100})
????} ?
}
//運動框架?startMove函數
function?startMove(obj,json,fn){
????clearInterval(obj.timer);
????var?flag?=?true;
????//開啟定時器
????obj.timer?=?setInterval(function(){
????????//遍歷json
????????for?(var?attr?in?json)?{
????????????var?speed?=?0;
????????????//取當前值?icur
????????????if?(attr?==?'opacity')?{
????????????????var?icur?=?Math.round(parseFloat(getStyle(obj,attr))*100);
????????????}else{
????????????????var?icur?=?parseInt(getStyle(obj,attr));
????????????}
????????????//算速度:speed
????????????//目標值:json[attr]
????????????if?(icur?<?json[attr])?{
????????????????speed?=?Math.ceil((json[attr]?-?icur)/8);
????????????}else{
????????????????speed?=?Math.floor((json[attr]?-?icur)/8);
????????????}
????????????//檢測停止
????????????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';
????????????}???
????????}
????????//動畫結束時,這里?flag?是怎么變成?true?的???????
????????if?(flag)?{
????????????clearInterval(obj.timer);
????????????if?(fn)?{fn()}
????????}?????????
????},30)
}
2018-09-14
視頻中flag的聲明位置應該是寫錯了,實際上應該是寫在定時器內。
視頻中為什么動畫會停止?完全是因為speed歸0了,動畫停止了,但實際上定時器并沒有停止,還在繼續運行。
感謝 @紙丶兩面白? ?同學!