2012-11-28 48 views
0

如果通過腳本的這一部分看到alert('hey'),那麼它在文檔的頭部追加五次......它不會引起任何問題,但我想知道如何讓它只發生一次?.append()只有一次?它在頭部追加五次

$('#logo').fadeIn(2000, function() { 
     $(this).animate({ top: "-=20px" },{ 
     duration: 1000, 
     specialEasing: {top: 'easeOutQuad', left: 'easeInQuad'},  
      complete: function() { 
       $(this).animate({ top: "63px" },{ 
       duration: 1000, 
       specialEasing: {top: 'easeOutQuad', left: 'easeInQuad'},  
        complete: function() { 
        } 
       }); 
       $('.home,.about,.investments,.media,.contact').animate({ top: "+=106px" },{ 
       duration: 2000, 
       specialEasing: {top: 'easeOutQuad', left: 'easeOutQuad'},  
        complete: function() { 
         $('#ballTarp').remove(); 
         $("head").append('<link href="css/hoverStates.css" rel="stylesheet" type="text/css">'); 
         alert('hey'); 
         $('#ball').removeClass('home').addClass('activeHome'); 
         $('.activeTitle').fadeIn("fast"); 
         $('.dropShadow').fadeIn(7000); 
         $('footer').fadeIn("fast"); 
         $('.title').fadeIn("slow"); 
         $('.login').fadeIn(3000); 
        } 
       }); 
      } 
    }); 

回答

3

它呼籲每個選擇的功能齊全(。家裏,。關於,.investments,.media,名爲.contact)。

這就是爲什麼它追加5次。

這應該工作...

$('#logo').fadeIn(2000, function() { 
    $(this).animate({ top: "-=20px" },{ 
    duration: 1000, 
    specialEasing: {top: 'easeOutQuad', left: 'easeInQuad'},  
     complete: function() { 
      $(this).animate({ top: "63px" },{ 
      duration: 1000, 
      specialEasing: {top: 'easeOutQuad', left: 'easeInQuad'},  
       complete: function() { 
       } 
      }); 
      $('.home,.about,.investments,.media,.contact').animate({ top: "+=106px" },{ 
      duration: 2000, 
      specialEasing: {top: 'easeOutQuad', left: 'easeOutQuad'},  
       complete: function() { 
        $('#ballTarp').remove(); 
        // not here... 
        $('#ball').removeClass('home').addClass('activeHome'); 
        $('.activeTitle').fadeIn("fast"); 
        $('.dropShadow').fadeIn(7000); 
        $('footer').fadeIn("fast"); 
        $('.title').fadeIn("slow"); 
        $('.login').fadeIn(3000); 
       } 
      }, function(){ 
       // but here... 
       $("head").append('<link href="css/hoverStates.css" rel="stylesheet" type="text/css">'); 
       alert('hey'); 

      }); 
     } 
}); 
+0

WOW我驚奇自己有時 – DJERock

+0

所以,是的,事情是否需要它發生動畫的一部分完成後,是否有任何可能的方法來保持它不發生變化而不改變選擇器 – DJERock

0

這是因爲它正在運行5次,每一次在你選擇的每一個元素。

$('.home,.about,.investments,.media,.contact') 
0

OK以及感謝@teewuane和@epascarello讓我看到明顯... #brainFart但這裏是我如何固定它

$('#logo').fadeIn(2000, function() { 
     $(this).animate({ top: "-=20px" },{ 
     duration: 1000, 
     specialEasing: {top: 'easeOutQuad', left: 'easeInQuad'},  
      complete: function() { 
       $(this).animate({ top: "63px" },{ 
       duration: 1000, 
       specialEasing: {top: 'easeOutQuad', left: 'easeInQuad'},  
        complete: function() { 
        } 
       }); 
       $('.home,.about,.investments,.media,.contact').animate({ top: "+=106px" },{ 
       duration: 2000, 
       specialEasing: {top: 'easeOutQuad', left: 'easeOutQuad'},  
        complete: function() { 
         $('#ballTarp').remove(); 
         $('#ball').removeClass('home').addClass('activeHome'); 
         $('.activeTitle').fadeIn("fast"); 
         $('.dropShadow').fadeIn(7000); 
         $('footer').fadeIn("fast"); 
         $('.title').fadeIn("slow"); 
         $('.login').fadeIn(3000); 
        } 
       }); 
       $("head").append('<link href="css/hoverStates.css" rel="stylesheet" type="text/css">'); 
      } 
    });