標籤:over log lte city 計時 add eve alpha font
本人也是菜鳥一枚,從網上搜尋了很久,封裝備忘了一套運動架構,加強大家對此的理解,希望多多提議。
getId:function(elemId){ return document.getElementById(elemId); }, getStyle:function(obj,attr){ return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj, false)[attr]; }, startMove:function(obj,json,fnEnd){ /* obj 代表需要運動的對象 json 需要運動的屬性的集合 例如{‘width‘:100,‘height‘:100} fnEnd 運動結束時的函數 */ var _this = this; clearInterval(obj.timer); //若物體有運動,先清除計時器 obj.timer = setInterval(function(){ var eStop = true; //表示物體運動 for(var attr in json){ var start = 0; if(attr == ‘opacity‘){ //如果是透明度的屬性 start = Math.round(Math.floor(_this.getStyle(obj,attr)*100)); }else{ start = parseInt(_this.getStyle(obj,attr)); } //如果 if(start != json[attr]){ eStop = false; } //設定速度的值 var speed = (json[attr] - start)/6 //如果大於0就向上取整,否則就向下取整; speed > 0 ? Math.ceil(speed) : Math.floor(speed); //如果透明度的變化 if(attr == ‘opacity‘){ obj.style.filter = ‘alpha(opacity:‘+ (start + speed) +‘)‘;//設定IE obj.style.opacity = (start+ speed)/100; }else{ //此時就是除了透明度的其他屬性 obj.style[attr] = start + speed + ‘px‘; } } //如果eStop == true 時 完成運動 到達目標點 清除計時器 if(eStop){ clearInterval(obj.timer); if(fnEnd) fnEnd(); } },30) }
上面是我封裝的三個方法:
調用如下
elemMouse:function(){ var _this = this; var conPhone = this.getId(‘conPhone‘); this.addEvent(conPhone,‘mouseover‘,function(){ _this.startMove(conPhone,{‘width‘:500,‘height‘:500,},function(){ alert(‘運動結束‘) }) }) this.addEvent(conPhone,‘mouseout‘,function(){ _this.startMove(conPhone,{‘width‘:300,‘height‘:50},function(){ alert(‘運動結束‘) }) }) }
JS運動架構