jquery фотогалерея проблема связывания кнопки «Предыдущая предыдущая» - PullRequest
0 голосов
/ 19 мая 2011

Я использую эту фотогалерею с этим:

$('.normsize img:gt(0)').hide();
$('.photos a').click(function(){
    var index = $('.photos a').index(this);
    $('.normsize img:visible').hide();
    $('.normsize img:eq('+index+')').fadeIn();
});

с миниатюрами в элементе div и картинками обычного размера рядом с ним.

Мой вопрос: как бы я связал следующую и предыдущую кнопки? Это, вероятно, проще, чем то, что я уже использую.

Редактировать: Исправлен вывод кода.

1 Ответ

0 голосов
/ 19 мая 2011

Вы должны сохранить текущий индекс изображения в глобальной переменной.

var currentIndex = 0;
var lastIndex = $('.normsize img').size() - 1;

function showPic(index) {
    $('.normsize img:visible').hide();
    $('.normsize img:eq('+index+')').fadeIn();
    currentIndex = index;
}

$('.photos a').click(function() {
    showPic($('.photos a').index(this));
});

$('#next').click(function() {
    // If is the last go to first, else go to next
    showPic(currentIndex == lastIndex ? 0 : currentIndex + 1);
});

$('#prev').click(function() {
    // If is the first, go to last, else go to previous
    showPic(currentIndex == 0 ? lastIndex : currentIndex - 1);
});

$('.normsize img').hide(); // Hide all
showPic(0); // Initialize first picture shown
...