如何重启/重置Jquery动画 [英] How to restart/reset Jquery animation

查看:440
本文介绍了如何重启/重置Jquery动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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的情况下将元素重置为它的基本样式,只需在动画结束时删除该属性 - 参见 jQuery API上的.animate()

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动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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