当前位置:网站首页>JS slow animation
JS slow animation
2022-07-02 02:13:00 【MyDreamingCode】
One 、 Uniform animation
<body>
<button> Click Move </button>
<div></div>
<script>
var btn = document.querySelector('button');
var div = document.querySelector('div');
btn.addEventListener('click', function() {
clearInterval(div.timer || null);
div.timer = setInterval(function(){
if(div.offsetLeft == 400) {
clearInterval(div.timer);
}
div.style.left = div.offsetLeft + 5 + 'px';
}, 30)
})
</script>
</body>
Two 、 Slow motion animation
<body>
<button> Click Move </button>
<div></div>
<script>
var btn = document.querySelector('button');
var div = document.querySelector('div');
btn.addEventListener('click', function() {
clearInterval(div.timer || null);
div.timer = setInterval(function(){
var step = (400 - div.offsetLeft) / 10;
if(div.offsetLeft == 400) {
clearInterval(div.timer);
}
div.style.left = div.offsetLeft + step + 'px';
}, 30)
})
</script>
</body>
3、 ... and 、 Encapsulate animation
explain :1. obj For animated objects
2. destination To achieve the goal
3. callback Callback function called before the end of animation execution
function animate(obj, destination, callback) {
clearInterval(obj.timer || null);
obj.timer = setInterval(function(){
var step = (destination - obj.offsetLeft) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);
if(obj.offsetLeft == destination) {
if(callback) {
callback();
}
clearInterval(obj.timer);
}
obj.style.left = obj.offsetLeft + step + 'px';
}, 10)
}
边栏推荐
- leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
- Construction and maintenance of business websites [13]
- 附加:信息脱敏;
- 2022 Q2 - 提昇技能的技巧總結
- 剑指 Offer II 031. 最近最少使用缓存
- 剑指 Offer 62. 圆圈中最后剩下的数字
- Sword finger offer 62 The last remaining number in the circle
- 剑指 Offer 42. 连续子数组的最大和
- 大厂裁员潮不断,双非本科出身的我却逆风翻盘挺进阿里
- MySQL主从延迟问题怎么解决
猜你喜欢
leetcode373. 查找和最小的 K 对数字(中等)
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
leetcode2309. The best English letters with both upper and lower case (simple, weekly)
SQLite 3 of embedded database
leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
Sword finger offer 62 The last remaining number in the circle
MySQL constraints and multi table query example analysis
734. Energy stone (greed, backpack)
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
MySQL view concept, create view, view, modify view, delete view
随机推荐
Sword finger offer 42 Maximum sum of continuous subarrays
软件开发生命周期 --瀑布模型
leetcode2309. The best English letters with both upper and lower case (simple, weekly)
OpenCASCADE7.6编译
【读书笔记】程序员修炼手册—实战式学习最有效(项目驱动)
np. Where and torch Where usage
What is the MySQL column to row function
Exception handling of class C in yyds dry goods inventory
An analysis of circuit for quick understanding
Comparative analysis of MVC, MVP and MVVM, source code analysis
If you want to rewind the video picture, what simple methods can you use?
A quick understanding of digital electricity
Data analysis on the disaster of Titanic
Ar Augmented Reality applicable scenarios
What are the necessary things for students to start school? Ranking list of Bluetooth headsets with good sound quality
Selection of field types for creating tables in MySQL database
正则表达式学习笔记
How to solve MySQL master-slave delay problem
Construction and maintenance of business websites [13]
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享