开发者

jquery loop through different backgrounds

I'm trying to change different backgrounds for a div looping through an array of images and timing it every 5 sec.

here's my code:

function changeBG(){

     //array of backgrounds
     var a开发者_如何学Pythonrray = ["test.jpg", "test2.jpg", "test3.jpg",];


     for ( var i=0, len=array.length; i<len; ++i){
         $('.round-mask').css('background-image', 'url("images/work/'+array[i]+'")');

      }

}

window.setInterval(changeBG(), 5000);

This is not working, I can see it is looping, but I'm getting always the 3rd image.

Any idea?

Thanks in advance.

Mauro


Please Try~

    <script  type="text/javascript">
    var now = 0;
    var int = self.setInterval("changeBG()", 1000);
    var array = ["001.jpg", "002.jpg", "003.jpg", ];

    function changeBG(){
        //array of backgrounds
        now = (now+1) % array.length ;
        $('.round-mask').css('background-image', 'url("' + array[now] + '")');
    }
</script>

and forget about this~

 for ( var i=0; i<array.length; i++){
 $('.round-mask').css('background-image', 'url("images/work/'+array[i]+'")');

  }


something like this should work:

var i = 0;

function changeBG(){

     //array of backgrounds
     var array = ["test.jpg", "test2.jpg", "test3.jpg",];



    $('.round-mask').css('background-image', 'url("images/work/'+array[i]+'")');

    if(i == array.length -1){
         i= 0;
    }
    else{
        i++;
    }


}


This really worked. 2014.

var i =0 ;

//array of backgrounds
var array = [
    "1.jpg", 
    "2.jpg", 
    "3.jpg", 
    "4.jpg", 
    "5.jpg", 
    "6.jpg", 
    "7.jpg", 
    ];

function changeBG(){
    if ( i > array.length -1) {
        i = 0;
    }

    $('.slideContainer').css('background-image', 'url("img/backgrounds/'+array[i]+'")');
    i++;
}

window.setInterval("changeBG()", 5000);


try this out

var i =0 ;

//array of backgrounds
var array = ["test.jpg", "test2.jpg", "test3.jpg"];

function changeBG(){
    if ( i > array.length -1) {
        i = 0;
    }

    $('.round-mask').css('background-image', 'url("images/work/'+array[i]+'")');
    i++;
}

window.setInterval(changeBG(), 5000);


$(element).on("click",function(){
  $("<div>").css("background-image", "url("+array[(i-1)]+")");
if (i == array.length){
 i=1;
}
else {
 i++;
}
})

This is my edit, so actually it run Or

$(element).css("background-image", "url("+array[(i-1)]+")");
i == bb.length ? i=1 : i++;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜