title: Reverse a CSS animation once it completes
body: <p>I have a CSS animation and what I would like to do is have the animation reverse once the initial animation has completed. Here's what I have tried so far:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(window).load(function () {
    $('.animation-container').addClass('animate');
  });
  
  $(function() {
      var animationCount = 0;
      $(".animation-container").one("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd", function () {
          animationCount++
          if (animationCount === 2) {
            $('.animation-container').addClass('reverse');
          }
      });
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.animation-container &gt; div {
    display:block;
    background:#000;
    width:20px;
    height:30px;
  }
  
  .animation-container.animate {
    animation:rotateOuter 2s ease-in-out;
    animation-fill-mode: forwards;
    animation-delay: 2s;
  }
    .animation-container.reverse {
      animation:rotateOuter 2s ease-in-out;
      animation-direction: reverse;
    }
  
  .animation-container.animate .top {
    animation:rotateInner 2s ease-in-out;
    animation-fill-mode: forwards;
  }
    .animation-container.reverse .top {
      animation:rotateInner 2s ease-in-out;
      animation-direction:reverse;
    }
  
  @keyframes rotateOuter {
      0% {
          transform:rotate(0deg);
      }
      100% {
          transform:rotate(-20deg);
      }
  }
  
  @keyframes rotateInner {
      0% {
          transform:rotate(0deg);
      }
      100% {
          transform:rotate(-30deg);
      }
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;div class="animation-container"&gt;
    &lt;div class="top"&gt;&lt;/div&gt;
    &lt;div class="bottom"&gt;&lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>The idea here was to add a class with <code>animation-direction:reverse;</code> to the element once the first animation has completed, unfortunately it's not working as planned and just goes back to the default non-animated state.</p>