Reputation: 278
$("#right").click(function(){
$(".block").animate({"left": "+=500px"}, "slow");
});
$("#left").click(function(){
$(".block").animate({"left": "-=50px"}, "slow");
});
//Control
$(document).keydown(function(e){
if (e.keyCode == 37) {
alert( "left pressed" );
return false;
}
if (e.keyCode == 38) {
alert( "up pressed " );
return false;
}
if (e.keyCode == 39) {
alert( "right pressed " );
return false;
}
if (e.keyCode == 40) {
alert( "down pressed " );
return false;
}
});
;
I am trying to figure out how to make the click function (Animate) go into the keydown events below can anyone help me? Im new to javascript.
Upvotes: 1
Views: 117
Reputation: 490433
Place the code inside the if
block like so...
if (e.keyCode == 40) {
$(".block").animate({"left": "-=50px"}, "slow");
}
Alternatively, you can call the click()
on $('#left')
, which would trigger its event handler.
Upvotes: 2