Friday, 23 August 2013

Loop through keypress Jquery (case : 1; etc..)

Loop through keypress Jquery (case : 1; etc..)

I have a problem which is how to maintain/continue a loop if a key is
pressed. I figured to animate the page with case 1 / case 2 / case 3 and
case 4 but I would like to know how to continue the loop if the key is
pressed again (and again...). I read about loop here but I just can't
figure it out.
Thanks in advance, here is my code :
var counter = 0;
$(document).bind('keydown', function(e) {
if(e.which == 37 || e.which == 39) {
counter++;
switch(counter) {
case 1:
$('#maikeximu,#dossier').stop()
.animate({
height: "toggle",
}, 1200, function() {
$('html,body').animate({
scrollTop: $("#maikeximu").next().offset().top
}, 1000);
});
break;
case 2:
$("#gal,#dossier, #viz").stop()
.animate({
height: "toggle",
}, 1200, function() {
$('html,body').animate({
scrollTop: $("#gal").next().offset().top
}, 1000);
});
break;
case 3:
$('#viz,#dossier').stop()
.animate({
height: "toggle",
}, 1200, function() {
$('html,body').animate({
scrollTop: $("#dossier").next().offset().top
}, 1000);
});
break;
case 4:
$('#maikeximu, #gal,#dossier').stop()
.animate({
height: "toggle",
}, 1200, function() {
$('html,body').animate({
scrollTop: $("#maikeximu").offset().top
}, 0);
});
break;
counter = 0;
return;
}
};
});
I hope to learn more about loops (a big problem for me) :)

No comments:

Post a Comment