[英]Jquery animate() once then stop
Im trying to make a box animate when i scroll down to its position, but the animation wont stop, the element just keeps repeating the animation forever. 当我向下滚动到其位置时,我试图使一个框动起来,但是动画不会停止,该元素只是一直重复播放动画。 The code im using :
我使用的代码是:
jQuery(document).scroll(function (e) {
var value = jQuery(this).scrollTop();
if (value = 600) {
jQuery( "body" ).addClass( "scroll" );
jQuery('#car1').animate({top: '+=50px'}, 2000);
}
});
A single =
isnt a comparison operator. 单个
=
不是比较运算符。 It is in fact assigning a value to your variable. 它实际上是为变量分配一个值。
That being said, your condition : 话虽如此,您的情况:
if (value = 600)
is always true since 600
is a truthy value. 始终为真,因为
600
是真实值。
Try using ==
or ===
. 尝试使用
==
或===
。
To anime once, you can use the class you are inputing on the body. 对于动漫,您可以使用要在身体上输入的类。 Just invert your 2 lines and do a condition :
只需反转您的两行,并做一个条件:
if(!jQuery("body").hasClass("scroll")) //Animate only if it haven't the class scroll
jQuery('#car1').animate({top: '+=50px'}, 2000);
jQuery( "body" ).addClass( "scroll" );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.