运动框架
2016-12-16 00:00
330 查看
运动框架演变过程
startMove(iTarget) 运动框架
startMove(obj, iTarget) 多物体
startMove(obj, attr, iTarget) 任意值
startMove(obj, attr, iTarget, fn) 链式运动
startMove(obj, json) 多值运动
startMove(obj, json, fn) 完美运动框架
运动框架
缓冲运动
完美运动
多物体的运动
startMove(iTarget) 运动框架
startMove(obj, iTarget) 多物体
startMove(obj, attr, iTarget) 任意值
startMove(obj, attr, iTarget, fn) 链式运动
startMove(obj, json) 多值运动
startMove(obj, json, fn) 完美运动框架
运动框架
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Examples</title> <meta name="description" content=""> <meta name="keywords" content=""> <style> * {margin: 0;padding: 0;} #test{width:100px;height:100px;background:red;position:absolute;} </style> <script> window.onload = function() { var oDiv1=document.getElementById('test'); oDiv1.onclick=function(){ this.timer=null; clearInterval(this.timer) this.timer=setInterval(function(){ if(oDiv1.offsetLeft==300){ clearInterval(this.timer) }else{ oDiv1.style.left=oDiv1.offsetLeft+5+"px"; } },30) } } </script> </head> <body> <div id="test"></div> </body> </html>
缓冲运动
function startMove(obj,target){ clearInterval(obj.timer) obj.timer=setInterval(function(){ var speed=(target-obj.offsetLeft)/5; speed=speed>0?Math.ceil(speed):Math.floor(speed); //缓冲运动的速度一定要取整 if(obj.offsetLeft==target){ //两点重合就清除定时器 clearInterval(obj.timer) }else{ obj.style.left=obj.offsetLeft+speed+"px"; } },30) }
完美运动
function getStyle(obj,attr){return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj,false)[attr]} //startMove(oDiv, {width: 400, height: 400}) function startMove(obj, json, fnEnd) { clearInterval(obj.timer); obj.timer=setInterval(function (){ var bStop=true; //假设:所有值都已经到了 for(var attr in json) { var cur=0; // 获取当前值 if(attr=='opacity') { cur=Math.round(parseFloat(getStyle(obj, attr))*100)||0; } else { cur=parseInt(getStyle(obj, attr))||0; } var speed=(json[attr]-cur)/6; //算速度 speed=speed>0?Math.ceil(speed):Math.floor(speed); if(cur!=json[attr]) //检测停止:如果当前的值不等于目标值就不清除定时器 bStop=false; if(attr=='opacity') { obj.style.filter='alpha(opacity:'+(cur+speed)+')'; obj.style.opacity=(cur+speed)/100; } else { obj.style[attr]=cur+speed+'px'; } } if(bStop) { clearInterval(obj.timer); fnEnd&&fnEnd.call(obj); } }, 30); }
多物体的运动
function startMove(obj, attr, iTarget, iSpeed) { clearInterval(obj.iTimer); var iCur = 0; obj.iTimer = setInterval(function() { if (attr == 'opacity') { iCur = Math.round(css( obj, 'opacity' ) * 100); } else { iCur = parseInt(css(obj, attr)); } if (iCur == iTarget) { clearInterval(obj.iTimer); } else { if (attr == 'opacity') { obj.style.opacity = (iCur + iSpeed) / 100; obj.style.filter = 'alpha(opacity='+ (iCur + iSpeed) +')'; } else { obj.style[attr] = iCur + iSpeed + 'px'; } } }, 30); } function css(obj, attr) { if (obj.currentStyle) { return obj.currentStyle[attr]; } else { return getComputedStyle(obj, false)[attr]; } }
相关文章推荐
- 事件委托
- centos6.2下搭建Web服务器
- 正则版----getByClass函数
- js上传图片并预览
- JS当前日期相加相减
- 100个直接可以拿来用的JavaScript实用功能代码片段
- @Autowired @Resource @Inject的区别
- HDU 5713 K个联通块【状压计数dp……补集转化?
- myeclipse tomcat的启动缓慢问题----一次会加载多个项目
- 内存介绍 - 继承
- java--Struts2入门示例教程(五)
- Java中Date插入数据库的一些问题总结(二)
- Java中数组的问题(三)
- 浏览器--如何让登陆页面的表单不默认显示账号和密码
- JS切换卡效果
- java项目HTTP Status 404(The requested resource is not avail)的问题
- Java 关于Ajax的实例--验证用户名(四)
- (四)基于myeclipse的RocketMQ--Demo实践
- Java中类与方法的学习笔记(一):
- 由数组随机化排序引出的对于js中sort()方法的理解分析