/** * @author 郑印 */ /** * 获取样式 * @param {object} obj * @param {object} attr */ function getstyle(obj,attr){ if(obj.currentstyle){ return obj.currentstyle[attr]; }else{ return getcomputedstyle(obj,false)[attr]; } } /** * 缓冲运动动画 * @param {object} obj 对象 * @param {object} json 运动目标值 * @param {object} speed 速度 * @param {object} fn 运动完成回调函数 */ function buffer(obj,json,speed,fn){ var speed = speed || 5; clearinterval(obj.t) //开始前关闭原有的定时器 obj.t=setinterval(function(){ var endmove=true; //设置运动停止初始值 for(var attr in json){ // icur 更新运动元素当前的属性值 if(attr=='opacity'){ //对透明度单独处理 var icur = parseint(parsefloat(getstyle(obj, attr))*100); }else{ //普通样式 var icur = parseint(getstyle(obj, attr)); } // 速度取整 var ispeed=(json[attr]-icur)/speed>0?math.ceil((json[attr]-icur)/speed):math.floor((json[attr]-icur)/speed); //检测是否到目标点 if(icur!=json[attr]){ endmove=false; } //设置样式,对透明度单独处理 if(attr=='opacity'){ obj.style.filter='alpha(opacity='+(icur+ispeed)+')'; obj.style.opacity=(icur+ispeed)/100; }else{ obj.style[attr]=icur+ispeed+'px'; } } //运动完成,关闭定时器 if(endmove){ clearinterval(obj.t) obj.t = null; if(fn){ //回调函数存在,调用回调函数,并把当前对象做为this fn.call(obj); } } },30); } /** * 弹性运动动画 * @param {object} obj 对象 * @param {object} json 运动目标值 * @param {object} way 是否从中点开始运动 * @param {object} fn 运动完成回调函数 */ function fiexible(obj,json,way,fn){ /*** 按坐标运动 ***/ if(way === true){ //检测left 与 top 是否都有值 if(typeof json.left !='undefined' && typeof json.top !='undefined'){ var x = math.floor(json.left + json.width/2); //计算x轴中心点 var y = math.floor(json.top + json.height/2); //计算y轴中心点 //设置初始的left 和 top 值 并让元素显示 obj.style.display = 'block'; obj.style.left = x-(parseint(getstyle(obj,'width'))/2) + 'px'; obj.style.top = y-(parseint(getstyle(obj,'height'))/2) + 'px'; //清除margin obj.style.margin = 0 + 'px'; } } var newjson = {} /*** 往参数中添加位置属性 用于设置元素的运动初始点 ***/ for(var arg in json){ newjson[arg] = [json[arg], parseint(getstyle(obj,arg))] //newjson[arg] = [运动目标点,运动初始点]; } var osite = {}; /** 添加单独的属性值 **/ for(var attr in newjson){ osite[attr] ={ispeed:0,cursite:newjson[attr][1],bstop:false}; //osite[attr] = {运动初始速度,运动当前值,判断是否完成运动依据}; } /** 运动开始前关闭本身的定时器 **/ clearinterval(obj.t); obj.t = setinterval(function(){ /*** 循环运动属性 ***/ for (var attr in newjson) { /** 运动状态 **/ osite[attr].bstop = false; // icur 更新运动元素当前的属性值 if(attr=='opacity'){ //对透明度单独处理 var icur = parseint(parsefloat(getstyle(obj, attr))*100); }else{ //普通样式 var icur = parseint(getstyle(obj, attr)); } osite[attr].ispeed += (newjson[attr][0] - icur) /5; //加速 osite[attr].ispeed *= 0.75; //磨擦 osite[attr].cursite += osite[attr].ispeed; //更新运动的当前位置 //运动停止条件 速度绝对值小于1 并且 当前值与目标值的差值的绝对值小于一 if (math.abs(osite[attr].ispeed) < 1 && math.abs(icur - newjson[attr][0]) < 1) { //设置样式,对透明度单独处理 if(attr=='opacity'){ obj.style.filter='alpha(opacity='+newjson[attr][0]+')'; obj.style.opacity=newjson[attr][0]/100; }else{ obj.style[attr] = newjson[attr][0] + 'px'; //设置到目标点 } osite[attr].bstop = true; //设置当前属性运动是否完成 } else { //更新运动对象的属性值 if(attr=='opacity'){ obj.style.filter='alpha(opacity='+osite[attr].cursite+')'; obj.style.opacity=osite[attr].cursite/100; }else{ obj.style[attr] = osite[attr].cursite + 'px'; } } } // 校验定时器停止 if(checkstop(osite)){ clearinterval(obj.t); if(fn){ fn.call(obj) } } }, 30); /** 校验运动是否完成 **/ function checkstop(osite){ for(var i in osite){ if(osite[i].bstop === false){ return osite[i].bstop; } } return true; } }