您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

JS封装animate运动框架的实例详解

2025/5/18 11:35:32发布22次查看
本文主要为大家带来一篇原生js封装animate运动框架的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能帮助到大家。
如下所示:
<!doctype html> <html> <head lang="en"> <meta charset="utf-8"> <title></title> <style> p { width: 100px; height: 100px; background-color: pink; position: absolute; left: 0; top: 50px; border-radius: 50%; } </style> </head> <body> <button id="btn200">200</button> <button id="btn400">400</button> <p id="box"></p> </body> </html> <script> var btn200 = document.getelementbyid("btn200"); var btn400 = document.getelementbyid("btn400"); var box = document.getelementbyid("box"); btn200.onclick = function() { animate(box,{width: 200, top: 100,left: 200,opacity:40,zindex:3},function(){alert("我来了")}); } btn400.onclick = function() { animate(box,{top:500,opacity:10}); } // 多个属性运动框架 添加回调函数 function animate(obj,json,fn) { // 给谁 json clearinterval(obj.timer); obj.timer = setinterval(function() { var flag = true; // 用来判断是否停止定时器 一定写到遍历的外面 for(var attr in json){ // attr 属性 json[attr] 值 //开始遍历 json // 计算步长 用 target 位置 减去当前的位置 除以 10 // console.log(attr); var current = 0; if(attr == "opacity") { current = math.round(parseint(getstyle(obj,attr)*100)) || 0; console.log(current); } else { current = parseint(getstyle(obj,attr)); // 数值 } // console.log(current); // 目标位置就是 属性值 var step = ( json[attr] - current) / 10; // 步长 用目标位置 - 现在的位置 / 10 step = step > 0 ? math.ceil(step) : math.floor(step); //判断透明度 if(attr == "opacity") // 判断用户有没有输入 opacity { if("opacity" in obj.style) // 判断 我们浏览器是否支持opacity { // obj.style.opacity obj.style.opacity = (current + step) /100; } else { // obj.style.filter = alpha(opacity = 30) obj.style.filter = "alpha(opacity = "+(current + step)* 10+")"; } } else if(attr == "zindex") { obj.style.zindex = json[attr]; } else { obj.style[attr] = current + step + "px" ; } if(current != json[attr]) // 只要其中一个不满足条件 就不应该停止定时器 这句一定遍历里面 { flag = false; } } if(flag) // 用于判断定时器的条件 { clearinterval(obj.timer); //alert("ok了"); if(fn) // 很简单 当定时器停止了。 动画就结束了 如果有回调,就应该执行回调 { fn(); // 函数名 + () 调用函数 执行函数 } } },30) } function getstyle(obj,attr) { // 谁的 那个属性 if(obj.currentstyle) // ie 等 { return obj.currentstyle[attr]; // 返回传递过来的某个属性 } else { return window.getcomputedstyle(obj,null)[attr]; // w3c 浏览器 } } </script>
相关推荐:
js实现jquery的animate()动画
jquery animate动画持续运动的实例
css动画制作——css animate
以上就是js封装animate运动框架的实例详解的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product