Продвижение слайд-шоу в jQuery

#jquery #slideshow

#jquery #слайд-шоу

Вопрос:

Я использую этот фрагмент кода из tutorialzine:

 $(window).load(function(){

    // The window.load event guarantees that all the images are loaded before the auto-advance begins.
    var timeOut = null;

    $('#slider_navigator .arrow, #slider_navigator .dot').click(function(e,simulated){
        // The simulated parameter is set by the trigger method.
        if(!simulated){
            // A real click occured. Cancel the auto advance animation.
            clearTimeout(timeOut);
        }
    });

    // A self executing named function expression:
    (function autoAdvance(){
        // Simulating a click on the next arrow.
        timeOut = setTimeout(autoAdvance,2000);
        $('.slider_rightlink').trigger('click',[true]);
    })();

});
  

Это работает, как и ожидалось, за исключением одного: первый слайд сразу же продвигается при загрузке страницы. После загрузки оно красиво вращается вместе с остальными слайдами.

Как я могу изменить это, чтобы он показывал первый слайд 2 секунды, как и остальные?

Ответ №1:

 $(window).load(function(){setTimeout(function(){

    // The window.load event guarantees that all the images are loaded before the auto-advance begins.
    var timeOut = null;

    $('#slider_navigator .arrow, #slider_navigator .dot').click(function(e,simulated){
        // The simulated parameter is set by the trigger method.
        if(!simulated){
            // A real click occured. Cancel the auto advance animation.
            clearTimeout(timeOut);
        }
    });

    // A self executing named function expression:
    (function autoAdvance(){
        // Simulating a click on the next arrow.
        timeOut = setTimeout(autoAdvance,2000);
        $('.slider_rightlink').trigger('click',[true]);
    })();

},2000);}

);
  

Вероятно, есть функция jQuery setTimeout, но это будет работать.

Комментарии:

1. Иногда жизнь кажется такой сложной. Спасибо!

Ответ №2:

не делайте его самоисполняющейся функцией:

 function autoAdvance(){
    // Simulating a click on the next arrow.
    timeOut = setTimeout(autoAdvance,2000);
    $('.slider_rightlink').trigger('click',[true]);
};
timeOut = setTimeout(autoAdvance,2000);
  

Комментарии:

1. Тонкие различия. Этот щелчок в первые 2 секунды будет отменен. У Walkerneo ничего не настроено до первого хода.