Почему функция обратного вызова в этой анимации Jquery будет выполняться раньше, чем анимация? - PullRequest
0 голосов
/ 27 июля 2011

У меня есть этот javascript:

        $('.carouselItem.' + currentCarouselItem).animate({left: '975px'}, 'slow', function(){
            $(this).removeClass('active');
            $(this).attr('style','');
            var nextItem = currentCarouselItem + 1;
            if (nextItem == 7) {
                nextItem = 1;
            }
            $('.carouselItem.' + nextItem).animate({left: '110px'}, 'slow', function(){
                $(this).addClass('active');
            });
            }
        })

Обратный вызов для первой анимации выполняется до завершения первой анимации.Любая идея, почему?

Вот полный код, если это будет более полезным:

var currentCarouselItem = 1; //set carousel to first slide
    var runCarousel = 1;
    var carouselInterupt = 0;


function autoScroll(){

        if (runCarousel == 1) {
            if (carouselInterupt == 0){

            $('.carouselItem.' + currentCarouselItem).animate({left: '975px'}, 'slow', function(){
                $(this).removeClass('active');
                $(this).attr('style','');
                var nextItem = currentCarouselItem + 1;
                if (nextItem == 7) {
                    nextItem = 1;
                }
                $('.carouselItem.' + nextItem).animate({left: '110px'}, 'slow', function(){
                    $(this).addClass('active');
                });
                }
            })

            if (currentCarouselItem == 6) {
                currentCarouselItem = 1;
            } else {
                currentCarouselItem++;
            }

        }
    }

    setTimeout(autoScroll, 3000);

}

$(function(){

$(window).load(function(){

    autoScroll();

});

});

А вот затронутый HTML (много lorem ipsum :)):

    <div id= "wrapper">
    <div id= "teasertext" class="active carouselItem 1">


        <div class= "title"> <h1> Growing Your Business Online</h1> </div>

        <p>     Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </p>
    <div id= "viewservices" class="teaserBttn"><a href="#" rel="#">View Services</a></div>


    </div>



    <div id= "about-teasertext" class="carouselItem 2">


        <div class= "about-title"> <h1> About</h1> </div>

        <p>     Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </p>
    <div id= "view-about" class="teaserBttn"><a href="#" rel="#">View About</a></div>


    </div>



    <div id= "portfolio-teasertext" class="carouselItem 3">


        <div class= "portfolio-title"> <h1> Portfolio</h1> </div>

        <p>     Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </p>
    <div id= "view-portfolio" class="teaserBttn"><a href="#" rel="#">View Portfolio</a></div>


    </div>



    <div id= "team-teasertext" class="carouselItem 4">

    <div class= "team-title"> <h1> Team</h1> </div>

        <p>     Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </p>
    <div id= "view-teams" class="teaserBttn"><a href="#" rel="#">View Team</a></div>


    </div>



    <div id= "process-teasertext" class="carouselItem 5">
    <div class= "process-title"> <h1> Process</h1> </div>

        <p>     Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </p>
    <div id= "view-process" class="teaserBttn"><a href="#" rel="#">View Process</a></div>


    </div>


    <div id= "packages-teasertext" class="carouselItem 6">
    <div class= "packages-title"> <h1> Packages</h1> </div>

        <p>     Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </p>
    <div id= "view-packages" class="teaserBttn"><a href="#" rel="#">View Packages</a></div>


    </div>


</div>

1 Ответ

1 голос
/ 27 июля 2011

У вас, кажется, есть дополнительная фигурная скобка }:

 $('.carouselItem.' + currentCarouselItem).animate({left: '975px'}, 'slow', function(){
    $(this).removeClass('active');
    $(this).attr('style','');
    var nextItem = currentCarouselItem + 1;
    if (nextItem == 7) {
      nextItem = 1;
    }
    $('.carouselItem.' + nextItem).animate({left: '110px'}, 'slow', function(){
       $(this).addClass('active');
     });
  }   //<----- HERE
  })
...