<main>
  <h2>CSS Animations: the <code>@keyframes</code> At-rule</h2>

  <p>A simple <code>translate</code> animation to demonstrate the <code>@keyframes</code> syntax.</p>

  <div class="box">
  </div>

  <button>Animate the Box</button> <button disabled>Reset</button>

</main>
body {
  font-family: Arial, sans-serif;
  font-size: 20px;
  padding: 0 20px;
}

main {
  text-align: center;
  margin: 0 auto;
  max-width: 800px;
}

p {
  text-align: left;
  padding: 0 20px;
}

code {
  color: firebrick;
}

.box {
  width: 100px;
  height: 100px;
  background: lightblue;
  border: solid 2px;
}

.box-animate {
  animation-name: moveObject;
  animation-duration: 2s;
  animation-timing-function: linear;
}

@keyframes moveObject {
  from {
    transform: translateX(0);
  }

  50% {
    transform: translateX(300px);
  }

  to {
    transform: translateX(300px) scale(1.5);
  }
}

button {
  margin-top: 20px;
}
let myObject = document.querySelector('.box'),
    btn1 = document.querySelector('button'),
    btn2 = document.querySelectorAll('button')[1];

btn1.addEventListener('click', function () {
  myObject.classList.add('box-animate');
  this.disabled = true;
  btn2.disabled = false;
}, false);

btn2.addEventListener('click', function () {
  myObject.classList.remove('box-animate');
  this.disabled = true;
  btn1.disabled = false;
}, false);

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.