当前位置:网站首页>Animation function encapsulation (slow motion animation)
Animation function encapsulation (slow motion animation)
2022-07-21 02:15:00 【Taro paste】
Animation function encapsulation
How animation works
The core principle : By timer setInterval() Keep moving the box .
Implementation steps :
- Get the current position of the box
- Let the box add... To its current position 1 It's a moving distance
- Use the timer to repeat the operation
- Add an end timer condition
- Note that this element needs to add positioning , Can be used element.style.left
Animation functions record different timers for different elements
If multiple elements use this animation function , Every time var Declare timer . We can use different timers for different elements ( Use your own timer ).
The core principle : utilize JS It's a dynamic language , You can easily add properties to the current object .
function animate(obj, target) {
// When we keep clicking on the button , This element is going to get faster and faster , Because too many timers are on
// The solution is Let's say that the element has only one timer to execute
// Clear the previous timer first , Only the current timer is kept for execution
clearInterval(obj.timer);
obj.timer = setInterval(function() {
if (obj.offsetLeft >= target) {
// Stop Animation It's essentially a stop timer
clearInterval(obj.timer);
}
obj.style.left = obj.offsetLeft + 1 + 'px';
}, 30);
}
Effect display
Source code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style> .box {
display: none; position: absolute; left: 0; width: 100px; height: 100px; background-color: pink; } .bBox {
position: absolute; top: 180px; left: 0; width: 200px; height: 200px; background-color: plum; } </style>
</head>
<body>
<button class="btn500"> Click on 500</button>
<button class="btn800"> Click on 800</button>
<div class="box"></div>
<div class="bBox"></div>
<script> function animate(obj, target, callback) {
clearInterval(obj.timer); obj.timer = setInterval(function () {
var x = (target - obj.offsetLeft) / 10; x = x > 0 ? Math.ceil(x) : Math.floor(x); if (obj.offsetLeft == target) {
clearInterval(obj.timer); if (callback) {
callback(); } } obj.style.left = obj.offsetLeft + x + 'px'; }, 15) }; var bBox = document.querySelector('.bBox'); var btn500 = document.querySelector('.btn500'); var btn800 = document.querySelector('.btn800'); btn500.addEventListener('click', function () {
animate(bBox, 500); }) btn800.addEventListener('click', function () {
animate(bBox, 800, function () {
bBox.style.backgroundColor = 'pink'; // console.log(bBox.style.backgroundColor); }); }) </script>
</body>
</html>
边栏推荐
- 【培训课程专用】cpu_context的初始化以切换
- 简略break、continue、return的区别
- [special for training courses] CPU_ Initialization of context to switch
- 百度飞浆paddle应用之在嵌入式ARM上运行
- Leetcode sword finger offer 32 - III. print binary tree III from top to bottom
- RedHat 7 network service cannot start. The problem ("device does not see to be present, delaying initialization") is handled
- C language binary tree + queue to realize hierarchical traversal
- Responsive dream weaving template lock cylinder website
- Activiti7 workflow and Alibaba components, second office OA, information management, ERP, etc
- 从三翼鸟,透视家居品牌的“飞轮效应”
猜你喜欢
redis分布式锁+线程池
[special for training courses] CPU_ Initialization of context to switch
分享一个好玩的JS小游戏
Daily question 1: numbers that appear more than half of the time in the array (Sword finger offer39)
ROS (sub, pub) test plotjuggler
[pygame Learning notes] 8. Elfe.
[special for training courses] Introduction to tee components
(PC+WAP)织梦模板会计服务类网站
JASMINER X4 became popular overseas and was praised by many well-known bloggers
Restore the password parameter of a website login interface
随机推荐
int类型转为double
[pygame Learning notes] 8. Elfe.
Caffeine缓存增改查过期
简略break、continue、return的区别
【培训课程专用】Storage API的介绍
索引什么时候失效
Mysql -分区列(横向分区 纵向分区)
scala Breaks.break()、Breaks.breakable()、控制抽象
正则表达式
(PC+WAP)织梦模板会计服务类网站
To clarify the tax arrears: there is no tax arrears, and will continue to operate in compliance, rooted in China
V4l2 learning notes
What do 1U, 2U and 4U of the server mean?
自定义MVC增查
《教程》torchtext的应用
Leetcode sword finger offer 11 Rotate the minimum number of the array
创建线程的方法
BOM浏览器对象模型(上篇)——概述、window对象常见事件、JS执行机制(5秒后关闭广告、倒计时案例、发送短信倒计时案例)
【培训课程专用】Trustzone--配置中断的安全属性
MySQL transaction management