How to change the css attribute of #header using jQuery and an array of images?

后端 未结 1 890
一向
一向 2021-01-24 16:43

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

相关标签:
1条回答
  • 2021-01-24 17:21

    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

    function changeBg() {
        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.

    0 讨论(0)
提交回复
热议问题