任意值运动框架
2013-03-18 16:31
134 查看
任意值运动框架思路:
1、任意值运动框架带有3个形参,第一个是obj对象,第二个是attr属性,第三个是iTarget对象运动的目标值。
2、清除当前对象的定时器 clearInterval(obj.timer)
3、设置定时器 obj.setInterval(function(){},30)
3.1、定义一个空的cur用来设置当前属性。
3.2、因为有时候是opacity透明度运动,所以就要用if else加一个判断
3.3、定义速度 var speed=(iTarget-cur)/6;
3.4、用三目运算符把速度向上取整和向下取整 speed=speed>0?Math.ceil(speed):Math.floor(speed);
3.5、用if else判断,如果对象的属性值已经等于目标值就清除定时器 clearInterval(obj.timer)
3.5.1、如果属性名为opacity就设置opacity的值
3.51.2、否则就设置其它的宽高值运动速度。
任意值运动框架代码:
任意值运动框架应用实例完整代码:
1、任意值运动框架带有3个形参,第一个是obj对象,第二个是attr属性,第三个是iTarget对象运动的目标值。
2、清除当前对象的定时器 clearInterval(obj.timer)
3、设置定时器 obj.setInterval(function(){},30)
3.1、定义一个空的cur用来设置当前属性。
3.2、因为有时候是opacity透明度运动,所以就要用if else加一个判断
if(attr=='opacity'){ cur=Math.round(parseFloat(getStyle(obj,attr))*100); //opacity要用parseFloat强制转换为浮点数才能起作用,因为浮点数习惯用整数表示所以乘以100。因为有些小数*100在计算机中会出现bug,会出现算不尽的数,所以要用Math.round()来四舍五入。 } else{ cur=parseInt(getStyle(obj,attr)); //getStyle获取样式 }
3.3、定义速度 var speed=(iTarget-cur)/6;
3.4、用三目运算符把速度向上取整和向下取整 speed=speed>0?Math.ceil(speed):Math.floor(speed);
3.5、用if else判断,如果对象的属性值已经等于目标值就清除定时器 clearInterval(obj.timer)
3.5.1、如果属性名为opacity就设置opacity的值
3.51.2、否则就设置其它的宽高值运动速度。
任意值运动框架代码:
function startMove(obj,attr,iTarget) //三个形参,第一个是对象obj,第二个是对象的属性名,第三个是对象运动的目标值 { clearInterval(obj.timer); //清除当前对象的定时器 obj.timer=setInterval(function(){ //定义当前对象的定时器 var cur=0; if(attr=='opacity') //如果attr属性是opacity时 { cur=Math.round(parseFloat(getStyle(obj,attr))*100); //opacity要用parseFloat强制转换为浮点数才能起作用,因为浮点数习惯用整数表示所以乘以100。因为有些小数*100在计算机中会出现bug,会出现算不尽的数,所以要用Math.round()来四舍五入。 } else { cur=parseInt(getStyle(obj,attr)); //把从对象获取到的属性值强制转换为整形 } var speed=(iTarget-cur)/6; speed=speed>0?Math.ceil(speed):Math.floor(speed); //用三目运算符,当速度大于0就向上取整,否则就向下取整,这样能避免缓冲运动出现bug if(cur==iTarget) { clearInterval(obj.timer); } else { if(attr=='opacity') { obj.style.filter='alpha(opacity:'+(cur+speed)+')'; //IE obj.style.opacity=(cur+speed)/100; } else { obj.style[attr]=cur+speed+'px'; } } },30); };
任意值运动框架应用实例完整代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>任意值运动框架</title> <style> div{margin:20px;} #div{width:200px;height:200px;background:red;} #div2{width:200px;height:200px;background:black;filter:alpha(opacity:30);opacity:0.3;} </style> <script> window.onload=function() { var oDiv=document.getElementById('div'); var oDiv2=document.getElementById('div2'); oDiv.onmouseover=function() { startMove(this,'height',100) }; oDiv.onmouseout=function() { startMove(this,'height',200) }; oDiv2.onmouseover=function() { startMove(this,'opacity',100) }; oDiv2.onmouseout=function() { startMove(this,'opacity',30) }; }; function getStyle(obj,name) { if(obj.currentStyle){ //获取对象属性的值 return obj.currentStyle[name]; } else{ //Chrome FireFox获取对象属性的值 return getComputedStyle(obj,false)[name]; } }; function startMove(obj,attr,iTarget) { clearInterval(obj.timer); //清除当前对象的定时器 obj.timer=setInterval(function(){ //定义当前对象的定时器 var cur=0; if(attr=='opacity'){ //如果attr属性是opacity时 cur=Math.round(parseFloat(getStyle(obj,attr))*100); //opacity要用parseFloat强制转换为浮点数才能起作用,因为浮点数习惯用整数表示所以乘以100。因为有些小数*100在计算机中会出现bug,会出现算不尽的数,所以要用Math.round()来四舍五入。 } else{ cur=parseInt(getStyle(obj,attr)); //把从对象获取到的属性值强制转换为整形 } var speed=(iTarget-cur)/6; speed=speed>0?Math.ceil(speed):Math.floor(speed); //用三目运算符,当速度大于0就向上取整,否则就向下取整,这样能避免缓冲运动出现bug if(cur==iTarget){ clearInterval(obj.timer); } else{ if(attr=='opacity'){ obj.style.filter='alpha(opacity:'+(cur+speed)+')'; //IE obj.style.opacity=(cur+speed)/100; document.getElementById('txt1').value=obj.style.opacity; } else{ obj.style[attr]=cur+speed+'px'; } } },30); }; </script> </head> <body> <div id="div"></div> <div id="div2"></div> <input type="text" id="txt1" /> </body> </html>
相关文章推荐
- js简单运动框架--2 多属性运动
- 完美运动框架全面剖析
- JavaScript完美运动框架进阶之旅
- javaScript简易运动框架封装——和派孔明
- JavaScript运动框架详解
- JavaScript运动框架 解决速度正负取整问题(一)
- javascript运动框架
- JaveScript之运动框架
- js 运动框架(一)匀速运动
- 2015.8.2js-19(完美运动框架)
- 运动框架
- 在IE8下通过运动框架调整透明度时注意事项
- JavaScript之运动框架简单例子
- js完美运动框架最终版
- js实现多物体运动框架并兼容各浏览器
- js运动框架 step by step
- JS 运动框架2 链式运动
- 封装多个属性的运动框架
- 链式运动框架
- JavaScript——创建运动框架