我正在开发一个网站,其中有一些绝对定位的 div,我需要在单击时调整其大小,然后这些将填充 div 所在的容器。问题是如何让它们切换到去返回到原始位置(顶部、左侧),每个位置都不同。
$(".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
您可以使用以下方式存储页面首次加载时的位置$.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()在绑定之前为每个元素的父元素设置,然后在稍后动画返回时使用它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)