当前位置:网站首页>JS缓动动画原理教学(超细节)
JS缓动动画原理教学(超细节)
2022-07-07 11:11:00 【周百万.】
1、先写出html和css样式,
<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盒子移动到100</div>
<div class="gump">gump盒子移动到500</div>
效果如图所示:重要是学js技术,样式好看不好看无所谓啊~
2、接下来写js,我直接在下面写的,所以没用load
3、封装一个动画函数,添加点击事件,事件中调用动画函数,点击两个盒子实现渐变动画
4、解释超详细,有需要讨论的点评论即可~
<script>
var box = document.querySelector('.box');
var gump = document.querySelector('.gump');
// 函数封装一个渐变动画
function animate(obj, target) {
// 让函数中始终只有一个定时器
clearInterval(obj.timer);
obj.timer = window.setInterval(function() {
// 如果到达目标位置就停止(清除定时器)
if (obj.offsetLeft == target) {
clearInterval(obj.timer)
}
// 每调用一次定时器就重新计算一次step的长度,目标长度不变,现在的位置离目标距离越来越近
// 他们之间的差的绝对值也就越来越小,使得间隔时间内每一步的长度减小,实现渐慢效果
// 第一步:(500-100)/10 = 40 第二步:(500-140)/10 = 36 ......
var step = (target - obj.offsetLeft) / 10;
// 如果步长为正,取较大的整数;如果步长为负,取较小的额整数。目的都是取绝对值较大的
step > 0 ? step = Math.ceil((step)) : step = Math.floor(step);
// obj.style.left可读写,但只能获取行内的样式,所以用来”写” 数据极好
// obj.offsetLeft 只能读,但他能读行内样式,内嵌样式,外链样式,”读”数据极好
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>
边栏推荐
- Session
- 3D content generation based on nerf
- Awk of three swordsmen in text processing
- 初学XML
- MySQL importing SQL files and common commands
- File operation command
- Leetcode brush questions: binary tree 19 (merge binary tree)
- 2022 practice questions and mock examination of the third batch of Guangdong Provincial Safety Officer a certificate (main person in charge)
- Steps of building SSM framework
- [untitled]
猜你喜欢
Sequoia China completed the new phase of $9billion fund raising
【学习笔记】AGC010
Leetcode skimming: binary tree 25 (the nearest common ancestor of binary search tree)
3D content generation based on nerf
Smart cloud health listed: with a market value of HK $15billion, SIG Jingwei and Jingxin fund are shareholders
MySQL入门尝鲜
Go language learning notes - structure
红杉中国完成新一期90亿美元基金募集
[untitled]
Sed of three swordsmen in text processing
随机推荐
详解ThinkPHP支持的URL模式有四种普通模式、PATHINFO、REWRITE和兼容模式
About how appium closes apps (resolved)
Find ID value MySQL in string
【学习笔记】AGC010
MySQL导入SQL文件及常用命令
Cmu15445 (fall 2019) project 2 - hash table details
Initialization script
About the problem of APP flash back after appium starts the app - (solved)
2022-07-07 Daily: Ian Goodfellow, the inventor of Gan, officially joined deepmind
DrawerLayout禁止侧滑显示
centso7 openssl 报错Verify return code: 20 (unable to get local issuer certificate)
MySQL master-slave replication
[learn microservices from 0] [03] explore the microservice architecture
Day-24 UDP, regular expression
What are the benefits of ip2long?
Practical example of propeller easydl: automatic scratch recognition of industrial parts
2022 practice questions and mock examination of the third batch of Guangdong Provincial Safety Officer a certificate (main person in charge)
TPG x AIDU|AI领军人才招募计划进行中!
How to continue after handling chain interruption / sub chain error removed from scheduling
Enterprise custom form engine solution (XII) -- experience code directory structure