animImage('history_main_image.png');
function animImage(img) {
$('#slider').delay(3000).animate({opacity: 0}, 'slow', function(img) {$('#slider').css('background-image','url(images/' + img +')').animate({opacity: 1});});
}
i have made it a function to animate the background in the slider element.but it doesn’t work.when i check the output in chrome’s inspect element option.it shows background-image:images/undefined
The callback you pass to
animate()takes animgargument and uses it to build an URL to the background image. That won’t work, becauseanimate()does not pass any argument to your callback.Instead, you can remove that argument and use the
imgargument originally passed toanimImage(). That variable exists in the caller’s scope, so it will be captured and accessible inside your callback: