3
I made this script to move a div by pressing the arrow keys on the keyboard. The right and down key are working perfectly, however, the top and left are not, the code ta the same as the others.
Follow the code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).keydown(function(x){
if(x.which == 39 || x.keyCode == 39){
$('div').animate({left: '1180px'});
}
}).keyup(function(){
$('div').animate().stop();
});
});
$(document).ready(function(){
$(document).keydown(function(x){
if(x.which == 40 || x.keyCode == 40){
$('div').animate({top: '1180px'});
}
}).keyup(function(){
$('div').stop();
});
});
$(document).ready(function(){
$(document).keydown(function(x){
if(x.which == 37 || x.keyCode == 37){
$('div').animate({right: '1180px'});
}
}).keyup(function(){
$('div').stop();
});
});
$(document).ready(function(){
$(document).keydown(function(x){
if(x.which == 38 || x.keyCode == 38){
$('div').animate({ '1180px'});
}
}).keyup(function(){
$('div').stop();
});
});
</script>
Thanks man, it worked perfectly. Excellent tips.
– blackbird312
@blackbird312 please mark the answer as correct if it solved the problem ;)
– Guilherme Nascimento