当前位置:网站首页>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)
}边栏推荐
- 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
- 分卷压缩,解压
- How to execute an SQL in MySQL
- JMeter (I) - download, installation and plug-in management
- RTL8189FS如何关闭Debug信息
- new和malloc的区别
- 【C#】使用正则校验内容
- Flutter un élément au milieu, l'élément le plus à droite
- 大厂裁员潮不断,双非本科出身的我却逆风翻盘挺进阿里
- leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
猜你喜欢

Software development life cycle -- waterfall model

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

【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享

Leetcode face T10 (1-9) array, ByteDance interview sharing

Number of palindromes in C language (leetcode)

附加:信息脱敏;

研发中台拆分过程的一些心得总结
![[opencv] - comprehensive examples of five image filters](/img/c7/aec9f2e03a17c22030d7813dd47c48.png)
[opencv] - comprehensive examples of five image filters

Cesium dynamic diffusion point effect
随机推荐
WebGPU(一):基本概念
SQLite 3 of embedded database
Opengauss database backup and recovery guide
Golang lock
np. Where and torch Where usage
An analysis of circuit for quick understanding
oracle创建只读权限的用户简单四步走
Open that kind of construction document
CSDN article underlined, font color changed, picture centered, 1 second to understand
Sword finger offer II 031 Least recently used cache
What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
Opencascade7.6 compilation
* and & symbols in C language
Software development life cycle -- waterfall model
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
STM32F103——两路PWM控制电机
How to solve MySQL master-slave delay problem
SQL server calculates the daily average and annual average of the whole province
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
研发中台拆分过程的一些心得总结