<header>
  <h2 class="title">animationiteration (HTML Event)</h2>
  <p class="description">Виконується кожного разу при завершенні одного циклу CSS-анімації.</p>
</header>
<main>
  <div class="result">
    <button id="animateButton" class="color-changing-button">Натисніть для старту</button>
    <p id="iterationCount">Кількість циклів: 0</p>
  </div>
</main>
body {
  font-size: 16px;
  line-height: 1.5;
  font-family: monospace;
}

header {
  background-color: #f1f1f1;
  margin-bottom: 25px;
  padding: 15px;
  -webkit-box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  -moz-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;
  -webkit-box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  -moz-box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  text-align: center;
}

.color-changing-button {
  padding: 10px 20px;
  font-size: 18px;
  border: none;
  cursor: pointer;
  background-color: #007bff;
  color: #fff;
  animation: changeColor 1s infinite;
  animation-play-state: paused; /* Анімація починається у паузі */
}

@keyframes changeColor {
  0% {
    background-color: #007bff;
  }
  50% {
    background-color: #28a745;
  }
  100% {
    background-color: #ff5733;
  }
}

#iterationCount {
  margin-top: 20px;
  font-size: 16px;
  color: #333;
}
document.addEventListener('DOMContentLoaded', function() {
  const animateButton = document.getElementById('animateButton');
  const iterationCountDisplay = document.getElementById('iterationCount');
  let iterationCount = 0;

  // Додаємо обробник події animationiteration до кнопки
  animateButton.addEventListener('animationiteration', function(event) {
    iterationCount++;
    iterationCountDisplay.textContent = `Кількість циклів: ${iterationCount}`;
    
    // Змінюємо текст кнопки кожного разу, коли анімація завершує цикл
    if (iterationCount % 2 === 0) {
      animateButton.textContent = 'Триває анімація...';
    } else {
      animateButton.textContent = 'Анімація в процесі...';
    }
  });

  // Запуск і зупинка анімації при натисканні на кнопку
  animateButton.addEventListener('click', function() {
    if (animateButton.style.animationPlayState === 'paused') {
      animateButton.style.animationPlayState = 'running';
      animateButton.textContent = 'Анімація запущена!';
    } else {
      animateButton.style.animationPlayState = 'paused';
      animateButton.textContent = 'Натисніть для старту';
      iterationCount = 0; // Скидання лічильника циклів
      iterationCountDisplay.textContent = `Кількість циклів: ${iterationCount}`;
    }
  });
});

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.