animate.css是一个不错CSS3预设动画库,是很好的CSS3动画学习资源,下面来剖析下bounce效果的实现原理,在此基础上实现自己的CSS动画。
先从animate.css把bounce效果单独移出来,暂不考虑针对不同的浏览器的支持,测试浏览器为chrome,看一下效果。
<html>
<head>
<title>bounce-sourcetitle>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1, minimal-ui" />
<style type="text/css">
html {
font: 100%/1.5 "Roboto", Verdana, sans-serif;
color: #3d464d;
background-color: #fff;
-webkit-font-smoothing: antialiased;
width: 100%;
overflow: hidden-x;
text-align: center;
}
.font-style{
color: #f35626;
background-image: -webkit-linear-gradient(92deg,#f35626,#feab3a);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
-webkit-animation: hue 10s infinite linear;
}
h1{
margin-bottom: 1.5rem;
font-size: 6rem;
font-weight: 100;
line-height: 1;
letter-spacing: -.05em;
}
@-webkit-keyframes hue {
from {
-webkit-filter: hue-rotate(0deg);
}
to {
-webkit-filter: hue-rotate(-360deg);
}
}
style>
<style type="text/css">
.animated {
animation-duration: 1s;
animation-fill-mode: both;
}
.animated.infinite {
animation-iteration-count: infinite;
}
.bounce {
animation-name: bounce;
transform-origin: center bottom;
}
@keyframes bounce {
from, 20%, 53%, 80%, to {
animation-timing-function: cubic-bezier(0.215, 0.610, 0.355, 1.000);
transform: translate3d(0,0,0);
}
40%, 43% {
animation-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060);
transform: translate3d(0, -30px, 0);
}
70% {
animation-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060);
transform: translate3d(0, -15px, 0);
}
90% {
transform: translate3d(0,-4px,0);
}
}
style>
head>
<body>
<header>
<div>
<span style="display: block;" class="bounce animated infinite"><h1 class="font-style">bounceh1>span>
div>
header>
body>
html>
看下一些公共的属性
基本属性定义完成后,看下动画执行的具体过程
@keyframes bounce {
from, 20%, 53%, 80%, to {
animation-timing-function: cubic-bezier(0.215, 0.610, 0.355, 1.000);
transform: translate3d(0,0,0);
}
40%, 43% {
animation-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060);
transform: translate3d(0, -30px, 0);
}
70% {
animation-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060);
transform: translate3d(0, -15px, 0);
}
90% {
transform: translate3d(0,-4px,0);
}
}
看起来不够直观,先按时间轴整理下
@keyframes bounce {
0%, 20%{
animation-timing-function: cubic-bezier(0.215, 0.610, 0.355, 1.000);
transform: translate3d(0,0,0);
}
40%, 43% {
animation-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060);
transform: translate3d(0, -30px, 0);
}
53%{
animation-timing-function: cubic-bezier(0.215, 0.610, 0.355, 1.000);
transform: translate3d(0,0,0);
}
70% {
animation-timing-function: cubic-bezier(0.755, 0.050, 0.855, 0.060);
transform: translate3d(0, -15px, 0);
}
80%{
animation-timing-function: cubic-bezier(0.215, 0.610, 0.355, 1.000);
transform: translate3d(0,0,0);
}
90% {
transform: translate3d(0,-4px,0);
}
100% {
animation-timing-function: cubic-bezier(0.215, 0.610, 0.355, 1.000);
transform: translate3d(0,0,0);
}
}
关于 贝塞尔曲线可以看下另一篇CSS3 三次贝塞尔曲线(cubic-bezier)
能否用ease-out与ease-in分别代替上升与下降的过程,完成4次弹跳过程。
@keyframes bounce {
0%, 15%, 47%, 73%,89%,100% {
animation-timing-function: ease-out;
transform: translate3d(0,0,0);
}
30%, 32% {
animation-timing-function: ease-in;
transform: translate3d(0, -42px, 0);
}
60% {
animation-timing-function: ease-in;
transform: translate3d(0, -24px, 0);
}
82% {
animation-timing-function: ease-in;
transform: translate3d(0, -12px, 0);
}
94% {
animation-timing-function: ease-in;
transform: translate3d(0,-3px,0);
}
}
<html>
<head>
<title>bounce-demotitle>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1, minimal-ui" />
<style type="text/css">
html {
font: 100%/1.5 "Roboto", Verdana, sans-serif;
color: #3d464d;
background-color: #fff;
-webkit-font-smoothing: antialiased;
width: 100%;
overflow: hidden-x;
text-align: center;
}
.font-style{
color: #f35626;
background-image: -webkit-linear-gradient(92deg,#f35626,#feab3a);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
-webkit-animation: hue 10s infinite linear;
}
h1{
margin-bottom: 1.5rem;
font-size: 6rem;
font-weight: 100;
line-height: 1;
letter-spacing: -.05em;
}
@-webkit-keyframes hue {
from {
-webkit-filter: hue-rotate(0deg);
}
to {
-webkit-filter: hue-rotate(-360deg);
}
}
style>
<style type="text/css">
.animated {
animation-duration: 1.5s;
animation-fill-mode: both;
}
.animated.infinite {
animation-iteration-count: infinite;
}
.bounce {
animation-name: bounce;
transform-origin: center bottom;
}
@keyframes bounce {
0%, 15%, 47%, 73%,89%,100% {
animation-timing-function: ease-out;
transform: translate3d(0,0,0);
}
30%, 32% {
animation-timing-function: ease-in;
transform: translate3d(0, -42px, 0);
}
60% {
animation-timing-function: ease-in;
transform: translate3d(0, -24px, 0);
}
82% {
animation-timing-function: ease-in;
transform: translate3d(0, -12px, 0);
}
94% {
animation-timing-function: ease-in;
transform: translate3d(0,-3px,0);
}
}
style>
head>
<body>
<header>
<div>
<span style="display: block;" class="bounce animated infinite"><h1 class="font-style">bounceh1>span>
div>
header>
body>
html>
附:animate.css动画库下载地址:https://daneden.github.io/animate.css/