سؤال

Essentially I've created a thumbnail gallery which you can scroll through using the left and right arrows. The right arrow event works perfectly fine, so I assumed that the left arrow event would be the same except with a (-) value. However when I press the left key it only goes to the previous thumbnail every SECOND time.

Can somebody take a look at my code and let me know what I'm missing? Thanks!

$(document).bind("keydown", function(event) {

if (event.keyCode == 39)
{   
    $("#thumbnail img").each(function(i) {

        if ($(this).hasClass("currentThumb"))
        {
            currentSelectionIndex = i;

            $("#thumbnail img").removeClass("currentThumb").addClass("thumb");  

            if (currentSelectionIndex == 14 && parseInt($('#end').text()) < parseInt($('#total').text()))
            {
                nextImages(currentCategory.value);  
            }
        }   
        if(i == currentSelectionIndex + 1)
        {
            $(this).removeClass("thumb").addClass("currentThumb");
            $(this).parent().click();
        }
    });
}

if (event.keyCode == 37)
{   

    $("#thumbnail img").each(function(i) {

        if ($(this).hasClass("currentThumb"))
        {
            currentSelectionIndex = i;

            $("#thumbnail img").removeClass("currentThumb");
            $("#thumbnail img").addClass("thumb");  

            if (currentSelectionIndex == 0  && parseInt($('#start').text()) > 1)
            {
                prevImages(currentCategory.value);  
            }
        }   
        if(i == currentSelectionIndex - 1)
        {
            $(this).removeClass("thumb").addClass("currentThumb");
            $(this).parent().click();
        }
    });
}

});

هل كانت مفيدة؟

المحلول

Reversing your selection should be enough to make it go in reverse.

$.fn.reverse = [].reverse; // at the top of your code


// in the event handler for left arrow
$("#thumbnail img").reverse().each(function(i) {

don't forget to change back to +

نصائح أخرى

It may works using keyup instead of keydown. I had some issues with it. If it doesn't work use http://jsfiddle.net so that we can easier solve it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top