简体   繁体   English

为什么这个jQuery动画循环不起作用?

[英]why this jQuery animation loop doesn't work?

<script>
function swim() {

    $("#ship").animate({left: "-=-540px"}, 3000, function() {

        $("#ship").css("-moz-transform", "scaleX(-1)");
        $("#ship").css("-o-transform", "scaleX(-1)");
        $("#ship").css("-webkit-transform", "scaleX(-1)");
        $("#ship").css("transform", "scaleX(-1)");
        $("#ship").css("filter", "FlipH");
        $("#ship").css("-ms-filter", "FlipH");
        $("#ship").animate({left: "-=540px"}, 3000);
        swim();

        })

    }


    swim();

</script>

When I use only document.ready works fine, but stops after an attempt to turn it to a loop. 当我仅使用document.ready效果很好,但是在尝试将其循环后停止。 There must be a syntax bug somewhere but I can't figure out where. 某处一定有语法错误,但我不知道在哪里。

EDIT: nvm. 编辑:nvm。 It was the case of putting the script to <head> instead of <body> . 就是将脚本放到<head>而不是<body>

There's a reason why it needs the document ready wrapper. 有一个原因需要文件包装器。 A "normal" function isn't an equal replacement for it. “正常”功能不能等同地替代它。 If you call the function before the DOM is ready, the #ship selector doesn't match anything, the animation doesn't run, and the callback isn't executed. 如果在DOM准备就绪之前调用该函数,则#ship选择器不匹配任何内容,动画不运行,并且不执行回调。

Call the function in a document ready event: 在文档准备事件中调用该函数:

$( swim );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM