js的运动框架
1 /** 2 * Created by zhouhaiou on 2015/6/10. 3 */ 4 5 6 // 封装获取id的方法 7 function $(id){ 8 return document.getElementById(id); 9 } 10 11 // 封装获取class的方法 12 13 function getClass(className,parent){ 14 var oParent = parent ? $(parent) : document, 15 eles = [], 16 elements = oParent.getElementsByTagName('*'); 17 for(var i = 0 ; i<elements.length;i++){ 18 if(elements[i].className == className){ 19 eles.push(elements[i]); 20 } 21 } 22 return eles; 23 } 24 25 //获取最终样式 26 function getStyle(obj,attr){ 27 if(obj.currentStyle){ 28 return obj.currentStyle[attr]; 29 }else{ 30 return getComputedStyle(obj,false)[attr]; 31 } 32 } 33 34 function startMove(obj,json,fn){ 35 36 clearInterval(obj.timer); 37 obj.timer = setInterval(function(){ 38 39 var flag = true; //假设动作是否全部达到期望值 40 41 for(var attr in json) { 42 // 取当前值 43 var iCur = 0; 44 if (attr == 'opacity') { 45 iCur = Math.round(parseFloat(getStyle(obj, attr)) * 100) 46 } else { 47 iCur = parseInt(getStyle(obj, attr)); 48 } 49 50 // 速度 51 var speed = (json[attr] - iCur) / 8; 52 speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); 53 54 //判断是否结束 55 if (iCur != json[attr]) { 56 flag = false; 57 } 58 if (attr == 'opacity') { 59 obj.style.opacity = (iCur + speed) / 100; 60 obj.style.filter = 'alpha:(opacity=' + iCur + speed + ')'; 61 } else { 62 obj.style[attr] = iCur + speed + 'px'; 63 } 64 65 } 66 67 if(flag){ 68 console.log(0); 69 clearInterval(obj.timer); 70 if (fn != undefined) { 71 fn(); 72 } 73 } 74 },30) 75 }