jQuery(document).ready(function() { $(document).foundation(); var holder = '#my-hero'; var holdertofade = '#my-hero2'; var images = ['http://www.maspi.net/cover-zurb/_MG_2293.jpg','http://www.maspi.net/cover-zurb/_MG_9644.jpg','http://www.maspi.net/tinymce_imatges/pagines/terrassa-gron.jpg','http://www.maspi.net/cover-zurb/_MG_2313.jpg','http://www.maspi.net/cover-zurb/_MG_2289.jpg']; $.each(images, function(i, img){ images[i] = img; }); var index = 0; var imatge=new Image(); $.each(images, function(i, img){ imatge.src=img; }); var canvi=0; setInterval(function() { //holder=( holder=='#my-hero' ? '#my-hero2':'#my-hero'); if(canvi==0){ if(holder=='#my-hero'){holder='#my-hero2';}else{holder='#my-hero';} $(holder).css('background-image','url('+images[index]+')'); // console.log('loaded:'+images[index]+' on '+ holder ); canvi=1; ++index; index = (images[index] ? index : 0); }else{ if(holdertofade=='#my-hero'){holdertofade='#my-hero2';}else{holdertofade='#my-hero';} $(holdertofade).css('opacity',0); $(holder).css('opacity',100); // console.log('faded:'+holder+' to 100' ); canvi=0; } }, 4000); });