2013-05-30 219 views
0

我試圖擴大對點擊的div,並在另一次點擊收縮再次。 當這個函數被調用時,沒有任何反應。Jquery切換動畫點擊

jQuery代碼:

$(".panel").toggle(function() 
{ 
    $(this).click(function() 
    { 
     $(this).animate({width:'300px',height:'300px'}, 200); 
    }); 
}, 
function() 
{ 
    $(this).click(function() 
    { 
     $(this).animate({width:'152px',height:'152px'}, 200); 
    }); 
}); 
+1

可能重複[Where fn.toggle(handler(eventObject),handler(eventObject)...)gone?](http://stackoverflow.com/questions/14301935/where-has-fn-toggle- handlereventobject-handlereventobject-gone) – j08691

+0

什麼版本的JQuery?另外,讓一個更多的代碼在其中的jsbin – JClaspill

+0

如果可能,你可以提供HTML代碼? – Edward

回答

4

切換()只皮和顯示器該div,並沒有將兩個函數作爲參數。 http://api.jquery.com/toggle/

這是我的解決方案,雖然我不確定這是否是最好的,但希望有人指出改進。

var next_move = "expand"; 
$(document).ready(function(){ 
$(".panel").click(function() 
{ 
    var css = {}; 
    if (next_move == "expand"){ 
     css = { 
      width: '300px', 
      height: '300px' 
     }; 
     next_move = "shrink"; 
    } else { 
     css = { 
      width: '152px', 
      height: '152px' 
     };  
     next_move = "expand"; 
    } 
    $(this).animate(css, 200); 
}); 
}); 

http://jsfiddle.net/Vc49G/

享受!

+0

Thanks al ot - 完美的作品!已經非常有用:) – Legatro

+0

不用客氣=) – Edward

1

click()不neccesary內切換功能,試試這個:

$(".panel").toggle(
function() 
{ 
    $(this).animate({width:'300px',height:'300px'}, 200); 
}, 
function() 
{ 
    $(this).animate({width:'152px',height:'152px'}, 200); 
} 
); 

順便說一句,this method was deprecated in jquery 1.8

+0

對不起,我使用1.9.2,所以它不適用於我,但無論如何感謝:) – Legatro