2012-08-09 67 views
0

我有一個小問題。有沒有可能使用jQuery恢復函數? 我有一些點擊功能與中間的行動。在點擊之前,我可以恢復這些元素嗎? Thx 4的幫助。功能恢復與jquery

$('.bottom_panel_button_02').click(function(){ 
    $('#recipe_panel').css('opacity','1'); 
    $('.default_clock, .info_top_panel .random_title, .bottom_panel_button_06').css('display','none'); 
    $('.bottom_main_panel_button').css('background','url("img/ap_bottom_panel_button_white.png") center bottom no-repeat') 
    setTimeout(function(){ 
     $('.arrow_up, .arrow_down').fadeIn(300); 
    },500); 
    $('.main_content, .oven_panel').fadeOut(200); 
}); 

回答

1

不,沒有內置的方法來恢復使用jQuery的DOM操作/動畫bucnch。你必須寫出兩個鏡像對方,寫出相關的代碼:

function action(){ 
    $('#recipe_panel').css('opacity','1'); 
    $('.default_clock, .info_top_panel .random_title, .bottom_panel_button_06').css('display','none'); 
    $('.bottom_main_panel_button').css('background','url("img/ap_bottom_panel_button_white.png") center bottom no-repeat') 
    setTimeout(function(){ 
     $('.arrow_up, .arrow_down').fadeIn(300); 
    },500); 
    $('.main_content, .oven_panel').fadeOut(200);  
} 

function revert(){ 
    $('#recipe_panel').css('opacity','0'); 
    $('.default_clock, .info_top_panel .random_title, .bottom_panel_button_06').css('display','block'); 
    $('.bottom_main_panel_button').css('background','') 
    setTimeout(function(){ 
     $('.arrow_up, .arrow_down').fadeOut(300); 
    },500); 
    $('.main_content, .oven_panel').fadeIn(200);  
} 

$('.bottom_panel_button_02').click(action); 
$('.someOtherButton').click(revert); 
+0

:)我想這是唯一的方法 - thx – Lukas 2012-08-09 09:49:12

0

你可以使用custom attributes做這樣的事情:

$(document).ready(function() { 
    $('.bottom_panel_button_02').attr("clicked", "false"); 

    $('.bottom_panel_button_02').click(function() { 
     var clicked = $(this).attr("clicked"); 

     if (clicked == "false") { 
      // do your click function 

      $(this).attr("clicked", "true"); 
     } else { 
      // do your revert function 

      $(this).attr("clicked", "false"); 
     } 
    }); 
}); 

或者你可以有,而不是使用屬性被設置一個全局變量/隱藏輸入元素。你也可以.addClass和.removeClass取決於元素的點擊狀態,並在這些CSS類中擁有你的狀態。

+0

是的,我可以做到這一點,但它覆蓋了所有的元素,我不想這樣做.. – Lukas 2012-08-09 09:03:31

+1

@ŁukaszBorawski在這種情況下,你不得不自己去做這個DOM的直接「恢復」函數。另一種可以實現這一點的方法是在你的DOM中用一些ID複製隱藏的div中的所有「可點擊」元素,並編寫自己的回覆,它將從div中獲取原始元素並用該副本替換單擊的元素,這將添加雖然下載你的頁面相當大的開銷。 – 2012-08-09 09:10:47

+0

這是一些想法,但我更好的辦法可以把這一切行動轉移到自然條件 - thx的利益 – Lukas 2012-08-09 09:53:55