2017-06-05 23 views
0

我有一個項目列表,每個項目都有一個關閉按鈕。點擊後,動畫播放,列表向上滑動,並且項目被成功移除。完成動畫後分離並移動元素

$(document).on("click", ".close-button", function(e) { //removes with animation 
    $(this).parent().animate({ 
     opacity: 0 
    }, 250, function() { 
     $(this).animate({ 
       marginBottom: 0 
      }, 250) 
      .children() 
      .animate({ 
       padding: 0 
      }, 250) 
      .wrapInner("<div />") 
      .children() 
      .slideUp(250, function() { 
       $(this).closest(".element").remove(); 
      }); 
    }); 
}); 

我試圖使得不是刪除該項目時,我分離的項目,並將其移動到另一個視圖來修改上面的一段代碼。我試圖使用下面的代碼,無濟於事。 $(this).closest('.element').detach().prependTo('#diff-view').hide().slideDown(250)

擺脫動畫將元素成功移動到不同的視圖。

$(document).on("click", ".close-button" ,function(e) { //detaches w/o animation 
    $(this).closest('.element').detach().prependTo('#diff-view').hide().slideDown(250) 
}); 

我想保持課程的動畫,但我只是無法弄清楚如何,而不是鬆脫,卸下的同時保持他們在的地方。任何幫助將不勝感激。

+0

是刪除工作? – Satpal

+0

@Satpal剛剛評論出來檢查。沒有視覺差異,但我檢查了文檔,顯然它不工作... HTML仍然存在,只是隱藏。我不知道 – moomin

回答

1

由於您正在爲動畫母親this引用動畫元素。因爲關閉按鈕的結果引用丟失。

存儲當前元素的元素的引用和以後使用的對象。

$(document).on("click", ".close-button", function (e) { 
    //Store the reference 
    var $this= $(this); 
    $(this).parent().animate({ 
     opacity: 0 
    }, 250, function() { 
     $(this).animate({ 
      marginBottom: 0 
     }, 250) 
     .children() 
     .animate({ 
      padding: 0 
     }, 250) 
     .wrapInner("<div />") 
     .children() 
     .slideUp(250, function() { 
      $this.closest('.element').prependTo('#diff-view').hide().slideDown(250) 
     }); 
    }); 
}); 
+0

好吧,我明白現在必須存儲參考,但它仍然沒有將元素移動到另一個視圖。它使用'.remove()'來移除,這非常棒。我無法理解如何在沒有所有動作的情況下進行前置作品,但它不在這裏... – moomin

+0

我在'.prependTo()'前面使用了'.clone()',它可以工作。我通過使用clone()而不是'.detach()'來丟失引用,但是我們會看看這會在後面產生什麼影響。 – moomin