<header>
<h2 class="title">animation</h2>
<p class="description">Застосовує анімацію, визначену в CSS, до елемента.</p>
</header>
<main>
<div class="result">
<div class="animated-element">Це анімований елемент</div>
<div class="controls">
<button id="playAnimation">Запустити анімацію</button>
<button id="pauseAnimation">Призупинити анімацію</button>
<select id="animationSelect">
<option value="slideIn">Анімація слайду</option>
<option value="fadeIn">Анімація появи</option>
<option value="bounce">Анімація відскоку</option>
</select>
</div>
</div>
</main>
body {
font-size: 16px;
line-height: 1.5;
font-family: monospace;
}
header {
background-color: #f1f1f1;
margin-bottom: 25px;
padding: 15px;
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
}
header h2.title {
padding-bottom: 15px;
border-bottom: 1px solid #999;
}
header p.description {
font-style: italic;
color: #222;
}
.result {
background-color: #f8f8f8;
padding: 15px;
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
}
.animated-element {
width: 200px;
height: 200px;
background-color: lightgray;
margin-bottom: 20px;
}
@keyframes slideIn {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(0);
}
}
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes bounce {
0% {
transform: translateY(0);
}
50% {
transform: translateY(-20px);
}
100% {
transform: translateY(0);
}
}
const animatedElement = document.querySelector('.animated-element');
const playButton = document.getElementById('playAnimation');
const pauseButton = document.getElementById('pauseAnimation');
const animationSelect = document.getElementById('animationSelect');
let animation;
playButton.addEventListener('click', () => {
const animationName = animationSelect.value;
animation = animatedElement.animate(
[
{ transform: 'none' },
{ transform: 'none' }
],
{
duration: 1000,
iterations: Infinity,
easing: 'ease-in-out',
direction: 'alternate'
}
);
animatedElement.style.animation = `${animationName} 1s infinite`;
});
pauseButton.addEventListener('click', () => {
if (animation) {
animation.cancel();
animatedElement.style.animation = 'none';
}
});
animationSelect.addEventListener('change', () => {
if (animation) {
animation.cancel();
}
const animationName = animationSelect.value;
animatedElement.style.animation = `${animationName} 1s infinite`;
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.