0
<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. There must be a syntax bug somewhere but I can't figure out where.

EDIT: nvm. It was the case of putting the script to <head> instead of <body>.

2
  • 1
    if function(){} you can use this insead of repeating query. Commented Aug 12, 2013 at 10:14
  • missing semicolon at end of $("#ship").... statement. what I don't think that might be the reason. Commented Aug 12, 2013 at 10:15

1 Answer 1

1

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.

Call the function in a document ready event:

$( swim );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.