jQuery动画返回原始位置 [英] jQuery animating back to original position

查看:300
本文介绍了jQuery动画返回原始位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在使用的网站,该网站上有一些绝对定位的div,单击时需要调整其大小,然后将这些填充到div所在的容器中.问题是如何使它们进入切换回原来的位置(顶部,左侧),每个位置都不相同.

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.

预先感谢.

PVS

推荐答案

您可以使用 $.data() 并稍后使用,就像这样:

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);
});

这将在绑定之前为每个元素的父级存储 .position() 稍后动画.

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

这篇关于jQuery动画返回原始位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆