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.