Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

revert css3 animation .click

Hello, I am playing around with the css -webkit- animation. No problem in making a div animate accross the screen etc but,

I am trying to get a div to animate once clicked, this is pretty easy by using .toggleClass with Jquery.

But I am having a hard time reversing the animation, so that it plays in reverse.

here is what I have thus far,

<body>
  <div class='box'>
  </div>
</body>

and this css with animation

.box {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: black;
}


.boxSlide {
  width: 100px;
  height: 100px;
  position: relative;
  -webkit-animation-name: first;
  -webkit-animation-duration: 5s;
  -webkit-animation-fill-mode: forwards;
  background-color: black;
}


/* Chrome, Safari, Opera */
@-webkit-keyframes first {
  0% {background:red; left: 0px;}
  25%{background:yellow; left: 200px;}
  50%{background:pink; left: 500px;}
  75%{background:red; left: 200px;}
  100%{background:black; left: 500px;}
}

and finally the Jquery

<script>
  $('.box').click(function() {
      $(this).toggleClass('boxSlide');
  });
</script>

<script>
  $('.boxSlide').click(function() {
    $(this).css('-webkit-animation-direction':'alternate;')
  });
</script>

Also I should, once this issue has been fixed, would click on the box again after it has reversed it play normally and will be able to reverse again?

Thanks guys,

First time I have played around with the CSS3 animations