Please excuse my ignorance, I only started writing javascript recently. I am not looking for an out-of-the-box solution, but more some insight that will help me to write the code myself.
I am working on a full-screen, sliding-panel type website, with the option to navigate using either arrow keys or clickable links.
I've got it all functioning nicely when navigating with the arrow keys, and what I want to do is to call the same functions when using the on-screen navigation links. I am not sure how to do this, as the functions and variables are declared inside the key binding function.
My second problem is that I need a way to prevent the slide functions from being called more than once while the function is still animating. I have tried various setTimeout implementations, and also Ben Alman's doTimeout and debounce, but can't seem to get it working properly.
Any help would be greatly appreciated!
Here is a jsFiddle with a simplex version of what I have built : http://jsfiddle.net/BKpAK/3/
And here is just the JS if you don't want to fiddle:
$(document).ready(function () {
var currentSlide = 1;
$(document).keydown(function(e){
nextSlide = currentSlide + 1;
prevSlide = currentSlide - 1;
lastSlide = $(".slide").length
cur1 = document.getElementById("slide" + currentSlide);
prev1 = document.getElementById("slide" + prevSlide);
next1 = document.getElementById("slide" + nextSlide);
function moveRight(){
if(currentSlide < lastSlide){
$(next1).addClass('frontAndCenter').stop().animate({
left: "0",
}, 500, function() {
$(this).removeClass("frontAndCenter");
$(cur1).css("left", "-100%");
});
currentSlide = currentSlide + 1;
}
else{
return false;
};
};
function moveLeft(){
if(currentSlide > 1){
$(prev1).addClass('frontAndCenter').stop().animate({
left: "0",
}, 500, function() {
$(this).removeClass("frontAndCenter");
$(cur1).css("left", "100%");
});
currentSlide = currentSlide - 1
}
else{
return false;
};
};
switch(e.which) {
case 37: // left
moveLeft();
break;
case 39: // right
moveRight();
break;
default: return;
}
e.preventDefault();
});
$("#arrowRight").click(function () {
moveRight();
});
$("#arrowLeft").click(function () {
moveLeft();
});
});