1.简单动画函数
复制代码
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
61
62<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>动画函数</title> <style> div { position: absolute; top: 0; left: 0; width: 100px; height: 100px; background-color: #ccc; } span { position: absolute; top: 0; left: 0; display: block; width: 150px; height: 150px; background-color: #ccc; opacity: .3; } button { position: absolute; top: 200px; left: 200px; } </style> </head> <body> <div>我</div> <span>朱一龙</span> <button>获取朱一龙的追求</button> <script> function animate(obj, target, time) { //设置点击点击事件 多次点击 不断添加定时器 这个元素会越来越快 //解决方案: 让元素只执行一个定时器 //先清除以前的定时器 只保留一个定时器进行执行 clearInterval(obj.timer); obj.timer = setInterval(function() { if (obj.offsetLeft >= target) { clearInterval(obj.timer); } obj.style.left = obj.offsetLeft + 1 + 'px'; }, time) } var div = document.querySelector('div'); var span = document.querySelector('span'); var btn = document.querySelector('button'); animate(div, 300, 30); btn.addEventListener('click', function() { animate(span, 200, 40); }) </script> </body> </html>
注:
记得给盒子加定位!!!!!!!!!!!
obj.timer 给不同对象添加不同定时器,防止歧义
清除定时器防止元素多次获得定时器速度变快
2. 缓动效果原理
缓动动画就是让元素运动速度有所变化,最常见的是让速度慢慢停下来
思路:
- 让盒子每次移动的距离慢慢变小,速度就会慢慢落下来。
- 核心算法: (目标值 - 现在的位置) / 10 做为每次移动的距离步长
- 停止的条件是: 让当前盒子位置等于目标位置就停止定时器
- 注意步长值需要取整
复制代码
1
2
3
4
5
6//步长值 改为整数 不要出现小数问题 var step = (target - obj.offsetLeft) / 10; step = step > 0 ? Math.ceil(step) : Math.floor(step); obj.style.left = obj.offsetLeft + step + 'px';
3.添加回调函数
复制代码
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<script> function animate(obj, target, time, callback) { //设置点击点击事件 多次点击 不断添加定时器 这个元素会越来越快 //解决方案: 让元素只执行一个定时器 //先清除以前的定时器 只保留一个定时器进行执行 clearInterval(obj.timer); obj.timer = setInterval(function() { if (obj.offsetLeft >= target) { clearInterval(obj.timer); //回调函数写到定时器结束里 if (callback) { callback(); } } //步长值 改为整数 不要出现小数问题 var step = (target - obj.offsetLeft) / 10; step = step > 0 ? Math.ceil(step) : Math.floor(step); obj.style.left = obj.offsetLeft + step + 'px'; }, time) } var div = document.querySelector('div'); var span = document.querySelector('span'); var btn = document.querySelector('button'); animate(div, 300, 15); btn.addEventListener('click', function() { animate(span, 200, 15, function() { span.style.backgroundColor = 'pink'; }); }) </script>
4.将动画函数封装到js文件
复制代码
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
30function animate(obj, target, callback) { // console.log(callback); callback = function() {} 调用的时候 callback() // 先清除以前的定时器,只保留当前的一个定时器执行 clearInterval(obj.timer); obj.timer = setInterval(function() { // 步长值写到定时器的里面 // 把我们步长值改为整数 不要出现小数的问题 // var step = Math.ceil((target - obj.offsetLeft) / 10); var step = (target - obj.offsetLeft) / 10; step = step > 0 ? Math.ceil(step) : Math.floor(step); if (obj.offsetLeft == target) { // 停止动画 本质是停止定时器 clearInterval(obj.timer); // 回调函数写到定时器结束里面 // if (callback) { // // 调用函数 // callback(); // } callback && callback(); } // 把每次加1 这个步长值改为一个慢慢变小的值 步长公式:(目标值 - 现在的位置) / 10 obj.style.left = obj.offsetLeft + step + 'px'; }, 15); }
最后
以上就是无情冰淇淋最近收集整理的关于简单动画函数封装的全部内容,更多相关简单动画函数封装内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复