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

JavaScript

Tati Belikova
Tati Belikova
14,117 Points

Javascript + CSS Animation with keydown

I have decided to practice my Javascript&jQuery&CSSAnimation skills by creating a site where user can trigger animation by pressing keyboard. Different key from A-Z = different animation.

But I m struggling with javascript, I have tried many codes and nothing seems to work.

How do I correctly create an event on specific keydown to addClass to the 'div' and on the keyup remove that class using jquery???

also how to better go through each Keycode to add different class to the div? Switch statement?

4 Answers

Liam Maclachlan
Liam Maclachlan
22,805 Points

Hi there, I have played around with this a bit an come up with this solution.

No need to add a second function, just use toggle instead of add on a keypress command.

Keypress will pick up both directions of the key and toggle will add/remove on alternating interactions.

 $(document).keypress('keypress',function(e){
      if(e.which==13){
         $('div').toggleClass('down');
     }
 });

Let me know if it is not bang on and I'll try and help further if I can :)

Tati Belikova
Tati Belikova
14,117 Points

Hi, Liam. Thanks a lot for your tip. I played around with your solution, and here is the code I came up with.

I used addClass instead of toggleClass, because toggleClass adds class with one keypress and removes that class with another keypress. And I wanted to get an animation that would appear every time you click a button. so I also added a keyup function. And I have found this delay function online, maybe there is a shorter way to add a delay to a keyup?

There is still one issue within the code that I would like to correct, when you press the key longer then 1 set of animation it gets stuck in the starting position. and of course I want it to repeat the animation as long as the key is being pressed. How should I loop it?

Here is my JS

var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
 };
});


$(document).on('keypress',function(e){
if(e.which==13){
$('div').addClass('minicircle');
 }

$(document).keyup(function() {
delay(function(){
$('div').removeClass('minicircle');
 }, 500 );
});

});

Here is my CSS

.minicircle {
  width: 200px;
  height: 200px;
  background-color: #86c5bd;
  border-radius: 50%;
  position: fixed;
  -webkit-animation: slide 2s linear;
}

@-webkit-keyframes slide {
  0% {
    left: 0;
    top: 0;
  }
  50% {
    left: 50%;
    top: 50%;
  }
  100% {
    left: 80%; 
    top: -30%;
  }

}
Liam Maclachlan
Liam Maclachlan
22,805 Points

Is it worth trying a while loop and mixing it with a queue? I won't give it a shot tonight but I feel like that would be the right thing to go with :)

EDIT:

I had found this, too http://stackoverflow.com/questions/4193314/continuous-movement-when-a-key-is-held-down

EDIT:

This is actually really useful. I was looking at creating a basic game using OOP PHP, AJAX and jQuery. I was going to make it text based but this should allow me to make it visual, too.

If you want to see something amazing, seach CSS3 FPS game (or follow this link http://www.keithclark.co.uk/labs/css-fps/)

Tati Belikova
Tati Belikova
14,117 Points

Liam, I added infinite to css animation and it solved the problem=). But now i ll need to add sample of sound along with animation, not sure if i can make it loop like animation through css, so i ll try to use the method u sent me. thanks

Ah that Keith Clark demo is dope!!!

Liam Maclachlan
Liam Maclachlan
22,805 Points

Aweosme. That infinite thing has just opened my eyes a bit more to what can be done in CSS. Looks interesting, even though I don't fully understand it at the moment >.>

Anyway, glad I could help :)

EDIT:

Quick question, does animation only need the -webkit- vendor prefix only now, or does it still need th -o- and -mozilla-?

EDIT:

Don;t worry. Checked out caniuse.com and it turns out yes, only the -webkit- is needed :)

Tati Belikova
Tati Belikova
14,117 Points

yeah -o- and -mozilla- are for older browsers only right? yeah CSS is great, thats why i wanted to practice it on this project for starters to see what i can squeeze out from it with animation. plus its easier then javascript. after i want to try to make animation on javascript with two.js .. i just need to learn and practice js more to make it happen:)