Skip to content Skip to sidebar Skip to footer

How To Change The Css Attribute Of #header Using Jquery And An Array Of Images?

I am trying to edit a script that changes the images of a div as shown below, to change the background-image property of #header. As you can see in my fiddle here, http://jsfiddle

Solution 1:

You need a couple of fixes:

  • Use a simple increment to keep track of the current image.
  • typo: "images" -> "imgs"
  • $("img") doesn't select anything, so the JQuery fadeto "complete" callback is never fired (I assume you meant to fade the div itself)
  • you need "url('http://...')" for the "background-image" CSS property

functionchangeBg() {
    if (i >= imgs.length) i=0;
    $('#header').fadeTo('slow',opacity,function(){
        var val = "url('" + imgs[i++] + "')";
        console.log(val);
        $('#header').css('background-image',val).fadeTo('slow',1);
    });
}

Here is the updated demo.

Post a Comment for "How To Change The Css Attribute Of #header Using Jquery And An Array Of Images?"