jQuery animating back to original position

不问归期 提交于 2019-11-30 15:55:22

问题


I've got a site i'm working on that has a few absolutelty positioned divs that I need to resize on clicking, these will then fill the container that the divs are in. The question is how do I get them on toggle to go back to the original position (top, left) which is different for each.

$(".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);
    });

Tried the above but it's getting the new position not the original.

Thanks in Advance.

PVS


回答1:


You can store the position when the page first loads using $.data() and use it later, like this:

$(".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);
});

This stores the .position() for each element's parent just before you bind, then uses that when animating back later.



来源:https://stackoverflow.com/questions/3854877/jquery-animating-back-to-original-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!