2013-12-09 52 views
0

我試圖動畫到高度爲0,然後在做替換後刪除。替換後動畫到高度爲0

有沒有人有任何想法爲什麼jQuery動畫沒有發生?

$('.dataCard').not('.focused').each(function(){ 
    var div = $('<div />',{ 
     css : { height : $(this).height() } 
}); 
    $(this).replaceWith(div).animate({ height:0 }, function(){ $(this).remove() }); 
+0

不夠代碼,PLZ置HTML以及更多的JavaScript/jQuery代碼。 –

回答

2

$(this).replaceWith(div);回報this到鏈,在這種情況下是你只是刪除了數據卡元素。

$('.dataCard').not('.focused').each(function() { 
    var div = $('<div />', { 
     css: { 
      height: $(this).height() 
     } 
    }); 
    $(this).replaceWith(div); 
    div.animate({ 
     height: 0 
    }, function() { 
     $(this).remove() 
    }); 
}); 

另請注意,您剛剛插入的元素實際上必須具有可以爲其設置動畫的高度。

+0

你能告訴我爲什麼你的代碼執行不起作用嗎? https://gist.github.com/theirf/7869132 – gomangomango

+0

原因有語法錯誤,在$(this).height()' - > http://jsfiddle.net/B4PSu/後面刪除分號 – adeneo

0

嘗試這種方式

var div = null; 
$('.dataCard').not('.focused').each(function(){ 
    var div = $('<div />',{ 
     css : { height : $(this).height() } 
}); 
if (div != null) { 
    $(this).replaceWith(div); 
    div.animate({ height:0 }, function(){ $(this).remove() }); 
}