Gabriel Santos
Gabriel Santos

Reputation: 4974

jQuery slideshow thumbnails show/hide

I have asked and done the follow code:

$(document).ready(function() {
    $('.gallery').each(function(index, element) {
        var id = $(this).attr('id');
        var images = $('#' + id + ' .content li').index() -1;

        $('#' + id + ' .prev').click(function(e) {
            e.preventDefault();

            if($('#' + id + ' .content .current').prev().length == 0 ) {
                $('#' + id + ' .content .current').fadeOut(1000).removeClass('current').hide();
                $('#' + id + ' .content li:last').fadeIn(1000).addClass('current').show();
            } else {
                $('#' + id + ' .content .current').removeClass('current').fadeOut(1000).hide().prev().fadeIn(1000).addClass('current').show();
            }

            return false;
        });

        $('#' + id + ' .next').click(function(e) {
            e.preventDefault();

            if($('#' + id + ' .content .current').next().length == 0 ) {
                $('#' + id + ' .content .current').fadeOut(1000).removeClass('current').hide();
                $('#' + id + ' .content li:first').fadeIn(1000).addClass('current').show();
            } else {
                $('#' + id + ' .content .current').removeClass('current').fadeOut(1000).hide().next().fadeIn(1000).addClass('current').show();
            }

            return false;
        });

        $('#' + id + ' .thumbnails li').click(function(e) {
            e.preventDefault();

            $('#' + id + ' .content .current').removeClass('current').hide();
            $('#' + id + ' .content .thumbnails').removeClass('current');
            $('#' + id + ' .content li').eq($(this).index()).fadeIn(1000).addClass('current').show();

            return false;
        });

        $('#' + id + ' .thumbnails ul').css('width', $('#' + id + ' .thumbnails ul li').length * scrollWidth + 'px');
        $('#' + id + ' .content li:first').addClass('current').show();
    });
});

The html:

<div id="galerry-one" class="gallery">
    <div class="content">
        <ul>
            <li id="content-1"><img src="image1.jpg" /></li>
            <li id="content-2"><img src="image2.jpg" /></li>
            <li id="content-3"><iframe src="http://www.youtube.com/..."></iframe></li>
            <li id="content-4"><img src="image3.jpg" /></li>
            <li id="content-5"><iframe src="http://www.youtube.com/..."></iframe></li>
        </ul>
    </div>
    <div class="thumbnails">
        <span class="prev"> « </span>
        <ul>
            <li id="go-content-1"><img src="image1_thumbnai.jpg" /></li>
            <li id="go-content-2"><img src="image2_thumbnai.jpg" /></li>
            <li id="go-content-3"><img src="youtube1_thumbnai.jpg" /></li>
            <li id="go-content-4"><img src="image3_thumbnai.jpg" /></li>
            <li id="go-content-5"><img src="youtube2_thumbnai.jpg" /></li>
        </ul>
        <span class="next"> » </span>
    </div>
</div>

The problem now is, how I can limit to 4 thumbnails each time I click previous/next?

Upvotes: 0

Views: 805

Answers (1)

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

I had a similar implementation for showing 4 items and scroll with Prev/Next option. So I just modified a bit for it to work with your html.. Check my DEMO

Change elToShow var to customize how many thumbnails you wanted to show.

var stPt = 0, elToShow = 4; //showing 4 elements

var $ul = $('.thumbnails ul');
var $li = $('.thumbnails ul li'); //get the list of li's
var $copy_li = [];
var copy_lgt = $li.length - elToShow;

//call to set thumbnails based on what is set
initNav();
function initNav() {
   var tmp;
   for (var i = elToShow; i < $li.length; i++) {
      tmp = $li.eq(i);
      $copy_li.push(tmp.clone());
      tmp.remove();
   }
}

$('.next').click (function () {
    $li = $('.thumbnails ul li'); //get the list of li's

    //move the 1st element clone to the last position in copy_li
    $copy_li.splice(copy_lgt, 0, $li.eq(0).clone() ); //array.splice(index,howmany,element1,.....,elementX)

    //kill the 1st element in the UL
    $li.eq(0).remove();

    //add to the last
    $ul.append($copy_li.shift());
});

$('.prev').click (function () {
    $li = $('.thumbnails ul li'); //get the list of li's

    //move the 1st element clone to the last position in copy_li
    $copy_li.splice(0, 0, $li.eq(elToShow-1).clone()); //array.splice(index,howmany,element1,.....,elementX)

    //kill the 1st element in the UL
    $li.eq(elToShow-1).remove();

    //add to the last
    $ul.prepend($copy_li.pop());

});

Upvotes: 1

Related Questions