this is my script.js file where I have created functions for go to previous and next slides resp with timer and buttons , but manually by clicking next and previous buttons the slides are changng but in addition to this I also want to do this by keyboard right and left key pressing .
//script for generate one by one questions
var timer;
function gotoNextSlide() {
timer && clearTimeout(timer);
goToSlide(getCurrentSlide().index() + 1);
timer = setTimeout(gotoNextSlide, 1000 * 60);
}
function gotoPrevSlide() {
goToSlide(getCurrentSlide().index() - 1);
}
function getCurrentSlide() {
return $("#slides .slide:visible");
}
function goToSlide(index) {
var $slides = $("#slides .slide");
if (index >= 0 && index < $slides.length) {
$slides
.removeClass("active")
.eq(index)
.addClass("active");
$(".dot")
.removeClass("active")
.eq(index)
.addClass("active");
}
}
function init() {
$(".goto-next-slide").click(gotoNextSlide);
//timer for next question slide
timer = setTimeout(gotoNextSlide, 1000 * 60);
$(".goto-prev-slide").click(gotoPrevSlide);
goToSlide(0);
}
$(document).ready(init);
here is my next and previous buttons
<a class="goto-next-slide overlay-button overlay-right">Next</a>
<a class="goto-prev-slide overlay-button overlay-left">Previous</a>
everytthing is working fine but by clicking those two buttons and in addition to this I just want to bind the arrow left and right keys of keyboard to go to next slide and previous slides resp , I am getting confused on how to do this ? I know the one way is to use key37 and 39 but unable to apply here.
I tried by this but not working actually I am confused where to call the keydown function ?
$(document).keydown(function(e) {
switch(e.which) {
case 37: // left
function() { $(".goto-prev-slide").click(); }
break;
case 39: // right
function() { $(".goto-next-slide").click(); }
break;
default: return; // exit this handler for other keys
}
e.preventDefault(); // prevent the default action (scroll / move caret)
});
where I am going wrong ?