[CSS3] 心地よいCSS3アニメーションを実装する
こんにちは、@yoheiMuneです。
最近githubで人気になっているtobiasahlin/SpinKitというレポジトリ(Starが2,500個も!)では、 CSS3アニメーションを使った心地よいアニメーションのサンプルが公開されています。
今回はそのレポジトリの内容を拝借して、各アニメーションの実装内容を学んで、自分も書けるようになろうというためのブログです。

特に2点目の奥行きの設定がポイントでしょうか。ここはノウハウ的な感じ。
簡単ではあるものの、上記1点を指定することで、綺麗なアニメーションになりますね。
ちょっとしたことに気をつけるといい感じのアニメーションが出来ますね。
いくつか他にも学び取れることがありました。
このようにスケール1倍でフェードアウトさせると綺麗でいい感じです。
こうすることで、keyframesの0%のスタイルを当てることが出来るという点、便利でメモメモです!
今後も積極的に人のコードを読んで、良いなと思ったものはブログで紹介したいと思います。
最後までご覧頂きましてありがとうございました。
最近githubで人気になっているtobiasahlin/SpinKitというレポジトリ(Starが2,500個も!)では、 CSS3アニメーションを使った心地よいアニメーションのサンプルが公開されています。
今回はそのレポジトリの内容を拝借して、各アニメーションの実装内容を学んで、自分も書けるようになろうというためのブログです。

SpinKitで公開されているCSSアニメーション
公開されているものは執筆時には全部で8個あり、http://tobiasahlin.com/spinkit/でデモが確認出来ます。 綺麗なものがいっぱいあって、個人的には好きです。 以下ではそれぞれのアニメーション内容を拝借して、実装内容を学びたいと思います。1、Rotating Plane
まずは、四角形がx軸やy軸でくるくる回っているアニメーションです。
<style>
.spinner1 {
width: 30px;
height: 30px;
background-color: #fff;
margin: 0 auto;
/*ポイント1:イーズの利用*/
-webkit-animation: rotateplane 1.2s infinite ease-in-out;
animation: rotateplane 1.2s infinite ease-in-out;
}
@-webkit-keyframes rotateplane {
/*ポイント2:perspectiveの指定*/
0% { -webkit-transform: perspective(120px) }
50% { -webkit-transform: perspective(120px) rotateY(180deg) }
100% { -webkit-transform: perspective(120px) rotateY(180deg) rotateX(180deg) }
}
@keyframes rotateplane {
0% { transform: perspective(120px) rotateX(0deg) rotateY(0deg) }
50% { transform: perspective(120px) rotateX(-180.1deg) rotateY(0deg) }
100% { transform: perspective(120px) rotateX(-180deg) rotateY(-179.9deg) }
}
</style>
<div class="spinner1"></div>
綺麗な回転アニメーションですね。綺麗さは以下のポイントから出ると思われます。- ease-in-outを指定して滑らかに
- 奥行き(perspective)を指定することで回転アニメーションを立体的に
特に2点目の奥行きの設定がポイントでしょうか。ここはノウハウ的な感じ。
2、Double Bounce
続いてはこちら、2つの円が拡大縮小しつつ透過を変化させることで、綺麗なアニメーションになっています。
<style>
.spinner2 {
width: 40px;
height: 40px;
position: relative;
margin: 0 auto;
}
.double-bounce1, .double-bounce2 {
width: 100%;
height: 100%;
border-radius: 50%;
background-color: white;
opacity: 0.85;
position: absolute;
top: 0;
left: 0;
-webkit-animation: bounce 2.0s infinite ease-in-out;
animation: bounce 2.0s infinite ease-in-out;
}
.double-bounce2 {
/*ポイント1:遅延時間*/
-webkit-animation-delay: -1.0s;
animation-delay: -1.0s;
}
@-webkit-keyframes bounce {
0%, 100% { -webkit-transform: scale(0.0) }
50% { -webkit-transform: scale(1.0) }
}
@keyframes bounce {
0%, 100% { transform: scale(0.0) }
50% { transform: scale(1.0) }
}
</style>
<div class="spinner2">
<div class="double-bounce1"></div>
<div class="double-bounce2"></div>
</div>
これは比較的簡単な実装ですね。ポイントは以下でしょうか。- 円2のアニメーション遅延に-1秒を指定して、アニメーションの時間差を出しつつ、アニメーション開始までのデフォルトの大きさとかを表示しないようにする
簡単ではあるものの、上記1点を指定することで、綺麗なアニメーションになりますね。
3、Wave
続いて並べられたレクトが順に拡大縮小するアニメーションです。
<style>
.spinner3 {
margin: 0 auto;
width: 50px;
height: 30px;
text-align: center;
font-size: 10px;
}
.spinner3 > div {
background-color: white;
height: 100%;
width: 6px;
display: inline-block;
-webkit-animation: stretchdelay 1.2s infinite ease-in-out;
animation: stretchdelay 1.2s infinite ease-in-out;
}
/*ポイント:以下のディレイの指定*/
.spinner3 .rect2 {
-webkit-animation-delay: -1.1s;
animation-delay: -1.1s;
}
.spinner3 .rect3 {
-webkit-animation-delay: -1.0s;
animation-delay: -1.0s;
}
.spinner3 .rect4 {
-webkit-animation-delay: -0.9s;
animation-delay: -0.9s;
}
.spinner3 .rect5 {
-webkit-animation-delay: -0.8s;
animation-delay: -0.8s;
}
@-webkit-keyframes stretchdelay {
0%, 40%, 100% { -webkit-transform: scaleY(0.4) }
20% { -webkit-transform: scaleY(1.0) }
}
@keyframes stretchdelay {
0%, 40%, 100% { transform: scaleY(0.4) }
20% { transform: scaleY(1.0) }
}
</style>
<div class="spinner3">
<div class="rect1"></div>
<div class="rect2"></div>
<div class="rect3"></div>
<div class="rect4"></div>
<div class="rect5"></div>
</div>
これも動きが気持ちいいですね。アニメーションのポイントは以下でしょうか。- ディレイの時間を等幅にしないことで、あえて動かない時間を作る
ちょっとしたことに気をつけるといい感じのアニメーションが出来ますね。
4、Wandering Cube
これはどうやって実装しているんだろうと気になったものの1つです。
<style>
.spinner4 {
margin: 0 auto;
width: 32px;
height: 32px;
position: relative;
}
.cube1, .cube2 {
background-color: white;
width: 10px;
height: 10px;
position: absolute;
top: 0;
left: 0;
-webkit-animation: cubemove 1.8s infinite ease-in-out;
animation: cubemove 1.8s infinite ease-in-out;
}
.cube2 {
-webkit-animation-delay: -0.9s;
animation-delay: -0.9s;
}
/*ポイント:移動、拡大縮小、回転を組み合わせる*/
@-webkit-keyframes cubemove {
25% { -webkit-transform: translateX(22px) rotate(-90deg) scale(0.5) }
50% { -webkit-transform: translateX(22px) translateY(22px) rotate(-180deg) }
75% { -webkit-transform: translateX(0px) translateY(22px) rotate(-270deg) scale(0.5) }
100% { -webkit-transform: rotate(-360deg) }
}
@keyframes cubemove {
25% {
transform: translateX(42px) rotate(-90deg) scale(0.5);
-webkit-transform: translateX(42px) rotate(-90deg) scale(0.5);
} 50% {
/* Hack to make FF rotate in the right direction */
transform: translateX(42px) translateY(42px) rotate(-179deg);
-webkit-transform: translateX(42px) translateY(42px) rotate(-179deg);
} 50.1% { /*ポイント:FFへの対応*/
transform: translateX(42px) translateY(42px) rotate(-180deg);
-webkit-transform: translateX(42px) translateY(42px) rotate(-180deg);
} 75% {
transform: translateX(0px) translateY(42px) rotate(-270deg) scale(0.5);
-webkit-transform: translateX(0px) translateY(42px) rotate(-270deg) scale(0.5);
} 100% {
transform: rotate(-360deg);
-webkit-transform: rotate(-360deg);
}
}
</style>
<div class="spinner4">
<div class="cube1"></div>
<div class="cube2"></div>
</div>
綺麗なアニメーションです。X軸Y軸を変えつつ、回転と拡大縮小も行うことでこの動きが実現されていました。いくつか他にも学び取れることがありました。
- Firefoxのためのハックがある。こんな問題があるんだと知りました。
- keyframesの書き方フォーマットが2種類あって、どっちも読み慣れているといいなと感じました。
5、Pulse
綺麗な消え方をするアニメーションです。
style>
.spinner5 {
width: 40px;
height: 40px;
margin: 0 auto;
background-color: white;
border-radius: 100%;
-webkit-animation: scaleout 1.0s infinite ease-in-out;
animation: scaleout 1.0s infinite ease-in-out;
}
@-webkit-keyframes scaleout {
0% { -webkit-transform: scale(0.0) }
100% {
-webkit-transform: scale(1.0);
opacity: 0;
}
}
@keyframes scaleout {
0% {
transform: scale(0.0);
-webkit-transform: scale(0.0);
} 100% {
transform: scale(1.0);
-webkit-transform: scale(1.0);
opacity: 0;
}
}
</style>
<div style="background-color:rgb(127, 140, 141); padding:20px; margin: 10px 0;">
<div class="spinner5"></div>
</div>
拡大するアニメーションの場合に、スケールを1倍以上にすることもありますが、それだとガビっちゃいます。このようにスケール1倍でフェードアウトさせると綺麗でいい感じです。
6、Chasing Dots
これもどうやって実装するんだ、と思ったアニメーションでした。
style>
.spinner6 {
margin: 0px auto;
width: 40px;
height: 40px;
position: relative;
text-align: center;
-webkit-animation: rotate 2.0s infinite linear;
animation: rotate 2.0s infinite linear;
}
.dot1, .dot2 {
width: 60%;
height: 60%;
display: inline-block;
position: absolute;
top: 0;
background-color: #fff;
border-radius: 100%;
-webkit-animation: bounce 2.0s infinite ease-in-out;
animation: bounce 2.0s infinite ease-in-out;
}
.dot2 {
top: auto;
bottom: 0px;
-webkit-animation-delay: -1.0s;
animation-delay: -1.0s;
}
/*ポイント:親Divの動き*/
@-webkit-keyframes rotate { 100% { -webkit-transform: rotate(360deg) }}
@keyframes rotate {
100% {
transform: rotate(360deg);
-webkit-transform: rotate(360deg);
}
}
/*ポイント:子Divの動き*/
@-webkit-keyframes bounce {
0%, 100% { -webkit-transform: scale(0.0) }
50% { -webkit-transform: scale(1.0) }
}
@keyframes bounce {
0%, 100% {
transform: scale(0.0);
-webkit-transform: scale(0.0);
} 50% {
transform: scale(1.0);
-webkit-transform: scale(1.0);
}
}
</style>
<div style="background-color:rgb(241, 196, 15); padding:20px; margin: 10px 0;">
<div class="spinner6">
<div class="dot1"></div>
<div class="dot2"></div>
</div>
</div>
これどんな風に実装するのかと思いきや、実装を見ると意外と簡単で驚きです。ポイントは以下でしょうか。- 親Divで回転の動きを付ける
- 子Div(2つ)で拡大縮小の動きを付ける
- 上記2つのアニメーションを組み合わせることで、いい感じのアニメーションに見せている
7、Three Bounce
これも綺麗なアニメーションです。でも実装は簡単な感じです。
<<style>
.spinner7 {
margin: 0 auto;
width: 70px;
text-align: center;
}
.spinner7 > div {
width: 18px;
height: 18px;
background-color: #fff;
border-radius: 100%;
display: inline-block;
-webkit-animation: bouncedelay 1.4s infinite ease-in-out;
animation: bouncedelay 1.4s infinite ease-in-out;
/* Prevent first frame from flickering when animation starts */
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
.spinner7 .bounce1 {
-webkit-animation-delay: -0.32s;
animation-delay: -0.32s;
}
.spinner7 .bounce2 {
-webkit-animation-delay: -0.16s;
animation-delay: -0.16s;
}
@-webkit-keyframes bouncedelay {
0%, 80%, 100% { -webkit-transform: scale(0.0) }
40% { -webkit-transform: scale(1.0) }
}
@keyframes bouncedelay {
0%, 80%, 100% {
transform: scale(0.0);
-webkit-transform: scale(0.0);
} 40% {
transform: scale(1.0);
-webkit-transform: scale(1.0);
}
}
</style>
<div style="background-color:rgb(211, 84, 0); padding:20px; margin: 10px 0;">
<div class="spinner7">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
CSSのインラインコメントにもありますが、「animation-fill-mode:both」を使って、初期状態の制御を行っていますね。こうすることで、keyframesの0%のスタイルを当てることが出来るという点、便利でメモメモです!
8、Circles
くるくると円を描くアニメーション、綺麗に作るのは難しそうです。delayの設定がいっぱいありますが、基本的には単純です。
<style>
.spinner8 {
margin: 0 auto;
width: 30px;
height: 30px;
position: relative;
}
.container1 > div, .container2 > div, .container3 > div {
width: 6px;
height: 6px;
background-color: #fff;
border-radius: 100%;
position: absolute;
-webkit-animation: bouncedelay 1.2s infinite ease-in-out;
animation: bouncedelay 1.2s infinite ease-in-out;
/* Prevent first frame from flickering when animation starts */
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
.spinner8 .spinner-container {
position: absolute;
width: 100%;
height: 100%;
}
/*ポイント:4点含んだコンテナを回転させて配置*/
.container2 {
-webkit-transform: rotateZ(45deg);
transform: rotateZ(45deg);
}
.container3 {
-webkit-transform: rotateZ(90deg);
transform: rotateZ(90deg);
}
.circle1 { top: 0; left: 0; }
.circle2 { top: 0; right: 0; }
.circle3 { right: 0; bottom: 0; }
.circle4 { left: 0; bottom: 0; }
.container2 .circle1 {
-webkit-animation-delay: -1.1s;
animation-delay: -1.1s;
}
.container3 .circle1 {
-webkit-animation-delay: -1.0s;
animation-delay: -1.0s;
}
.container1 .circle2 {
-webkit-animation-delay: -0.9s;
animation-delay: -0.9s;
}
.container2 .circle2 {
-webkit-animation-delay: -0.8s;
animation-delay: -0.8s;
}
.container3 .circle2 {
-webkit-animation-delay: -0.7s;
animation-delay: -0.7s;
}
.container1 .circle3 {
-webkit-animation-delay: -0.6s;
animation-delay: -0.6s;
}
.container2 .circle3 {
-webkit-animation-delay: -0.5s;
animation-delay: -0.5s;
}
.container3 .circle3 {
-webkit-animation-delay: -0.4s;
animation-delay: -0.4s;
}
.container1 .circle4 {
-webkit-animation-delay: -0.3s;
animation-delay: -0.3s;
}
.container2 .circle4 {
-webkit-animation-delay: -0.2s;
animation-delay: -0.2s;
}
.container3 .circle4 {
-webkit-animation-delay: -0.1s;
animation-delay: -0.1s;
}
@-webkit-keyframes bouncedelay {
0%, 80%, 100% { -webkit-transform: scale(0.0) }
40% { -webkit-transform: scale(1.0) }
}
@keyframes bouncedelay {
0%, 80%, 100% {
transform: scale(0.0);
-webkit-transform: scale(0.0);
} 40% {
transform: scale(1.0);
-webkit-transform: scale(1.0);
}
}
</style>
<div style="background-color:rgb(39, 174, 96); padding:20px; margin: 10px 0;">
<div class="spinner8">
<div class="spinner-container container1">
<div class="circle1"></div>
<div class="circle2"></div>
<div class="circle3"></div>
<div class="circle4"></div>
</div>
<div class="spinner-container container2">
<div class="circle1"></div>
<div class="circle2"></div>
<div class="circle3"></div>
<div class="circle4"></div>
</div>
<div class="spinner-container container3">
<div class="circle1"></div>
<div class="circle2"></div>
<div class="circle3"></div>
<div class="circle4"></div>
</div>
</div>
</div>
</div>
ポイントとしては以下のような点でしょうか。- 点の配置の仕方が面白いですね。4点ずつ保持するDivを用意して、90度ずつ回転させて配置させるとは。
最後に
今回はGithubの1レポジトリの内容を紹介するという記事でした。 コードが長い部分もありましたが、理解することでノウハウとして吸収出来るのがGithubの良い点の1つだと思ってます。今後も積極的に人のコードを読んで、良いなと思ったものはブログで紹介したいと思います。
最後までご覧頂きましてありがとうございました。






