当前位置:网站首页>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)
}
边栏推荐
- 479. Additive binary tree (interval DP on the tree)
- Start from scratch - Web Host - 01
- Volume compression, decompression
- CSDN article underlined, font color changed, picture centered, 1 second to understand
- MySQL operates the database through the CMD command line, and the image cannot be found during the real machine debugging of fluent
- The middle element and the rightmost element of the shutter
- What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
- leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
- How to debug apps remotely and online?
- leetcode2309. The best English letters with both upper and lower case (simple, weekly)
猜你喜欢
MySQL如何解决delete大量数据后空间不释放的问题
Ar Augmented Reality applicable scenarios
Volume compression, decompression
leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
leetcode373. Find and minimum k-pair numbers (medium)
As a software testing engineer, will you choose the bank post? Laolao bank test post
Design and implementation of key value storage engine based on LSM tree
MySQL主从延迟问题怎么解决
Software No.1
How to use redis ordered collection
随机推荐
Pytest testing framework
Sword finger offer II 031 Least recently used cache
np. Where and torch Where usage
1222. Password dropping (interval DP, bracket matching)
Opengauss database backup and recovery guide
Comparative analysis of MVC, MVP and MVVM, source code analysis
Start from scratch - Web Host - 01
2022 Q2 - 提升技能的技巧总结
SQL server calculates the daily average and annual average of the whole province
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
Number of palindromes in C language (leetcode)
leetcode2312. 卖木头块(困难,周赛)
剑指 Offer 62. 圆圈中最后剩下的数字
leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
What are the necessary things for students to start school? Ranking list of Bluetooth headsets with good sound quality
How to use a product to promote "brand thrill"?
研发中台拆分过程的一些心得总结
Data analysis on the disaster of Titanic
Construction and maintenance of business websites [10]
Software development life cycle -- waterfall model