封装缓动动画函数增加回调函数
2018-03-29 15:53
393 查看
<!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8"> <style type="text/css"> #dv{ width:200px; height: 100px; background-color: gray; position: absolute; } </style> </head> <body> <input type="button" value="移动" id="btn" name=""> <div id="dv"></div> <script type="text/javascript"> var obtn=document.getElementById("btn"); var odv=document.getElementById("dv"); json={"width":200,"height":300,"top":150,"left":500}; json1={"width":100,"height":400,"top":350,"left":200}; obtn.onclick=function(){ animate(odv,json,function(){animate(odv,json1)}); }; function animate(element,json,fn){ clearInterval(element.timer); element.timer=setInterval( function(){ var flag=true; for(var attr in json){ var current=parseInt(getStyle(element,attr)); //这个取得的是字符串 需要parseInt var target=json[attr]; var step=(target-current)/10; step=step>0?Math.ceil(step):Math.floor(step); current+=step; element.style[attr]=current+"px"; if(current!=target){flag=false;} } if(flag){ clearInterval(element.timer); if(fn) {fn();} } },20); } function getStyle(element,attr){ return window.getComputedStyle?window.getComputedStyle(element,null)[attr]:element.currentStyle[attr]; //第一个谷歌火狐第二个IE } </script> </body> </html>
相关文章推荐
- 缓动动画原理--封装基本函数
- JavaScript封装缓动动画函数
- 封装缓动动画函数
- 封装函数动画 缓动动画
- 第59天:缓动动画封装函数
- JS封装缓动动画函数
- 用缓动函数模拟物理动画
- 004_JS封装简单动画函数
- js简单封装能向回调函数传入参数的事件处理函数
- 动画封装三:单条样式的缓动运动
- 使用缓动动画函数实现导航栏效果
- JS轮播图中缓动函数的封装
- 缓动函数requestAnimationFrame 更好的实现浏览器经动画
- 动画封装四:用json实现多条样式的缓动运动
- 封装简单动画函数-由简到完善
- 根据动画原理,初步封装运动函数 2
- IOS 动画设计(5)——用缓动函数实现物理动画效果
- 封装回调函数——为对象方法(Object Method,参数中带this指针的函数) 构造 普通函数(参数中无this指针的函数)形式 的入口
- js-匀速/缓动动画-简单封装
- scroll()+client()函数封装+返回当前样式+动画多属性框架封装-兼容写法