封装变速函数:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> #dv{ width: 200px; height: 100px; background-color: #FF6600; position: absolute; left: 0; top: 0; margin-top: 40px; } </style> </head> <body> <input type="button" value="移动到400" id="btn"> <div id="dv"></div> <script src="common.js"></script> <script> my$("btn").onclick=function () { animate(my$("dv"),400); } //移动函数--变速 function animate(elment,target) { //第一步,清除定时器 clearInterval(elment.timeId); elment.timeId=setInterval(function () { //获取元素的当前位置 var current=elment.offsetLeft; //确定每一次走多少步 var step=(target-current)/10;//有正负 //step取整 step=step>0?Math.ceil(step):Math.floor(step); //走完一次后,当前位置发生改变 current+=step; elment.style.left=current+"px"; //判断是否到当前位置 if(target==current){ //清除定时器 clearInterval(elment.timeId); } console.log("目标位置:"+target+"当前位置"+current+"每次移动的步数"+step); },20); } </script> </body> </html>
变速动画函数封装增加任意一个属性:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> #dv{ width: 100px; height: 100px; background-color: pink; position: absolute; left: 100px; top: 0; margin-top: 40px; border: 1px solid red; } </style> </head> <body> <input type="button" value="显示内容" id="btn"> <div id="dv"></div> <script src="common.js"></script> <script> my$("btn").onclick=function () { animate(my$("dv"),"width",400); } function getStyle(element,attr) { if (getComputedStyle) { return window. getComputedStyle?getComputedStyle(element,null)[attr]:element.currentStyle[atrr]; } } function animate(elment,attr,target) { clearInterval(elment.timeId); elment.timeId=setInterval(function () { var current=parseInt(getStyle(elment,attr)); var step=(target-current)/10; step=step>0?Math.ceil(step):Math.floor(step); current+=step; elment.style[attr]=current+"px"; if(target==current){ clearInterval(elment.timeId); } console.log("目标位置:"+target+"当前位置"+current+"每次移动的步数"+step); },20); } </script> </body> </html>
变速动画函数封装增加任意多个属性:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> #dv{ width: 100px; height: 100px; background-color: pink; position: absolute; left: 100px; top: 0; margin-top: 40px; border: 1px solid red; } </style> </head> <body> <input type="button" value="显示内容" id="btn"> <div id="dv"></div> <script src="common.js"></script> <script> my$("btn").onclick=function () { animate(my$("dv"),{"width":300,"left":500}); } //获取任意元素的任意属性---带单位 function getStyle(element,attr) { if (getComputedStyle) { return window. getComputedStyle?getComputedStyle(element,null)[attr]:element.currentStyle[atrr]; } } function animate(elment,json) { //第一步:清除定时器 clearInterval(elment.timeId); elment.timeId=setInterval(function () { var flag=true;//默认所有的属性达到目标值 for (var attr in json) { var current=parseInt(getStyle(elment,attr));//获取数字类型,进行运算 //确认每次走多少步 var step=(json[attr]-current)/10;//有正负 //step取整 step=step>0?Math.ceil(step):Math.floor(step); //走完一次后当前位置发生变化 current+=step; elment.style[attr]=current+"px"; //判断是否到当前位置 if(json[attr]!=current){ flag=false; } } //清除定时器 if (flag){ clearInterval(elment.timeId); } console.log("目标位置:"+json[attr]+"当前位置"+current+"每次移动的步数"+step); },20); } </script> </body> </html>
变速函数增加多个属性和回调函数:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> #dv{ width: 100px; height: 100px; background-color: pink; position: absolute; left: 100px; top: 0; margin-top: 40px; border: 1px solid red; } </style> </head> <body> <input type="button" value="显示内容" id="btn"> <div id="dv"></div> <script src="common.js"></script> <script> my$("btn").onclick=function () { var json={"width":300,"left":500,"top":100,"height":200}; animate(my$("dv"),json,function () { animate(my$("dv"),{"width":50,"height":50,"left":50},function () { animate(my$("dv"),{"width":200,"left":300,"top":50}) }) }) } //获取任意元素的任意属性---带单位 function getStyle(element,attr) { if (getComputedStyle) { return window. getComputedStyle?getComputedStyle(element,null)[attr]:element.currentStyle[atrr]; } } function animate(elment,json,fn) { //第一步:清除定时器 clearInterval(elment.timeId); elment.timeId=setInterval(function () { var flag=true;//默认所有的属性达到目标值 for (var attr in json) { var current=parseInt(getStyle(elment,attr));//获取数字类型,进行运算 //确认每次走多少步 var step=(json[attr]-current)/10;//有正负 //step取整 step=step>0?Math.ceil(step):Math.floor(step); //走完一次后当前位置发生变化 current+=step; elment.style[attr]=current+"px"; //判断是否到当前位置 if(json[attr]!=current){ flag=false; } } //清除定时器 if (flag){ clearInterval(elment.timeId); if (fn) { fn(); } } console.log("目标位置:"+json[attr]+"当前位置"+current+"每次移动的步数"+step); },20); } </script> </body> </html>
变速动画函数封装增加任意多个属性和回调函数和z-index和opacity:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> #dv{ width: 100px; height: 100px; background-color: pink; position: absolute; margin-top: -20px; border: 1px solid red; z-index: -10; } </style> </head> <body> <input type="button" value="显示内容" id="btn"> <div id="dv"></div> <script src="common.js"></script> <script> my$("btn").onclick=function () { var json={"width":300,"left":500,"top":100,"height":200,"opacity":0.4}; animate(my$("dv"),json,function () { animate(my$("dv"),{"width":50,"height":50,"left":50},function () { animate(my$("dv"),{"width":200,"height":250,"top":-20,"left":-20,"zIndex":100}) }) }) } //获取任意元素的任意属性---带单位 function getStyle(element,attr) { return window. getComputedStyle?getComputedStyle(element,null)[attr]:element.currentStyle[attr]; } /** * 变速动画函数添加任意一个属性 * @param element 要移动的元素 * @param json {"attr1":target1,"attr2":target2} * @param fn 回调函数 * * "width":400, "height":300, "left":400 */ function animate(elment,json,fn) { //第一步:清除定时器 clearInterval(elment.timeId); elment.timeId=setInterval(function () { var flag = true;//默认所有的属性达到目标值 for (var attr in json) //判断是不是opacity if (attr == "opacity") { //获取当前元素的attr这个属性的属性值 var current = getStyle(elment, attr) * 100;//增大100倍 //确定每一次走多少步 var target = json[attr] * 100;//目标也增大100倍 var step = (target - current) / 10; //有正负 //step取整 step = step > 0 ? Math.ceil(step) : Math.floor(step); //走完一次后当前位置发生变化 current += step; elment.style[attr] = current / 100; //判断是否到当前位置 } else if (attr == "zIndex") {//判断是不是z-index //直接改变层级属性 elment.style[attr] = json[attr]; } else { //普通属性 //获取当前元素的attr这个属性的属性值 var current = parseInt(getStyle(elment, attr)); target = json[attr]; var step = (target - current) / 10; step = step > 0 ? Math.ceil(step) : Math.floor(step); current += step; elment.style[attr] = current + "px"; } if (target != current) { flag = false; } if (flag){ clearInterval(elment.timeId); if (fn){ fn(); } } console.log("目标位置:"+json[attr]+"当前位置"+current+"每次移动的步数"+step); },20); } </script> </body> </html>