简单的动画函数的封装
封装一个从当前位置移动到目标位置的动画
封装一个均速动画函数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21function animate(element,target){ //先清理定时器,这样可以保证每次点击按钮都只产生一个定时器 clearInterval(element.timeId); element.timeId=setInterval(function () { //获取div的当前位置 var current=element.offsetLeft; //设置每次移动多少像素 var step=10; step= current < target ?step:-step; //每次移动后的位置 current += step; //判断当前位置距离目标位置多少像素 if(Math.abs(current-target)>=Math.abs(step)){ element.style.left=current+"px"; }else{ clearInterval(element.timeId); element.style.left=target+"px"; } },10); }
例子:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0; padding: 0; } button{ margin-top: 20px; } #box{ width: 200px; height: 100px; background-color: greenyellow; margin-top: 20px; position:absolute; left: 20px; } </style> </head> <body> <button id="btn1">移动到400px</button> <button id="btn2">移动到800px</button> <div id="box"></div> <script> var box=document.getElementById("box"); document.getElementById("btn1").οnclick=function () { animate(box,400); }; document.getElementById("btn2").οnclick=function () { animate(box,800); }; function animate(element,target){ //先清理定时器,这样可以保证每次点击按钮都只产生一个定时器 clearInterval(element.timeId); element.timeId=setInterval(function () { //获取div的当前位置 var current=element.offsetLeft; //设置每次移动多少像素 var step=10; step= current < target ?step:-step; //每次移动后的位置 current += step; //判断当前位置距离目标位置多少像素 if(Math.abs(current-target)>=Math.abs(step)){ element.style.left=current+"px"; }else{ clearInterval(element.timeId); element.style.left=target+"px"; } },10); } </script> </body> </html>
封装一个变速(缓动)动画函数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20function animate(element,target){ //先清理定时器,这样可以保证每次点击按钮都只产生一个定时器 clearInterval(element.timeId); element.timeId=setInterval(function () { //获取div的当前位置 var current=element.offsetLeft; //设置每次移动多少像素 var step=(target-current)/10; step= step>0 ?Math.ceil(step):Math.floor(step); //每次移动后的位置 current += step; element.style.left=current+"px"; //判断如果到达目标位置 if(current==target){ //清理定时器 clearInterval(element.timeId); } },10); }
例子:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0; padding: 0; } button{ margin-top: 20px; } #box{ width: 200px; height: 100px; background-color: greenyellow; margin-top: 20px; position:absolute; left: 20px; } </style> </head> <body> <button id="btn1">移动到400px</button> <button id="btn2">移动到800px</button> <div id="box"></div> <script> var box=document.getElementById("box"); document.getElementById("btn1").οnclick=function () { animate(box,400); }; document.getElementById("btn2").οnclick=function () { animate(box,800); }; function animate(element,target){ //先清理定时器,这样可以保证每次点击按钮都只产生一个定时器 clearInterval(element.timeId); element.timeId=setInterval(function () { //获取div的当前位置 var current=element.offsetLeft; //设置每次移动多少像素 var step=(target-current)/10; step= step>0 ?Math.ceil(step):Math.floor(step); //每次移动后的位置 current += step; element.style.left=current+"px"; //判断如果到达目标位置 if(current==target){ //清理定时器 clearInterval(element.timeId); } // 测试代码 console.log("当前位置:"+current+",目标位置:"+target+",每次移动像素数:"+step); },10); } </script> </body> </html>
最后
以上就是妩媚龙猫最近收集整理的关于JavaScript封装动画函数(一)的全部内容,更多相关JavaScript封装动画函数(一)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复