问题描述
如何在jquery中重置动画?例如:
How can i reset an animation in jquery ? For example:
CSS
.block {
position:absolute;
top: 0;
left: 0;
}
JS:
$('.block').animate({
left: 50,
top: 50
});
如果我这样做:
$('.block').stop();
动画将停止。但是我怎样才能清除这个位置,重新开始呢?从点0,0。
the animation will stop. But how can i clear the position, to start over ? from point 0,0.
推荐答案
当jQuery为元素设置动画时,它会在<$ c $中添加样式相关信息c> style 属性。如果你需要在没有jQuery css的情况下将元素重置为它的基本样式,只需在动画结束时删除该属性 - 参见。
When jQuery is animating an element, it adds the style related information in the style
attribute. If you need to reset the element to it's base style without the jQuery css, simply remove this attribute at the end of the animation - See .animate() on jQuery API.
$('.block').animate({
left: 50,
top: 50
}, 'slow', function () { $(this).removeAttr('style'); });
回调函数将删除样式
属性并在动画结束时重置元素。
The callback function will remove the style
attribute and reset the element at the end of the animation.
这篇关于如何重启/重置Jquery动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!