开发者

Jquery Slideshow Script, Pause on Click

开发者 https://www.devze.com 2023-03-16 19:47 出处:网络
I\'m very new to jquery and javascript in general, so sorry if this is a simple question.Any help would be hugely appreciated. I have a slideshow auto-paging and I want it so when you click on the con

I'm very new to jquery and javascript in general, so sorry if this is a simple question. Any help would be hugely appreciated. I have a slideshow auto-paging and I want it so when you click on the controller (#thumbs li) the show pauses. Here's the JS:

    <script type="text/javascript">
var currentImage;
var currentIndex = -1;
var interval;
function showImage(index){
    if(index < $('#bigPic div').length){
        var indexImage = $('#bigPic div')[index]
        if(currentImage){   
            if(currentImage != indexImage ){
                $(currentImage).css('z-index',2);
                clearTimeout(myTimer);
                $(currentImage).fadeOut(400, function() {
                    myTimer = setTimeout("showNext()", 3500);
                    $(this).css({'display':'none','z-index':1})
                });
            }
        }
        $(indexImage).css({'display':'block', 'opacity':1});
        currentImage = indexImage;
        currentIndex = index;
        $('#thumbs li').removeClass('active');
        $($('#thumbs li')[index]).addClass('active');
    }
}

function showNext(){
    var len = $('#bigPic div').length;
    var next = currentIndex < (len-1) ? currentIndex + 1 : 0;
    showImage(next);
}

var myTimer;

$(document).ready(function() {
    myTimer = setTimeout("showNext()", 3500);
    showNext(); //loads first image
    $('#thumbs 开发者_StackOverflow中文版li').bind('click',function(e){
        var count = $(this).attr('rel');
        showImage(parseInt(count)-1);
    });
});


</script>   


I would make a function to start the slide show as well as stop so something like this.

// must be set to declare that we're using it later.
var myTimer;

function startShow(){
  myTimer = setTimeout("showNext()", 3500);
}
function stopShow(){
  clearTimeout(myTimer);
}

$(document).ready(function() {
   startShow();
   showNext(); //loads first image
  $('#thumbs li').bind('click',function(e){
       var count = $(this).attr('rel');
       showImage(parseInt(count)-1);
   });
  // you're going to want to hide this element initially, until the user pauses the slide show, vice versa.
  $('#selectorToStartShow').bind('click',function(){
     startShow();
  });
  $('#selectorToPauseShow').bind('click',function(){
     stopShow();
  });
});

i'm not sure what you're trying to do with the #thumbs li click, however that should do the trick.

0

精彩评论

暂无评论...
验证码 换一张
取 消