为什么这个Jquery动画的回调函数会在动画之前执行?

时间:2011-07-27 02:48:03

标签: javascript jquery callback jquery-animate

我有这个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 个答案:

答案 0 :(得分:1)

您似乎有一个额外的大括号}

 $('.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
  })