jQuery Slideshow - Continuous Loop

落爺英雄遲暮 提交于 2020-01-06 08:12:39

问题


I'm just starting out with jQuery and began working with a slideshow gallery tutorial found here : http://tutorialzine.com/2009/11/beautiful-apple-gallery-slideshow/

The latest version of the slideshow adds code to auto advance each slide, but when it gets to the end it "rewinds" to the first slide instead of simply advancing. I think it has something to do with the use of positioning and 'animate' but I can't seem to work it out.

$(document).ready(function(){
/* This code is executed after the DOM has been completely loaded */

var totWidth=0;
var positions = new Array();

$('#slides .slide').each(function(i){

    /* Traverse through all the slides and store their accumulative widths in totWidth */

    positions[i]= totWidth;
    totWidth += $(this).width();

    /* The positions array contains each slide's commulutative offset from the left part of the container */

    if(!$(this).width())
    {
        alert("Please, fill in width & height for all your images!");
        return false;
    }

});

$('#slides').width(totWidth);

/* Change the cotnainer div's width to the exact width of all the slides combined */

$('#nav ul li a').click(function(e,keepScroll){

        /* On a thumbnail click */

        $('li.navItem').removeClass('active').addClass('over');
        $(this).parent().addClass('active');

        var pos = $(this).parent().prevAll('.navItem').length;

        $('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
        /* Start the sliding animation */

        e.preventDefault();
        /* Prevent the default action of the link */


        // Stopping the auto-advance if an icon has been clicked:
        if(!keepScroll) clearInterval(itvl);
});

$('#nav ul li.navItem:first').addClass('active').siblings().addClass('over');
/* On page load, mark the first thumbnail as active */



/*****
 *
 *  Enabling auto-advance.
 *
 ****/

var current=1;
function autoAdvance()
{
    if(current==-1) return false;

    $('#nav ul li a').eq(current%$('#nav ul li a').length).trigger('click',[true]); // [true] will be passed as the keepScroll parameter of the click function on line 28
    current++;
}

// The number of seconds that the slider will auto-advance in:

var changeEvery = 5;

var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

});

回答1:


Why build your own? Embrace the power of building on top of what others built. There is a decent and quite popular slider plugin called Coda Slider. It offers plenty of useful options for customization should you need them.



来源:https://stackoverflow.com/questions/11972587/jquery-slideshow-continuous-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!