当前位置:网站首页>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)
}边栏推荐
- Automatically browse pinduoduo products
- JMeter (II) - install the custom thread groups plug-in
- leetcode373. 查找和最小的 K 对数字(中等)
- 剑指 Offer 31. 栈的压入、弹出序列
- how to add one row in the dataframe?
- 【毕业季】研究生学长分享怎样让本科更有意义
- leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
- 花一个星期时间呕心沥血整理出高频软件测试/自动化测试面试题和答案
- 研发中台拆分过程的一些心得总结
- leetcode2305. 公平分发饼干(中等,周赛,状压dp)
猜你喜欢

Data analysis on the disaster of Titanic

leetcode2310. 个位数字为 K 的整数之和(中等,周赛)

leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)

Number of palindromes in C language (leetcode)

How to solve MySQL master-slave delay problem

MySQL operates the database through the CMD command line, and the image cannot be found during the real machine debugging of fluent

Architecture evolution from MVC to DDD

Sword finger offer 62 The last remaining number in the circle

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

leetcode2309. The best English letters with both upper and lower case (simple, weekly)
随机推荐
Sword finger offer 42 Maximum sum of continuous subarrays
【带你学c带你飞】2day 第8章 指针(练习8.1 密码开锁)
Open that kind of construction document
essay structure
Construction and maintenance of business websites [12]
How to batch add background and transition effects to videos?
np.where 和 torch.where 用法
Ar Augmented Reality applicable scenarios
2022 Q2 - 提升技能的技巧总结
医药管理系统(大一下C语言课设)
剑指 Offer 29. 顺时针打印矩阵
RTL8189FS如何关闭Debug信息
leetcode2312. Selling wood blocks (difficult, weekly race)
What are the necessary things for students to start school? Ranking list of Bluetooth headsets with good sound quality
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
[question] - why is optical flow not good for static scenes
DNS domain name resolution
剑指 Offer 47. 礼物的最大价值
leetcode373. 查找和最小的 K 对数字(中等)
Based on configured schedule, the given trigger will never fire