2012-10-13 29 views
0

我有一個動畫函數,每次點擊一堆按鈕時都會發生,它們都會有特定的事情發生,但動畫會每次都發生。現在我已經設置好了,所以它是點擊的一部分,所以我重新定義了每次,我如何設置它一次,然後每個按鈕只是說點擊時運行該功能?在點擊事件期間設置稍後調用的函數

現在它設置爲這樣:

$("#animation1").click(function() { 
$("#clinic_850_CONTAINER").animate({ 
    height: box_TOTAL_ht + 20 
}, 300); 

$("#staff_850_CONTAINER").animate({ 
    marginTop: staff_850_CONTAINER_ht * -1 
}, 300); 

$("#profile_850_HEADER").delay(160).animate({ 
    top: 10 
}, 270); 
$("#profile_850_BIO").delay(330).animate({ 
    top: profile_850_HEADER_ht + 20 
}, 400); 
$("#profile_850_EDU").delay(450).animate({ 
    top: profile_850_HEADER_ht + profile_850_BIO_ht + 20 
}, 400); 
$("#profile_850_CONTACT").delay(570).animate({ 
    top: profile_850_HEADER_ht + profile_850_BIO_ht + profile_850_EDU_ht + 20 
}, 400); 

}); 

設置它像這樣在頁面加載運行,你怎麼防止它從運行,直到點擊偏偏但讓他們單獨

function profile_850_close_animation() { 
$("#staff_850_CONTAINER").animate({ 
    marginTop: 0 
}, 300); 

$("#clinic_850_CONTAINER").animate({ 
    height: staff_850_CONTAINER_ht 
}, 300); 

$("#profile_850_HEADER").animate({ 
    top: box_TOTAL_ht + 20 
}, 300); 
$("#profile_850_BIO").animate({ 
    top: box_TOTAL_ht + 50 
}, 450); 
$("#profile_850_EDU").animate({ 
    top: box_TOTAL_ht + 60 
}, 450); 
$("#profile_850_CONTACT").animate({ 
    top: box_TOTAL_ht + 70 
}, 450); 
} 

$("#profile_850_CLOSE").click(profile_850_open_animation()); 
+0

Arrrg,CAAAAAPS! – elclanrs

+0

爲所有按鈕定義一個類,並將點擊處理程序綁定到該類 – Huangism

回答

2

這兩個會的工作:

$("#animation1, #animation2, #animation3").click(function() { 
    // existing code 
}); 

function animate() { 
    //existing code 
} 
$("#animation1").click(animate); 
+0

我喜歡第二個想法,但是當我這樣做時,動畫只是運行onload – loriensleafs

+0

如果它正在運行onload,很可能是因爲您鍵入了'.click(animate())'而不是'.click(動畫)'。你需要傳遞一個函數引用(不要調用函數)。 – TimHayes

-1
function animation() { 
// Animation code goes here 
} 

$("#animation1").click(animation); 

此外,您可以使用jquery選擇器來選擇所有的按鈕,並一次分配他們的onclick事件。例如,如果所有的動畫按鈕都屬於類別按鈕

$(".animation").click(animation); 
+0

還有一個特殊的選擇器來選擇所有按鈕:按鈕http://api.jquery.com/category/selectors/?rdfrom=http%3A%2F %2Fdocs.jquery.com%2Fmw%2Findex.php%3Ftitle%3DSelectors%26redirect%3Dno – clementine

+0

所以當我設置它的動畫只是運行onload – loriensleafs

+0

你不傳遞'animation()',你只要通過函數名稱(無parens)像這樣'$(「#animation1」)。click(animation);' – jfriend00

0

設置這些類型以應用於。我需要查看html結構以更好地優化您的點擊處理程序和動畫,但是您可以指定相對於動畫點擊或者可以檢查id以確定要設置其他哪些元素。

相關問題