<header>
  <h2 class="title">Animation Events</h2>
  <p class="description">Відстеження початку, ітерацій та завершення анімації в JavaScript</p>
</header>
<main>
  <div class="result">
    <button id="startAnimationButton">Запустити анімацію</button>
    <div id="animatedBox" class="box"></div>
    <div id="eventLog">
      <h3>Журнал подій:</h3>
      <ul id="logList"></ul>
    </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);
}

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);
}

#animatedBox {
  width: 100px;
  height: 100px;
  background-color: #3498db;
  margin: 20px 0;
  animation: none;
}

@keyframes boxAnimation {
  0% { transform: scale(1); background-color: #3498db; }
  50% { transform: scale(1.5); background-color: #e74c3c; }
  100% { transform: scale(1); background-color: #3498db; }
}

#eventLog {
  margin-top: 20px;
}

#logList {
  list-style-type: none;
  padding: 0;
}

#logList li {
  padding: 5px;
  border-bottom: 1px solid #ccc;
}
const animatedBox = document.getElementById('animatedBox');
const startAnimationButton = document.getElementById('startAnimationButton');
const logList = document.getElementById('logList');

// Функція для додавання запису в журнал подій
function logEvent(message) {
  const logItem = document.createElement('li');
  logItem.textContent = message;
  logList.appendChild(logItem);
}

// Запускає анімацію на кнопці "Запустити анімацію"
startAnimationButton.addEventListener('click', () => {
  animatedBox.style.animation = 'boxAnimation 2s ease-in-out infinite';
  logEvent('Анімація запущена');
});

// Обробляє подію animationstart
animatedBox.addEventListener('animationstart', (event) => {
  logEvent(`Подія: animationstart; Назва анімації: ${event.animationName}`);
});

// Обробляє подію animationiteration
animatedBox.addEventListener('animationiteration', (event) => {
  logEvent(`Подія: animationiteration; Час ітерації: ${event.elapsedTime}s`);
});

// Обробляє подію animationend
animatedBox.addEventListener('animationend', (event) => {
  logEvent(`Подія: animationend; Анімація завершена`);
  animatedBox.style.animation = 'none'; // Скидання анімації після завершення
});

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.