0

I have a set of jQuery effects that I would like to have run one after the other (currently they run simultaneously. Is there any way to fix this?

The code is as follows:

 <SCRIPT>
$(document).ready(function() {

    $(".right").click(function () {
         $(this).show("slide", { direction: "right" }, 1200);
    });

    // Trigger the handler once on document ready
    $(".right").click();

});
</SCRIPT>
<SCRIPT>
$(document).ready(function() {

    $("#left").click(function () {
         $(this).show("slide", { direction: "left" }, 1200);
    });

    // Trigger the handler once on document ready
    $("#left").click();

});
</SCRIPT>
<SCRIPT>
$(document).ready(function() {

    $("#up").click(function () {
         $(this).show("slide", { direction: "down" }, 1200);
    });

    // Trigger the handler once on document ready
    $("#up").click();

});
</SCRIPT>
Lily Evans
  • 27
  • 1
  • 1
  • 5

4 Answers4

0

You need to use callbacks. Callbacks are functions that run when another function is complete, in this case your animation.

http://api.jquery.com/show/

Also you do not need so many declarations. Try is like this

<script>

    $(document).ready(function(){

           $('.right').show("slide", { direction: "right" }, 1200, function(){
                  //first callback
                  $('#left').show("slide", { direction: "left" }, 1200, function(){
                          //second callback
                          $('#up').show("slide", { direction: "down" }, 1200);
                  });
           });

    });

</script>

If you want animations to also trigger on click, you can do so by using the same .click statements you have now, just no need to //Trigger the handler on document ready

Fresheyeball
  • 29,567
  • 20
  • 102
  • 164
0

Is this something you after!

$(document).ready(function() {

        $(".right").click(function () {
             $(this).show("slide", { direction: "right" }, 1200, function() {
                  $(".right").click();
             })
        });
    });
Dips
  • 3,220
  • 3
  • 20
  • 21
0

It depends on how you going to sequence it.. Check below as an example and modified as you wanted it,

The final argument of the .show is a call back function which will be called after executing .show

<SCRIPT>
$(document).ready(function() {
    $(".right").click(function () {
         $(this).show("slide", { direction: "right" }, 1200, function () {
                // Trigger the handler once on document ready
                $("#left").click();
         });
    });

    $("#left").click(function () {
         $(this).show("slide", { direction: "left" }, 1200, function () {
              $("#up").click();
         });
    }); 

    $("#up").click(function () {
         $(this).show("slide", { direction: "down" }, 1200);
    });

    // Trigger the handler once on document ready
    $(".right").click();
});
</SCRIPT>
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
0

You should call the next animation the first animations callback to animate them in sequence and the callback is the last argument of the show method.

Also instead of using multiple $(document).ready() you can club all together in just one.

$(document).ready(function() {
    $("#left").click(function () {
         $(this).show("slide", { direction: "left" }, 1200, function () {
              $("#up").click();//Trigger up click
         });
    }); 

    $("#up").click(function () {
         $(this).show("slide", { direction: "down" }, 1200);
    });

    $(".right").click(function () {
         $(this).show("slide", { direction: "right" }, 1200, function () {
              $("#left").click();//Trigger left click
         });
    }).click();//Trigger the right click
});
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124