当前位置:网站首页>JS slow motion animation principle teaching (super detail)
JS slow motion animation principle teaching (super detail)
2022-07-07 13:21:00 【Zhou million】
1、 Write first html and css style ,
<style>
* {
margin: 0;
padding: 0;
}
.box {
position: absolute;
width: 100px;
height: 100px;
background-color: pink;
}
.ha {
position: absolute;
top: 300px;
left: 400px;
width: 200px;
height: 200px;
background-color: purple;
color: aliceblue;
}
body {
height: 1000px;
font-size: 20px;
}
</style>
</head>
<body>
<div class="box">gump Move the box to 100</div>
<div class="gump">gump Move the box to 500</div>The effect is shown in the figure : The important thing is to learn js technology , It doesn't matter whether the style is good-looking or not ~

2、 Next write js, I wrote it directly below , So it's no use load
3、 Encapsulate an animation function , Add click event , Call animation function in event , Click two boxes to realize gradient animation
4、 The explanation is super detailed , There are comments that need to be discussed ~
<script>
var box = document.querySelector('.box');
var gump = document.querySelector('.gump');
// Function encapsulates a gradient animation
function animate(obj, target) {
// Let there always be only one timer in the function
clearInterval(obj.timer);
obj.timer = window.setInterval(function() {
// If you reach the target position, stop ( Clear timer )
if (obj.offsetLeft == target) {
clearInterval(obj.timer)
}
// The timer is recalculated every time it is called step The length of , The target length remains unchanged , The current position is getting closer and closer to the target
// The absolute value of the difference between them is getting smaller and smaller , Reduce the length of each step in the interval , Realize the slowing effect
// First step :(500-100)/10 = 40 The second step :(500-140)/10 = 36 ......
var step = (target - obj.offsetLeft) / 10;
// If the step size is positive , Take a larger integer ; If the step size is negative , Take the smaller integer . The purpose is to take the larger absolute value
step > 0 ? step = Math.ceil((step)) : step = Math.floor(step);
// obj.style.left read-write , But you can only get the style in the line , So it's used to ” Write ” Excellent data
// obj.offsetLeft Only read , But he can read in-line patterns , embedded style , Outer chain style ,” read ” Excellent data
obj.style.left = obj.offsetLeft + step + 'px';
}, 20);
}var box1 = box.addEventListener('click', function() {
animate(gump, 100);})
var box2 = gump.addEventListener('click', function() {
animate(gump, 500);})
</script>
边栏推荐
- 单片机学习笔记之点亮led 灯
- [untitled]
- Read PG in data warehouse in one article_ stat
- My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
- Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
- 【学习笔记】AGC010
- 记一次 .NET 某新能源系统 线程疯涨 分析
- “新红旗杯”桌面应用创意大赛2022
- RealBasicVSR测试图片、视频
- MongoDB优化的几点原则
猜你喜欢

Storage principle inside mongodb

My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime

About how appium closes apps (resolved)

Introduce six open source protocols in detail (instructions for programmers)
![[learning notes] agc010](/img/2c/37f2537a4dadd84adacf3da5f1327a.png)
[learning notes] agc010
![[learning notes] zkw segment tree](/img/18/21f455a06e8629243fc5cf4df0044c.png)
[learning notes] zkw segment tree

DHCP 动态主机设置协议 分析

DETR介绍

10 张图打开 CPU 缓存一致性的大门

LIS 最长上升子序列问题(动态规划、贪心+二分)
随机推荐
学习突围2 - 关于高效学习的方法
User management summary of mongodb
How to reset Firefox browser
[learning notes] segment tree selection
DrawerLayout禁止侧滑显示
Differences between MySQL storage engine MyISAM and InnoDB
分屏bug 小记
DETR介绍
Digital IC Design SPI
php——laravel缓存cache
PCAP学习笔记二:pcap4j源码笔记
[untitled]
Pay close attention to the work of safety production and make every effort to ensure the safety of people's lives and property
Adopt a cow to sprint A shares: it plans to raise 1.85 billion yuan, and Xu Xiaobo holds nearly 40%
飞桨EasyDL实操范例:工业零件划痕自动识别
LIS 最长上升子序列问题(动态规划、贪心+二分)
Cmake learning and use notes (1)
JS determines whether an object is empty
Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
[etc.] what are the security objectives and implementation methods that cloud computing security expansion requires to focus on?