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?
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).
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.
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 .
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);
});
You could use setTimeout:
$("#dMyDiv").stop(true, true).animate({
backgroundColor: "#aa0000"
}, 1000, function() {
setTimeout(function() {
$(#dMyDiv").animate({
backgroundColor: "#fff"
}, 1000);
}, 10 * 1000);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With