Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery animate but then come back to normal

Consider a div "dMyDiv"

I notice in jquery I can animate so this:

$( "#dMyDiv").animate({
         backgroundColor: "#aa0000"
  }, 1000 );

Brings a red like colored background to my div. But is there a way to then after say 10 seconds go back to my normal white background of my div? I see there is a callback function but Im not sure if it is possible to start the animation, and once the animation is done to go back to my regular background (before the animation). Should I just do this:

 $( "#dMyDiv").animate({
             backgroundColor: "#aa0000"
          }, 1000, function() {
           $(#dMyDiv").animate({ backgroundColor: "#fff" }, 1000);
          };
);

Meaning after the red animate, just animate again to white?

like image 676
oJM86o Avatar asked Apr 24 '12 23:04

oJM86o


People also ask

How do you stop the currently running animation in jQuery?

The jQuery stop() method is used to stop an animation or effect before it is finished. The stop() method works for all jQuery effect functions, including sliding, fading and custom animations. Syntax: $(selector).

How can use a animate () method in jQuery?

The jQuery animate() method is used to create custom animations. Syntax: $(selector).animate({params},speed,callback); The required params parameter defines the CSS properties to be animated.

What is the default easing used for jQuery animation?

An easing function specifies the speed at which the animation progresses at different points within the animation. The only easing implementations in the jQuery library are the default, called swing , and one that progresses at a constant pace, called linear .


2 Answers

You're right. the third parameter is a function that executes once the animation is complete.

$("#dMyDiv").stop().animate({backgroundColor:'#aa0000'},1000,function(){
    $(this).animate({backgroundColor:'#fff'},1000);
});
like image 70
lu1s Avatar answered Sep 21 '22 08:09

lu1s


You could use setTimeout:

$("#dMyDiv").stop(true, true).animate({

    backgroundColor: "#aa0000"

}, 1000, function() {

   setTimeout(function() {       

       $(#dMyDiv").animate({ 

           backgroundColor: "#fff" 

       }, 1000);

   }, 10 * 1000);

});
like image 25
Code Maverick Avatar answered Sep 22 '22 08:09

Code Maverick