当前位置:网站首页>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)
}边栏推荐
- Construction and maintenance of business websites [11]
- 【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
- What are the necessary things for students to start school? Ranking list of Bluetooth headsets with good sound quality
- how to come in an investnent bank team
- Post infiltration flow encryption
- Spend a week painstakingly sorting out the interview questions and answers of high-frequency software testing / automated testing
- Five skills of adding audio codec to embedded system
- MySQL view concept, create view, view, modify view, delete view
- Iterative unified writing method of binary tree
- 2022 Q2 - 提升技能的技巧总结
猜你喜欢

MySQL如何解决delete大量数据后空间不释放的问题

Sword finger offer 62 The last remaining number in the circle

Infix expression to suffix expression (computer) code

pytest 测试框架

【带你学c带你飞】1day 第2章 (练习2.2 求华氏温度 100°F 对应的摄氏温度

Word search applet design report based on cloud development +ppt+ project source code + demonstration video

【毕业季】研究生学长分享怎样让本科更有意义

Opencascade7.6 compilation

The concepts and differences between MySQL stored procedures and stored functions, as well as how to create them, the role of delimiter, the viewing, modification, deletion of stored procedures and fu

How to execute an SQL in MySQL
随机推荐
Flutter un élément au milieu, l'élément le plus à droite
How to use redis ordered collection
How to build and use redis environment
[question] - why is optical flow not good for static scenes
【带你学c带你飞】4day第2章 用C语言编写程序(练习 2.5 生成乘方表与阶乘表
essay structure
Cesium dynamic diffusion point effect
Additional: information desensitization;
How to turn off debug information in rtl8189fs
As a software testing engineer, will you choose the bank post? Laolao bank test post
Number of palindromes in C language (leetcode)
Using mongodb in laravel
Duplicate keys detected: ‘0‘. This may cause an update error. found in
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
C language 3-7 daffodils (enhanced version)
mysql列转行函数指的是什么
leetcode2312. Selling wood blocks (difficult, weekly race)
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
【带你学c带你飞】1day 第2章 (练习2.2 求华氏温度 100°F 对应的摄氏温度
Construction and maintenance of business websites [11]