September 9, 2013

Jquery delay background animation not working

GEspinha’s Question:

I don’t understand why this doesn’t work:

$('#myDiv').css('background', 'url("Img/image.gif")').delay(500).css('background', 'none');

I want #myDiv to have a background and it be removed after 500ms.

Can anyone help on this please?

css is not an animation, therefore delay does not affect it.

Use setTimeout instead:

var $div = $('#myDiv').css('background', 'url("Img/image.gif")');

setTimeout(function(){
    $div.css('background', 'none');
}, 500);

That is because delay() works with animations and also we cannot animate between bitmap data like images try using fadeIn() and fadeOut() instead to create the fading background effect.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!