2010-10-04 150 views
5

我有一個網站我正在研究,有幾個absolutelty定位divs,我需要調整點擊,這些將填補divs在容器中。問題是我怎麼讓他們切換回到原來的位置(頂部,左側),這是不同的。jQuery動畫回到原來的位置

$(".work-item .toggle").toggle(function() { 
     $(this).parent().animate({ height: '430', width: '930', top: '0', left: '0' }, 750); 
    }, 
    function() { 
     var pos   = $(this).parent(); 
     var position = pos.position();  

     $(this).parent().animate({ height: '150', width: '200', top: position.top, left: position.left }, 750); 
    }); 

嘗試了上述,但它獲得了新的位置不是原來的。

在此先感謝。

PVS

回答

12

可以使用,當第一次加載頁面$.data()並在以後使用它,喜歡這家店的位置:

$(".work-item .toggle").each(function() { 
    $.data(this, 'position', $(this).parent().position()); 
}).toggle(function() { 
    $(this).parent().animate({ height: '430', width: '930', top: '0', left: '0' }, 750); 
}, function() { 
    var position = $.data(this, 'position'); 
    $(this).parent().animate({ height: '150', width: '200', top: position.top, left: position.left }, 750); 
}); 

此存儲.position()每個元素的父綁定之前,則在稍後動畫時使用它。

+0

謝謝尼克!作品一種享受。 – 2010-10-04 11:58:12