当前位置:网站首页>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)
}边栏推荐
- How to build and use redis environment
- 跨域?同源?一次搞懂什么是跨域
- Construction and maintenance of business websites [15]
- 研发中台拆分过程的一些心得总结
- Word search applet design report based on cloud development +ppt+ project source code + demonstration video
- leetcode2310. The one digit number is the sum of integers of K (medium, weekly)
- 【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
- [技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
- Selection of field types for creating tables in MySQL database
- Additional: information desensitization;
猜你喜欢

JMeter (II) - install the custom thread groups plug-in

leetcode2312. Selling wood blocks (difficult, weekly race)

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

If you want to rewind the video picture, what simple methods can you use?

医药管理系统(大一下C语言课设)
![[question] - why is optical flow not good for static scenes](/img/8d/2cf6f582bc58cc2985f50e3f85f334.jpg)
[question] - why is optical flow not good for static scenes

1222. Password dropping (interval DP, bracket matching)

Software development life cycle -- waterfall model

leetcode2305. 公平分发饼干(中等,周赛,状压dp)
![[graduation season] graduate seniors share how to make undergraduate more meaningful](/img/03/9adc44476e87b2499aa0ebb11cb247.png)
[graduation season] graduate seniors share how to make undergraduate more meaningful
随机推荐
Software development life cycle -- waterfall model
Medical management system (C language course for freshmen)
开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
Construction and maintenance of business websites [15]
Construction and maintenance of business websites [13]
* and & symbols in C language
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
Based on configured schedule, the given trigger will never fire
Ar Augmented Reality applicable scenarios
跨域?同源?一次搞懂什么是跨域
Redis环境搭建和使用的方法
Based on configured schedule, the given trigger will never fire
花一个星期时间呕心沥血整理出高频软件测试/自动化测试面试题和答案
golang---锁
[deep learning] Infomap face clustering facecluster
JMeter (I) - download, installation and plug-in management
A quick understanding of analog electricity
MySQL view concept, create view, view, modify view, delete view
leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享