动画实现原理
核心原理:通过定时器setInterval()不断移动盒子位置
实现步骤:
1. 获得盒子当前位置
2. 让盒子在当前位置加上1个移动距离
3. 利用定时器不断重复这个操作
4. 加一个结束定时器的条件
5. 注意此元素需要添加定位, 才能使用element.style.left
复制代码
1
2
3
4
5
6
7
8
9
10<script> var div = document.querySelector('div'); var timer = setInterval(function() { if (div.offsetLeft >= 400) { // 停止动画 本质是停止定时器 clearInterval(timer); } div.style.left = div.offsetLeft + 1 + 'px'; }, 30); </script>
代码优化:
复制代码
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
63
64
65
66<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> div { position: absolute; left: 0; width: 100px; height: 100px; background-color: pink; } span { position: absolute; left: 0; top: 200px; display: block; width: 150px; height: 150px; background-color: purple; } </style> </head> <body> <button>点击夏雨荷才走</button> <div></div> <span>夏雨荷</span> <script> // var obj = {}; // obj.name = 'andy'; // 简单动画函数封装obj目标对象 target 目标位置 // 给不同的元素指定了不同的定时器 function animate(obj, target) { // 当我们不断的点击按钮,这个元素的速度会越来越快,因为开启了太多的定时器 // 解决方案就是 让我们元素只有一个定时器执行 // 先清除以前的定时器,只保留当前的一个定时器执行 clearInterval(obj.timer); // 当每次用var timer声明定时器的时候,就会在内存开辟新的空间,浪费内存资源;并且调用的时候,定时器的名字都为timer,容易引起歧义。因此利用给对象添加属性的方法,给定时器声明。 obj.timer = setInterval(function() { if (obj.offsetLeft >= target) { // 停止动画 本质是停止定时器 clearInterval(obj.timer); } obj.style.left = obj.offsetLeft + 1 + 'px'; }, 30); } var div = document.querySelector('div'); var span = document.querySelector('span'); var btn = document.querySelector('button'); // 调用函数 animate(div, 300); btn.addEventListener('click', function() { animate(span, 200); }) </script> </body> </html>
缓动效果原理
缓动动画就是让元素运动速度有所变化,最常见的就是让速度慢慢停下来
思路:
1.让盒子每次运动的距离都慢慢变小,速度就会慢慢的落下来
2.核心算法:(目标值:需要移动的总长度-现在的位置)/10 作为每次运动的距离 步长(10可以修改)
3.停止条件:当当前盒子位置等于目标位置就停止定时器
4.注意步长取整,否则存在精度问题
复制代码
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<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> div { position: absolute; left: 0; width: 100px; height: 100px; background-color: pink; } span { position: absolute; left: 0; top: 200px; display: block; width: 150px; height: 150px; background-color: purple; } </style> </head> <body> <button>点击夏雨荷才走</button> <div></div> <span>夏雨荷</span> <script> function animate(obj, target) { clearInterval(obj.timer); obj.timer = setInterval(function () { // 定义一个 步长 =(目标值-现在的位置)/10 obj.step = (target - obj.offsetLeft) / 10; if (obj.offsetLeft == target) { // 停止动画 本质是停止定时器 clearInterval(obj.timer); } obj.style.left = obj.offsetLeft + obj.step + 'px'; }, 15); } var div = document.querySelector('div'); var span = document.querySelector('span'); var btn = document.querySelector('button'); // 调用函数 animate(div, 300); btn.addEventListener('click', function () { animate(span, 200); }) // 匀速动画 就是 盒子是当前的位置 + 固定的值 10 // 缓动动画就是 盒子当前的位置 + 变化的值(目标值 - 现在的位置) / 10) </script> </body> </html>
缓动动画在多个目标位置之间移动
复制代码
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<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> span { position: absolute; left: 0; top: 200px; display: block; width: 150px; height: 150px; background-color: purple; } </style> </head> <body> <button class="btn500">点击夏雨荷走500px</button> <button class="btn800">点击夏雨荷走800px</button> <span>夏雨荷</span> <script> function animate(obj, target) { clearInterval(obj.timer); obj.timer = setInterval(function () { // 定义一个 步长 =(目标值-现在的位置)/10 // 步长需要取整,否则存在精度问题 当盒子往前移动时 需要向上取整 当盒子往后移动时 需要向下取整(盒子在行走的过程中,不要回退取整) obj.step = target - obj.offsetLeft >= 0 ? Math.ceil((target - obj.offsetLeft) / 10) : Math.floor((target - obj.offsetLeft) / 10); if (obj.offsetLeft == target) { // 停止动画 本质是停止定时器 clearInterval(obj.timer); } obj.style.left = obj.offsetLeft + obj.step + 'px'; }, 15); } var span = document.querySelector('span'); var btn500 = document.querySelector('.btn500'); var btn800 = document.querySelector('.btn800'); // 调用函数 btn500.addEventListener('click', function () { animate(span, 500); }); btn800.addEventListener('click', function () { animate(span, 800); }); </script> </body> </html>
动画函数添加回调函数
回调函数原理:函数可以作为一个参数,将这个函数作为参数传到另一个函数里面,当那个函数执行完成之后,在执行这个传进去的这个函数,这个过程就叫做回调。
案例:引入自己封装的动画函数,实现滑动栏效果
JS:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15function animate(obj, target, callback) { clearInterval(obj.timer); obj.timer = setInterval(function () { obj.step = target - obj.offsetLeft >= 0 ? Math.ceil((target - obj.offsetLeft) / 10) : Math.floor((target - obj.offsetLeft) / 10); if (obj.offsetLeft === target) { // 停止动画 本质是停止定时器 clearInterval(obj.timer); // 停止动画后 判断是否有回调函数 如果有就执行 if (callback) { callback(); // 函数调用记得加 () } } obj.style.left = obj.offsetLeft + obj.step + 'px'; }, 15); }
HTML:
复制代码
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> .sliderbar { position: fixed; right: 0; bottom: 100px; width: 40px; height: 40px; text-align: center; line-height: 40px; cursor: pointer; color: #fff; } .con { position: absolute; left: 0; top: 0; width: 200px; height: 40px; background-color: purple; z-index: -1; } </style> <!-- 引入animate.js 文件--> <script src="animate.js"></script> </head> <body> <div class="sliderbar"> <span>←</span> <div class="con">问题反馈</div> </div> <script> // 获取元素 当鼠标经过 sliderbar 时,con往右滑动出来,鼠标离开,收回去 var sliderbar = document.querySelector('.sliderbar'); var con = document.querySelector('.con'); var span=document.querySelector('span'); // con 需要滑动的目标距离=con盒子的宽度-sliderbar盒子的宽度 var distance = con.offsetWidth - sliderbar.offsetWidth; // 鼠标经过 sliderbar.addEventListener('mouseenter', function () { // 执行回调函数 当 con 被拉出来的时候 span 里面的箭头 改为 → animate(con, -distance,function () { span.innerHTML='→'; }); }); // 鼠标离开 sliderbar.addEventListener('mouseleave', function () { animate(con, 0,function () { span.innerHTML='←'; }); }) </script> </body> </html>
最后
以上就是野性早晨最近收集整理的关于前端学习笔记-JS-BOM-动画函数封装动画实现原理的全部内容,更多相关前端学习笔记-JS-BOM-动画函数封装动画实现原理内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复