user1063192
user1063192

Reputation: 157

How to add a 3 second delay to this function

My goal with this code is to create an effect of leaves blowing away 3 seconds after page load, but currently I am unable to create a delay. Perhaps its because the code format. I made i quick jsfiddle to demonstrate what I have so far.

http://jsfiddle.net/vXpDk/

So my question is how to create a delay of the function so that it doesn't start to rotate and slide for 3 seconds...and if it is possible to to create diagonal paths instead of horizontal and vertical.

Here's the code from the jsFiddle:

var $elie = $("#leaf1"), degree = 0, timer; 

rotate();

function rotate() {
    $elie.delay(2000)
        .css({ WebkitTransform: 'rotate(' + degree + 'deg)'})
        .animate({ "left": "+=800px" }, 2000)
        .fadeOut(100);  
    $elie.delay(2000)
        .css({ '-moz-transform': 'rotate(' + degree + 'deg)'})
        .animate({ "left": "+=800px" }, 2000)
        .fadeOut(100);   

    timer = setTimeout(function() {
        ++degree; 
        rotate();
    },0);
}

Upvotes: 1

Views: 565

Answers (1)

Cheery
Cheery

Reputation: 16214

Like this? http://jsfiddle.net/L69Ud/

    var $elie = $("#leaf1"), degree = 0; 

    $elie.animate({ "left": "+=800px" }, 5000).fadeOut(100); 
    setTimeout(rotate, 3000);

    function rotate() {
    $elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});
    $elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});   

    setTimeout(function() {
            ++degree; rotate();
            }, 0);
    }

and if it is possible to to create diagonal paths instead of horizontal and vertical.

Animate left and top at the same time http://jsfiddle.net/L69Ud/1/

The only modification here is

$elie.animate({ left: "+=500px", top: "+=500px" }, 5000).fadeOut(100); 

How would you code this so that the div does not move at all for 3 seconds then begins to rotate and slide at the same time? http://jsfiddle.net/L69Ud/3/

    var $elie = $("#leaf1"), degree = 0; 

    setTimeout(function() {
       $elie.animate({ left: "+=500px", top: "+=500px" }, 5000).fadeOut(100); 
       rotate();
    }, 3000);

    function rotate() {
        $elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});
        $elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});   

        setTimeout(function() {
                ++degree; rotate();
                }, 0);
    }

Upvotes: 1

Related Questions