【CSS】animation プロパティの基本設定と、挙動の違いがわかる比較デモ
Webサイトに動きをつけて、ユーザーの目を引いたり使いやすさを向上させたりできる「CSSアニメーション」。 非常に強力な機能ですが、設定項目(プロパティ)が多く、「どうしてこの動きになるの?」「アニメーションが終わった後に元の状態に戻ってしまうのはなぜ?」と混乱してしまうことも少なくありません。
今回は、アニメーションを構成する基本の項目を解説し、タイミング(timing-function)、方向(direction)、終了後の状態(fill-mode)などの違いがボタン1つで切り替わる、比較デモをご用意しました!
デモ動作
See the Pen CSS animationプロパティ基本&挙動比較 by mikanbako (@mikanbako) on CodePen.
demo: CSS animationプロパティ基本&挙動比較
実装コード紹介
【HTML】
<div class="l-wrap">
<h2>CSS animation 比較デモ</h2>
<p class="p-desc">ボタンを押すと、パラメータに合わせたアニメーションが発火します!</p>
<div class="p-demo-box">
<!-- アニメーションする対象の要素 -->
<div class="c-stage">
<div class="c-ball js-ball">ANIME</div>
</div>
<div class="c-code-display js-code-display">animation: 設定値;</div>
<!-- 設定の切り替えボタン群 -->
<div class="p-controls">
<!-- 💡 1. 速度変化 (timing-function) -->
<div class="c-control-group">
<p class="c-group-title">1. タイミング (timing-function)</p>
<button class="c-btn js-btn" data-class="u-ease" data-code="animation: slide 1.5s ease;">ease (標準)</button>
<button class="c-btn js-btn" data-class="u-linear" data-code="animation: slide 1.5s linear;">linear (等速)</button>
<button class="c-btn js-btn" data-class="u-ease-in" data-code="animation: slide 1.5s ease-in;">ease-in</button>
<button class="c-btn js-btn" data-class="u-ease-out" data-code="animation: slide 1.5s ease-out;">ease-out</button>
<button class="c-btn js-btn" data-class="u-ease-in-out" data-code="animation: slide 1.5s ease-in-out;">ease-in-out</button>
</div>
<!-- 💡 2. アニメーションの方向 (direction) -->
<div class="c-control-group">
<p class="c-group-title">2. 再生方向 (direction)</p>
<button class="c-btn js-btn" data-class="u-normal" data-code="animation: slide 1.5s ease normal;">normal (通常通り)</button>
<button class="c-btn js-btn" data-class="u-reverse" data-code="animation: slide 1.5s ease reverse;">reverse (逆再生)</button>
<button class="c-btn js-btn" data-class="u-alternate" data-code="animation: slide 1.5s ease alternate 2;">alternate (往復)</button>
</div>
<!-- 💡 3. 終了後の状態 (fill-mode) -->
<div class="c-control-group">
<p class="c-group-title">3. 終了後の状態 (fill-mode)</p>
<button class="c-btn js-btn" data-class="u-none" data-code="animation: slide 1.5s ease 1s none;">none (開始前に戻る)</button>
<button class="c-btn js-btn" data-class="u-forwards" data-code="animation: slide 1.5s ease 1s forwards;">forwards (最後で停止)</button>
<button class="c-btn js-btn" data-class="u-backwards" data-code="animation: slide 1.5s ease 1s backwards;">backwards (1秒遅延時・開始前に最初の形へ)</button>
</div>
</div>
</div>
</div>
【CSS】
/* ベース装飾 */
.l-wrap {
padding: 40px 20px;
max-width: 650px;
margin: 0 auto;
color: #3e3e3e;
text-align: center;
}
h2 {
margin-bottom: 10px;
font-size: 16px;
color: #1bb4d4;
}
.p-desc {
margin-bottom: 30px;
font-size: 16px;
font-weight: bold;
color: #666;
line-height: 1.6;
}
.p-demo-box {
padding: 20px;
background: #fff;
border-radius: 12px;
box-shadow: 0 4px 15px rgb(0 0 0 / 5%);
}
/* アニメーションのステージ */
.c-stage {
position: relative;
height: 120px;
margin-bottom: 20px;
overflow: hidden;
background: #f0f0f0;
border: 2px dashed #ddd;
border-radius: 8px;
}
.c-ball {
position: absolute;
top: 25px;
left: 20px;
display: flex;
align-items: center;
justify-content: center;
width: 70px;
height: 70px;
font-size: 14px;
font-weight: bold;
color: #fff;
background: #e83e8c;
border-radius: 50%;
box-shadow: 0 4px 10px rgb(232 62 140 / 30%);
}
.c-code-display {
margin-bottom: 20px;
font-family: monospace;
font-size: 16px;
font-weight: bold;
}
/* コントロールエリア */
.p-controls {
display: flex;
flex-direction: column;
gap: 20px;
text-align: left;
}
.c-control-group {
padding-top: 15px;
border-top: 1px solid #eee;
}
.c-group-title {
margin-bottom: 10px;
font-size: 14px;
font-weight: bold;
color: #1bb4d4;
}
.c-btn {
padding: 8px 14px;
margin-right: 5px;
margin-bottom: 5px;
font-size: 13px;
font-weight: bold;
cursor: pointer;
background: #fff;
border: 1px solid #ccc;
border-radius: 6px;
transition: 0.2s;
}
.c-btn:hover {
background: #f0f0f0;
border-color: #888;
}
.c-btn.is-active {
color: #fff;
background: #e83e8c;
border-color: #e83e8c;
}
/* =========================================
✨ アニメーション設定 (@keyframesとクラス)
========================================= */
@keyframes slide {
0% {
background-color: #e83e8c;
transform: translateX(0);
}
100% {
background-color: #1bb4d4;
transform: translateX(450px);
}
}
@media (width <= 600px) {
@keyframes slide {
0% {
background-color: #e83e8c;
transform: translateX(0);
}
100% {
background-color: #1bb4d4;
transform: translateX(350px);
}
}
}
@media (width <= 450px) {
@keyframes slide {
0% {
background-color: #e83e8c;
transform: translateX(0);
}
100% {
background-color: #1bb4d4;
transform: translateX(200px);
}
}
}
/* 各パターンのユーティリティ(JSでクラスを脱着して動かします) */
/* 基本:1.5秒、1回再生 */
.u-ease {
animation: slide 1.5s ease;
}
.u-linear {
animation: slide 1.5s linear;
}
.u-ease-in {
animation: slide 1.5s ease-in;
}
.u-ease-out {
animation: slide 1.5s ease-out;
}
.u-ease-in-out {
animation: slide 1.5s ease-in-out;
}
.u-normal {
animation: slide 1.5s ease normal;
}
.u-reverse {
animation: slide 1.5s ease reverse;
}
/* 往復させるので回数は2回にして効果を確認 */
.u-alternate {
animation: slide 1.5s ease alternate 2;
}
/* 遅延を設定して、開始前・終了後の状態をわかりやすくする */
.u-none {
animation: slide 1.5s ease 1s none;
}
.u-forwards {
animation: slide 1.5s ease 1s forwards;
}
.u-backwards {
animation: slide 1.5s ease 1s backwards;
}
【JavaScript】
const ball = document.querySelector('.js-ball');
const buttons = document.querySelectorAll('.js-btn');
const codeDisplay = document.querySelector('.js-code-display');
buttons.forEach(btn => {
btn.addEventListener('click', () => {
// アクティブボタンの見た目の切り替え
buttons.forEach(b => b.classList.remove('is-active'));
btn.classList.add('is-active');
// 💡 ボタンの data-code を取得して、表示エリアのテキストを書き換える
codeDisplay.textContent = btn.dataset.code;
// ボタンの data-class アニメーション用クラスを取得
const animeClass = btn.dataset.class;
// 💡 ボールのクラスをリセットして再発火させる
// クラスを一度すべて外し、少し遅らせて追加することで再度再生させます
ball.className = 'c-ball';
setTimeout(() => {
ball.classList.add(animeClass);
}, 100);
});
});
コード解説
アニメーションの基本設定項目
animation-name:@keyframesで定義した任意の名前を指定します。animation-duration: アニメーションが1周する時間(秒数:s、ミリ秒:ms)を指定します(例:2s,500ms)。animation-timing-function: 速度の変化(加速・減速など)のパターンを定義します(例:ease,linear,ease-in-out)。animation-delay: アニメーションが実際に始まるまでの待機時間(遅延)を指定します(例:1s)。animation-iteration-count: 再生する回数を指定します。ずっと繰り返すならinfinite。animation-direction: 逆再生や、折り返し(往復)をさせるかを指定します(normal,reverse,alternate)。animation-fill-mode: 開始前や終了後に、スタイルをどのように保持するかを定義します(none,forwards,backwards,both)。animation-play-state: 再生中か一時停止中かをコントロールします(running,paused)。
ショートハンド(一括指定)の書く順番
animation プロパティは、複数の設定を半角スペース区切りで1行にまとめて書くことができます。 順番は基本的に自由ですが、「時間」に関する値(秒数)が2つある場合だけは厳格なルールがあります。
animation: name duration timing-function delay iteration-count direction fill-mode play-state;
- 時間のルール:1つ目に出てきた秒数が必ず
duration(アニメーションの長さ)になり、2つ目に出てきた秒数がdelay(遅延時間)として解釈されます。 - よくある書き方:
animation: slide 1.5s ease-out forwards;のように、必要なものだけをピックアップして書くのが一般的です。
補足:公式ドキュメント
まとめ
今回はCSSアニメーションを自在に操るための animation プロパティについて解説しました。
最初のうちは animation: slide 1s ease; のような簡単な設定から始め、要素を往復させたい時に alternate を足したり、最後の状態で止めたい時に forwards を足したりと、デモの動きを参考にしながら少しずつパラメーターを追加していくと理解が深まります。
Webサイトに心地よい動きを取り入れて、ワンランク上の魅力的なUIを作れるようになりましょう!