--- title: 'HTMLElement: animationcancel イベント' slug: Web/API/HTMLElement/animationcancel_event tags: - Animation - AnimationEvent - CSS Animations - CSS アニメーション - Event - Reference - animationcancel - events - イベント translation_of: Web/API/HTMLElement/animationcancel_event ---
animationcancel イベントは、 CSS アニメーションが予期せず中断されたときに発生します。言い換えれば、 {{domxref("HTMLElement/animationend_event", "animationend")}} イベントを送出せずに実行が停止するときはいつでもです。これは {{cssxref("animation-name")}} が変更されてアニメーションが削除されたり、動いているノードが CSS を使用して非表示にされた場合などに起こることがあります。したがって、直接または原因として、その包含ノードのいずれかが隠されています。
このイベントのイベントハンドラーは、 {{domxref("GlobalEventHandlers.onanimationcancel", "onanimationcancel")}} プロパティを設定するか、 {{domxref("EventTarget.addEventListener", "addEventListener()")}} を使用することで追加することができます。
| バブリング | あり |
|---|---|
| キャンセル | 不可 |
| インターフェイス | {{domxref("AnimationEvent")}} |
| イベントハンドラープロパティ | {{domxref("GlobalEventHandlers/onanimationcancel","onanimationcancel")}} |
このコードは現在アニメーションしている要素を取得し、 animationcancel イベントのリスナーを追加します。次に、要素の {{cssxref("display")}} プロパティを none に設定し、 animationcancel イベントを引き起こします。
const animated = document.querySelector('.animated');
animated.addEventListener('animationcancel', () => {
console.log('アニメーションが取り消されました');
});
animated.style.display = 'none';
同様に、 {{domxref("GlobalEventHandlers/onanimationcancel", "onanimationcancel")}} プロパティを addEventListener() の代わりに使用するとこうなります。
const animated = document.querySelector('.animated');
animated.onanimationcancel = () => {
console.log('アニメーションが取り消されました');
};
animated.style.display = 'none';
<div class="animation-example">
<div class="container">
<p class="animation">あなたは私たちの惑星を訪れるために寒い夜を選びました。</p>
</div>
<button class="activate" type="button">アニメーションを有効にする</button>
<div class="event-log"></div>
</div>
.container {
height: 3rem;
}
.event-log {
width: 25rem;
height: 2rem;
border: 1px solid black;
margin: 0.2rem;
padding: 0.2rem;
}
.animation.active {
animation-duration: 2s;
animation-name: slidein;
animation-iteration-count: 2;
}
@keyframes slidein {
from {
transform: translateX(100%) scaleX(3);
}
to {
transform: translateX(0) scaleX(1);
}
}
const animation = document.querySelector('p.animation');
const animationEventLog = document.querySelector('.animation-example>.event-log');
const applyAnimation = document.querySelector('.animation-example>button.activate');
let iterationCount = 0;
animation.addEventListener('animationstart', () => {
animationEventLog.textContent = `${animationEventLog.textContent}'アニメーション開始' `;
});
animation.addEventListener('animationiteration', () => {
iterationCount++;
animationEventLog.textContent = `${animationEventLog.textContent}'アニメーション反復: ${iterationCount}' `;
});
animation.addEventListener('animationend', () => {
animationEventLog.textContent = `${animationEventLog.textContent}'アニメーション終了'`;
animation.classList.remove('active');
applyAnimation.textContent = "アニメーションを有効にする";
});
animation.addEventListener('animationcancel', () => {
animationEventLog.textContent = `${animationEventLog.textContent}'アニメーション取り消し'`;
});
applyAnimation.addEventListener('click', () => {
animation.classList.toggle('active');
animationEventLog.textContent = '';
iterationCount = 0;
let active = animation.classList.contains('active');
if (active) {
applyAnimation.textContent = "アニメーションを取り消す";
} else {
applyAnimation.textContent = "アニメーションを有効にする";
}
});
{{ EmbedLiveSample('Live_example', '100%', '150px') }}
| 仕様書 | 状態 | 備考 |
|---|---|---|
| {{SpecName("CSS3 Animations", "#eventdef-animationevent-animationcancel")}} | {{Spec2("CSS3 Animations")}} | 初回定義 |
{{Compat("api.HTMLElement.animationcancel_event")}}