2011-11-06 289 views
0

嘿傢伙這是我當前代碼的一個片段。我想知道如何將「onFinish或Finished」事件監聽器添加到此代碼中。jQuery中的onfinish事件監聽器/ javascript

這是setStage功能

function setStage($section) 
    { 

    switch($section) 
    { 
     case "about": 
      if (stageSet == true) 
      { 
       resetStage(); 
      } 
      $("#one").animate({ width: "+=30", height: "+=30"}, 1000); 
      $("#two").animate({ width: "+=30", height: "+=30"}, 1000); 
      $("#three").animate({ height: "+=30"}, 1000); 
      $("#four").animate({ width: "+=30"}, 1000); 
      $("#five").animate({ width: "+=30"}, 1000); 
      stageSet = true; 
      break; 
     case "portfolio": 
      if (stageSet == true) 
      { 
       resetStage(); 
      } 
      $("#one").animate({ width: "+=30", height: "+=30"}, 1000); 
      $("#two").animate({ width: "+=30", height: "+=30"}, 1000); 
      $("#three").animate({ height: "+=30"}, 1000); 
      $("#four").animate({ width: "+=30"}, 1000); 
      $("#five").animate({ width: "+=30"}, 1000); 
      stageSet = true; 
      break; 
     case "contact": 
      if (stageSet == true) 
      { 
       resetStage(); 
      }; 
      $("#one").animate({ width: "+=30", height: "+=30"}, 1000); 
      $("#two").animate({ width: "+=30", height: "+=30"}, 1000); 
      $("#three").animate({ height: "+=30"}, 1000); 
      $("#four").animate({ width: "+=30"}, 1000); 
      $("#five").animate({ width: "+=30"}, 1000); 
      stageSet = true; 
      break; 
     case "resume": 
      if (stageSet == true) 
      { 
       resetStage(); 
      } 
      $("#one").animate({ width: "+=30", height: "+=30"}, 1000); 
      $("#two").animate({ width: "+=30", height: "+=30"}, 1000); 
      $("#three").animate({ height: "+=30"}, 1000); 
      $("#four").animate({ width: "+=30"}, 1000); 
      $("#five").animate({ width: "+=30"}, 1000); 
      stageSet = true; 
      break; 
     default: 
      alert('not a valid section'); 
      break; 
     } 
    } 

每個機箱內的動畫將是獨一無二的,但我只是做動畫,現在是相同的。以下是點擊發生的情況。我需要做的是應用類「關」到每個資產淨值,這是的話,然後當setStage完成我需要它起飛類

$(".nav-btn").click(function() 
{ 
    var section = $(this).attr("title"); 
    //turn off pointer for obsessive clickers 
    $(".nav-btn").addClass("off"); 

    onComplete: function(setStage(section) 
    { 
     $(".nav-btn").removeClass("off"); 
    }); 
}); 

這當然行不通的。 setStage函數效果很好,我想要做的是在函數結束時刪除類。我檢查了jQuery網站上的api事件部分,並沒有看到任何足夠的東西,也許我只是不夠努力。 http://api.jquery.com/category/events/或者我在一起看錯了部分。有什麼想法嗎?提前感謝您幫助我降低我的不速之客。

+1

刪除什麼類,何時完成什麼功能? 'setStage'是做什麼的? –

+0

你去馬特,我需要它做這樣的事情。 – Brodie

回答

0
var removeClass = function() { 
    $(this).removeClass("off"); 
} 


    function setStage($section) 
{ 

switch($section) 
{ 
    case "about": 
    case "portfolio": 
    case "resume": // and so on 

     if (stageSet == true) 
     { 
      resetStage(); 
     } 
     $("#one").animate({ width: "+=30", height: "+=30"}, 1000, removeClass); 
     $("#two").animate({ width: "+=30", height: "+=30"}, 1000, removeClass); 
     $("#three").animate({ height: "+=30"}, 1000, removeClass); 
+0

我試過了,它不會等待setStage()在刪除類之前完成。 – Brodie

+0

這是因爲.animate()異步執行。您必須定義一個函數來移除該類,並將其作爲回調函數傳遞給.animate()。我將編輯答案,以顯示 – user907860

+0

非常感謝,現在我的問題是我可以將該回調添加到我的setStage函數,還是必須將它添加到每個動畫函數的末尾?如果可能,我真的很喜歡它在每個案例之後執行。我試圖把它放在案例循環之外,但它不起作用。 – Brodie