jQuery: Every 5 seconds, automatically click on next image in a list?

后端 未结 5 1255
醉话见心
醉话见心 2020-12-21 02:41

I have a given list of images, presented as thumbnails:

  • &
5条回答
  •  独厮守ぢ
    2020-12-21 03:12

    You can go with the plain JQuery way, if you like. Here's my try

    var intervalId;
    $(function(){
    
    
    function cycleImage(){
    
        var onLastLi = $("#thumbs li:last").hasClass("current");       
        var currentImage = $("#thumbs li.current img");
        var targetImage = $('div.feature-photo img');             
        $(targetImage).hide().attr('src', $(currentImage).attr('src')).fadeIn();
    
        var currentLi = $("#thumbs li.current");
        currentLi.removeClass("current");
    
        if(onLastLi){
            $("#thumbs li:first").addClass("current");
        }else{        
            currentLi.next().addClass("current");
        }
    
    };
    
     intervalID = setInterval(cycleImage, 5000);
    
    
    
    })
    

    And you can find it in action here

提交回复
热议问题