当前位置:网站首页>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>
边栏推荐
猜你喜欢

Lingyunguang of Dachen and Xiaomi investment is listed: the market value is 15.3 billion, and the machine is implanted into the eyes and brain

存储过程的介绍与基本使用

数字ic设计——SPI

Aosikang biological sprint scientific innovation board of Hillhouse Investment: annual revenue of 450million yuan, lost cooperation with kangxinuo

Per capita Swiss number series, Swiss number 4 generation JS reverse analysis

Vscode编辑器ESP32头文件波浪线不跳转彻底解决

High end for 8 years, how is Yadi now?
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

Storage principle inside mongodb

滑轨步进电机调试(全国海洋航行器大赛)(STM32主控)
随机推荐
[untitled]
The difference between cache and buffer
ESP32系列专栏
regular expression
【学习笔记】zkw 线段树
单片机学习笔记之点亮led 灯
详细介绍六种开源协议(程序员须知)
1、深拷贝 2、call apply bind 3、for of for in 区别
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
Adopt a cow to sprint A shares: it plans to raise 1.85 billion yuan, and Xu Xiaobo holds nearly 40%
PACP学习笔记三:PCAP方法说明
[learning notes] zkw segment tree
Users, groups, and permissions
Realbasicvsr test pictures and videos
[dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours
一文读懂数仓中的pg_stat
如何让electorn打开的新窗口在window任务栏上面
TPG x AIDU|AI领军人才招募计划进行中!
cmake 学习使用笔记(一)