【发布时间】:2017-08-13 21:36:28
【问题描述】:
我正在尝试创建一个@keyframe 动画,以从屏幕底部弹出一个框,然后让它从 HTML 文档正文的顶部反弹。我用来执行此操作的方法不允许您在动画期间更改速度,实习生不会使它看起来很逼真(参见下面的代码)。
我找到了answer 来回答我的问题,唯一的问题是我真的不明白cubic-bezier 是如何创建动画的。在我继续之前,我想了解代码在做什么,然后将其添加到我的项目中。所以,是的,我猜这个问题的标题不应该是“如何使用立方体贝塞尔曲线来改变关键帧动画的速度?”,而是 “立方体贝塞尔曲线是如何工作的以及它在this 实例中的工作方式”。如果有人能稍微解释一下,甚至给我一个简单的代码示例以供遵循和理解,我将非常感激。
注意:我查看了Mozila Developer Network 的这个属性,但仍然不完全理解它在@keyframe 动画中的工作原理。
document.querySelector('#button').addEventListener('click', function() {
document.querySelector('#square').className = 'bounce';
document.querySelector('#square').style.display = 'block';
});
document.querySelector('#square').addEventListener('animationend', function(e) {
if (e.animationName == 'animate-in') {
document.querySelector('#square').removeAttribute('class');
}
});
#button {
display: block;
position: absolute;
left: 75px;
}
#square {
display: none;
position: relative;
width: 50px;
height: 50px;
background-color: tomato;
animation-fill-mode: forwards;
}
.bounce {
animation: animate-in 1.5s;
}
@keyframes animate-in {
20%,
50%,
80%,
100% {
transform: translateY(0);
}
40% {
transform: translateY(-30px);
}
60% {
transform: translateY(-15px);
}
0% {
transform: translateY(100vh);
}
}
<input type="button" id="button" value="Run Animation">
<div id="square"></div>
【问题讨论】:
-
您可能想使用这个网站,该网站可视化三次贝塞尔曲线的实际工作原理:cubic-bezier.com
标签: css css-animations