--- title: animation slug: Web/CSS/animation tags: - Animation - CSS - CSS アニメーション - CSS プロパティ - Reference - animation-delay - animation-direction - animation-duration - animation-fill-mode - animation-iteration-count - animation-name - animation-play-state - animation-timing-function translation_of: Web/CSS/animation ---
CSS の animation 一括指定プロパティは、スタイル間のアニメーションを適用します。これは {{cssxref("animation-name")}}, {{cssxref("animation-duration")}}, {{cssxref("animation-timing-function")}}, {{cssxref("animation-delay")}}, {{cssxref("animation-iteration-count")}}, {{cssxref("animation-direction")}}, {{cssxref("animation-fill-mode")}}, {{cssxref("animation-play-state")}} の一括指定です。
このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 https://github.com/mdn/interactive-examples をクローンしてプルリクエストを送信してください。
/* @keyframes duration | timing-function | delay | iteration-count | direction | fill-mode | play-state | name */ animation: 3s ease-in 1s 2 reverse both paused slidein; /* @keyframes duration | timing-function | delay | name */ animation: 3s linear 1s slidein; /* @keyframes duration | name */ animation: 3s slidein;
<div class="grid">
<div class="col">
<div class="note">
Given the following animation:
<pre>@keyframes slidein {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}</pre>
</div>
<div class="row">
<div class="cell">
<button class="play" title="PLAY"></button>
</div>
<div class="cell flx">
<div class="overlay">animation: 3s ease-in 1s 2 reverse both paused slidein;</div>
<div class="animation a1"></div>
</div>
</div>
<div class="row">
<div class="cell">
<button class="pause" title="PAUSE"></button>
</div>
<div class="cell flx">
<div class="overlay">animation: 3s linear 1s slidein;</div>
<div class="animation a2"></div>
</div>
</div>
<div class="row">
<div class="cell">
<button class="pause" title="PAUSE"></button>
</div>
<div class="cell flx">
<div class="overlay">animation: 3s slidein;</div>
<div class="animation a3"></div>
</div>
</div>
</div>
</div>
html,body {
height: 100%;
box-sizing: border-box;
}
pre { margin-bottom: 0; }
svg { width: 1.5em; height: 1.5em; }
button {
width: 27px;
height: 27px;
background-size: 16px;
background-position: center;
background-repeat: no-repeat;
border-radius: 3px;
cursor: pointer;
}
button.play {
background-image: url('data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cstyle%3Epath%20%7Bdisplay%3Anone%7D%20path%3Atarget%7Bdisplay%3Ablock%7D%3C%2Fstyle%3E%3Cpath%20id%3D%22play%22%20d%3D%22M3%2C3%20L3%2C13%20L13%2C8%20Z%22%20%2F%3E%3Cpath%20id%3D%22pause%22%20d%3D%22M5%2C4%20L7%2C4%20L7%2C13%20L5%2C13%20Z%20M9%2C4%20L11%2C4%20L11%2C13%20L9%2C13%20Z%22%20%2F%3E%3Cpath%20id%3D%22restart%22%20d%3D%22M13%2C9%20A5%2C5%2C1%2C1%2C1%2C8%2C4%20L8%2C2%20L12%2C5%20L8%2C8%20L8%2C6%20A3%2C3%2C1%2C1%2C0%2C11%2C9%20A1%2C1%2C1%2C1%2C1%2C13%2C9%20z%22%20%2F%3E%3C%2Fsvg%3E#play');
}
button.pause {
background-image: url('data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cstyle%3Epath%20%7Bdisplay%3Anone%7D%20path%3Atarget%7Bdisplay%3Ablock%7D%3C%2Fstyle%3E%3Cpath%20id%3D%22play%22%20d%3D%22M3%2C3%20L3%2C13%20L13%2C8%20Z%22%20%2F%3E%3Cpath%20id%3D%22pause%22%20d%3D%22M5%2C4%20L7%2C4%20L7%2C13%20L5%2C13%20Z%20M9%2C4%20L11%2C4%20L11%2C13%20L9%2C13%20Z%22%20%2F%3E%3Cpath%20id%3D%22restart%22%20d%3D%22M13%2C9%20A5%2C5%2C1%2C1%2C1%2C8%2C4%20L8%2C2%20L12%2C5%20L8%2C8%20L8%2C6%20A3%2C3%2C1%2C1%2C0%2C11%2C9%20A1%2C1%2C1%2C1%2C1%2C13%2C9%20z%22%20%2F%3E%3C%2Fsvg%3E#pause');
}
button.restart {
background-image: url('data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cstyle%3Epath%20%7Bdisplay%3Anone%7D%20path%3Atarget%7Bdisplay%3Ablock%7D%3C%2Fstyle%3E%3Cpath%20id%3D%22play%22%20d%3D%22M3%2C3%20L3%2C13%20L13%2C8%20Z%22%20%2F%3E%3Cpath%20id%3D%22pause%22%20d%3D%22M5%2C4%20L7%2C4%20L7%2C13%20L5%2C13%20Z%20M9%2C4%20L11%2C4%20L11%2C13%20L9%2C13%20Z%22%20%2F%3E%3Cpath%20id%3D%22restart%22%20d%3D%22M13%2C9%20A5%2C5%2C1%2C1%2C1%2C8%2C4%20L8%2C2%20L12%2C5%20L8%2C8%20L8%2C6%20A3%2C3%2C1%2C1%2C0%2C11%2C9%20A1%2C1%2C1%2C1%2C1%2C13%2C9%20z%22%20%2F%3E%3C%2Fsvg%3E#restart');
}
.grid {
width: 100%;
height: 100%;
display: flex;
background: #EEE;
font: 1em monospace;
}
.row {
display: flex;
flex: 1 auto;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
}
.col {
display: flex;
flex: 1 auto;
flex-direction: column;
}
.cell {
box-sizing: border-box;
margin: .5em;
padding: 0;
background-color: #FFF;
overflow: hidden;
text-align: left;
}
.flx {
flex: 1 0;
}
.note {
background: #fff3d4;
padding: 1em;
margin: .5em;
font: .8em sans-serif;
text-align: left;
flex: none;
}
.overlay { padding: .5em; }
@keyframes slidein {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
.a1 { animation: 3s ease-in 1s 2 reverse both paused slidein; }
.a2 { animation: 3s linear 1s slidein; }
.a3 { animation: 3s slidein; }
.animation {
background: #3F87A6;
width: 100%;
height: calc(100% - 1.5em);
transform-origin: left center;
}
window.addEventListener('load', function () {
var ANIMATION = Array.from(document.querySelectorAll('.animation'));
var BUTTON = Array.from(document.querySelectorAll('button'));
function toggleButton (btn, type) {
btn.classList.remove('play', 'pause', 'restart');
btn.classList.add(type);
btn.title = type.toUpperCase(type);
}
function playPause (i) {
var btn = BUTTON[i];
var anim = ANIMATION[i];
if (btn.classList.contains('play')) {
anim.style.animationPlayState = 'running';
toggleButton(btn, 'pause');
} else if (btn.classList.contains('pause')) {
anim.style.animationPlayState = 'paused';
toggleButton(btn, 'play');
} else {
anim.classList.remove('a' + (i + 1));
setTimeout(function () {
toggleButton(btn, i === 0 ? 'play' : 'pause');
anim.style.animationPlayState = '';
anim.classList.add('a' + (i + 1));
}, 100)
}
}
ANIMATION.forEach(function (node, index) {
node.addEventListener('animationstart', function () { toggleButton(BUTTON[index], 'pause'); });
node.addEventListener('animationend', function () { toggleButton(BUTTON[index], 'restart'); });
});
BUTTON.forEach(function (btn, index) {
btn.addEventListener('click', function () { playPause(index); });
});
})
{{EmbedLiveSample("animation", "100%", 260, "", "", "example-outcome-frame")}}
アニメーションできるプロパティの説明があります。この説明は言うまでもなく CSS トランジションにも有効です。
{{cssinfo}}
animation プロパティは1つまたはコンマで区切った複数のアニメーションとして指定します。
それぞれ個別のアニメーションは以下のように定義されます。
none, {{cssxref("<custom-ident>")}}, {{cssxref("<string>")}} のいずれかになります。各アニメーションの定義の中での順序は重要です。{{cssxref("<time>")}} として解釈される最初の値は、 {{cssxref("animation-duration")}} に、そして2番目の値は、{{cssxref("animation-delay")}} に割り当てられます。
順序はまた、各アニメーションの定義において、他のキーワードから {{cssxref("animation-name")}} の値を区別するためにも重要です。 {{cssxref("animation-name")}} 以外のプロパティの値として解釈することが可能なキーワードは、一括指定の以前にそれらのプロパティの値が見つかっていないのならば、 {{cssxref("animation-name")}} ではなく、それらのプロパティの値として取り扱われます。また、シリアライズされる場合には、他のプロパティの既定値は少なくとも、他のプロパティの値と解釈することも可能な {{cssxref("animation-name")}}と区別する必要がある場合には出力する必要があり、他の場合には出力してもいいことになっています。
<single-animation-iteration-count><single-animation-direction><single-animation-fill-mode><single-animation-play-state><div class="view_port">
<div class="polling_message">
Listening for dispatches
</div>
<div class="cylon_eye"></div>
</div>
.polling_message {
color: white;
float: left;
margin-right: 2%;
}
.view_port {
background-color: black;
height: 25px;
width: 100%;
overflow: hidden;
}
.cylon_eye {
background-color: red;
background-image: linear-gradient(to right,
rgba(0, 0, 0, .9) 25%,
rgba(0, 0, 0, .1) 50%,
rgba(0, 0, 0, .9) 75%);
color: white;
height: 100%;
width: 20%;
-webkit-animation: 4s linear 0s infinite alternate move_eye;
animation: 4s linear 0s infinite alternate move_eye;
}
@-webkit-keyframes move_eye { from { margin-left: -20%; } to { margin-left: 100%; } }
@keyframes move_eye { from { margin-left: -20%; } to { margin-left: 100%; } }
{{EmbedLiveSample('Cylon_Eye')}}
これ以外の例は CSS アニメーションを参照してください。
点滅を伴うアニメーションは、注意欠陥障害 (ADHD) のような認知障害を持つ人々に問題を起こす可能性があります。加えて、特定の種類の動きが、前庭障害、てんかん、片頭痛、痙攣感受性などの引き金になることもあります。
アニメーションを停止したり無効にしたりする仕組みを、できれば Reduced Motion Media Query を使用して、アニメーションがない操作の設定を表明したユーザーの希望を叶えるように作成することができます。
| 仕様書 | 状態 | 備考 |
|---|---|---|
| {{SpecName('CSS3 Animations', '#animation', 'animation')}} | {{Spec2('CSS3 Animations')}} | 初回定義 |
{{Compat("css.properties.animation")}}
open 属性を使用しても、アニメーションが有効になっていると既定で開かないというものがあります ({{bug(1382124)}})。 Quantum CSS では修正されています。