jQuery 动画循环卡在第一个循环中

jQuery animate loop stuck in first loop

我正在尝试在 jQuery 上制作一个简单的动画。这个想法是将图像向右移动,然后返回到开始的位置。

A 编写了这段代码:

function logo(img) {
    $(img).animate( {
       left: 100
    }, 900, function() {
       $(img).animate({
          left: 0,
       }, 900,  logo(img))
    })
}    

还有这个HTML

<div style="position: relative; width:500px; height: 55px; overflow: visible;">
<img class="pena" src="https://www.google.com.br/images/srpr/logo11w.png" width="50px" style="position: absolute; left: 0px; top: 0;" onclick="logo(this);">
</div>

http://jsfiddle.net/rtfmu5za/1/

但是在第一次动画的时候运行,她卡了一小会儿。之后一切正常。

我错过了什么?

您的代码确实存在一个奇怪的问题,我无法真正解释为什么会这样。试试这个解决方案,对我有用:

var position = 0;

function logo(img) {
    position = position == 0 ? 100 : 0;
    $(img).animate( {
        left: position
    }, 900, function () { logo(this) });
}

http://jsfiddle.net/oqLakcbj/