当前位置:网站首页>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)
}边栏推荐
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
- Infix expression to suffix expression (computer) code
- How to build and use redis environment
- flutter 中間一個元素,最右邊一個元素
- 附加:信息脱敏;
- leetcode2310. The one digit number is the sum of integers of K (medium, weekly)
- MySQL如何解决delete大量数据后空间不释放的问题
- Design and implementation of key value storage engine based on LSM tree
- leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
- "C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure
猜你喜欢

Selection of field types for creating tables in MySQL database

mysql列转行函数指的是什么

剑指 Offer 62. 圆圈中最后剩下的数字

Design and implementation of key value storage engine based on LSM tree

WebGPU(一):基本概念

如何用一款产品推动「品牌的惊险一跃」?

This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable

【读书笔记】程序员修炼手册—实战式学习最有效(项目驱动)

How to batch add background and transition effects to videos?

321. Chessboard segmentation (2D interval DP)
随机推荐
【带你学c带你飞】1day 第2章 (练习2.2 求华氏温度 100°F 对应的摄氏温度
Based on configured schedule, the given trigger will never fire
[graduation season] graduate seniors share how to make undergraduate more meaningful
Redis环境搭建和使用的方法
JMeter (I) - download, installation and plug-in management
Comparative analysis of MVC, MVP and MVVM, source code analysis
What are the necessary things for students to start school? Ranking list of Bluetooth headsets with good sound quality
Ar Augmented Reality applicable scenarios
The wave of layoffs in big factories continues, but I, who was born in both non undergraduate schools, turned against the wind and entered Alibaba
Post infiltration flow encryption
Regular expression learning notes
321. Chessboard segmentation (2D interval DP)
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
Data analysis on the disaster of Titanic
AR增强现实可应用的场景
SQLite 3 of embedded database
How to use redis ordered collection
CSDN insertion directory in 1 second
[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology
Spend a week painstakingly sorting out the interview questions and answers of high-frequency software testing / automated testing