jQuery Slider - Progress Bar

2019-04-17 06:10发布

问题:

Similar to: Jquery slider with progress bar

I have this slider and progress bar, but I want the progress bar synced with with the "previous" and "next" buttons as well. So when you click "next" the progress bar starts from beginning.

http://jsfiddle.net/aidenguinnip/8rJws/

$(window).load(function(){
var currentIndex = 0;// store current pane index displayed
var ePanes = $('#slider .panel'), // store panes collection
    time   = 3000,
    bar = $('.progress_bar');

function showPane(index){// generic showPane
    // hide current pane
    ePanes.eq(currentIndex).stop(true, true).fadeOut();
    // set current index : check in panes collection length
    currentIndex = index;
    if(currentIndex < 0) currentIndex = ePanes.length-1;
    else if(currentIndex >= ePanes.length) currentIndex = 0;
    // display pane
    ePanes.eq(currentIndex).stop(true, true).fadeIn();
    // menu selection
    $('.nav li').removeClass('current').eq(currentIndex).addClass('current');
}
// bind ul links
$('.nav li').click(function(ev){showPane($(this).index());});
// bind previous & next links
$('.previous').click(function(){showPane(currentIndex-1);});
$('.next').click(function(){showPane(currentIndex+1);});
// apply start pane
showPane(0);

function run(){
    bar.width(0);
    showPane(currentIndex+1);
    bar.animate({'width': "+=400"}, time, run);
}

run();

});

回答1:

Here you go...

http://jsfiddle.net/8rJws/1/

I stopped the animation and reset the bar width to 0 when changing slides, and then restarted the animation (see line 10)...

bar.stop(true, true).css("width", 0).animate({'width': "+=400"}, time, run);