效果图
结构
复制代码
1
2
3<div></div> <span></span>
CSS
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17div { position: absolute; left: 0; width: 100px; height: 100px; background-color: pink; } span { position: absolute; left: 0; top: 100px; background-color: red; width: 100px; height: 100px; }
js
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18// 简单动画函数封装obj目标对象 target 目标位置 function animate (obj, target) { var timer = setInterval(function () { if (obj.offsetLeft >= target) { // 停止动画 本质是停止定时器 clearInterval(timer); } obj.style.left = obj.offsetLeft + 1 + 'px'; }, 30) } var div = document.querySelector('div'); var span = document.querySelector('span'); animate(div, 300); animate(span, 300);
最后
以上就是俊逸萝莉最近收集整理的关于JavaScript-231:简单动画函数封装的全部内容,更多相关JavaScript-231内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复