Search code examples
javascriptgalleryslideshowphoto-gallery

Javascript autoPlay Slideshow


i have got a little slideshow. It works fine with the previous and next button. But now I would like to make a slideshow with a play&pause button. Maybe you got a idea?

the javascript:

 function loadSlide(index){
    $('#gallery li').fadeTo(6000, 1.0);
        $('#gallery li').hide()
    .       eq(index).show();
        }

        $('#gallery').data('index',0).find('li').hide();
            loadSlide(0);

        $('#next').on('click',function(e){
          var index = $('#gallery').data('index'),
           numSlides = $('#gallery li').length;

          index = (index + 1) % numSlides;    
            loadSlide(index);
          $('#gallery').data('index',index);
            e.preventDefault();            
    });
        $('#previous').on('click',function(e){
         var index = $('#gallery').data('index'),
            numSlides = $('#gallery li').length;

            index = (index + numSlides - 1) % numSlides;
                loadSlide(index);
        $('#gallery').data('index',index);
            e.preventDefault();
    });
    $("#gallery li img").css({
                    'height':$("#gallery").height()+'px'
                });
                $("#gallery li").each(function(){
                    $(this).css({
                        'margin-left':'+'+parseInt($(this).children("img").width()/2)+'px'
                });
            });

            $("#gallery li img").css({
                    'height':$("#gallery").height()+'px'
                });
                $("#gallery li").each(function(){
                    $(this).css({
                        'margin-left':'+'+parseInt($(this).children("img").width()/2)+'px'
                });
            });
        }

thanks in advance!


Solution

  • Didn't test, but something like this should work:

    $('#start').click( function(){
     t = setInterval( function(){ $('#next').trigger('click'), 2000 } );
    });
    
    $('#stop').click( function(){
     clearInterval(t);
    });