“stop”方法不适用于jQuery中的图像元素

HTML:

  

jQuery的:

 var hel = $('.heli'); fly(); function fly() { hel.animate({top :'-=25px' ,left :'+=25px'},800,CollisonCheck) } function CollisonCheck() { var tp =$(this).offset().top; var lft = $(this).offset().left; if (((tp>=365) && (tp=241) && (lft<=370))) { stop_game(); } } function stop_game() { hel.stop(); } 

当我运行此代码时, stop()方法不起作用。 控制台也不会给出任何错误。

这段代码中的错误是什么? 如何解决这个问题呢?

只有在动画完成后才会调用CollisonCheck函数,因此stop()将不执行任何操作(因为它已经停止)。 你必须在某种间隔时调用“

 function fly() { hel.animate({top :'-=25px' ,left :'+=25px'},800) setInterval(CollisonCheck, 50); } function CollisonCheck() { var tp = hel.offset().top; var lft = hel.offset().left; if (((tp>=365) && (tp<=610)) && ((lft>=241) && (lft<=370))) { stop_game(); } } 

http://api.jquery.com/animate/

我为我的代码找到了另一种“停止”方法的解决方案。只需写“$ fx.off = true”而不是stop()。

 function stop_game() { $fx.off = true; }