What you need to do is stack the two elements on top of each other. Then do one fadeout and another fadein.
This is how I will do it ...
css ...
#background-images { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 1; } #bImg1 { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 3; background: url(starting-img1.jpg) no-repeat; background-size: contain; } #bImg2 { display: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 2; background: url(starting-img2.jpg) no-repeat; background-size: contain; } .container { width: 960px; height: 900px; background: rgba(255,255,255,.7); margin: auto; position: relative; z-index: 10; }
html ...
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <html> <body> <div id="background-images"> <div id="bImg1"></div> <div id="bImg2"></div> </div> <div class="container"> Content Here </div> </body> </html>
script ...
var imageSet1 = ["image1.jpg", "image2.jpg", "image3.jpg"]; var currentImageSet1 = 0; var imageSet2 = ["image4.jpg", "image5.jpg", "image6.jpg"]; var currentImageSet2 = 0; function changeBackgroundImages() { img1Fade(); setTimeout(img2Fade, 2000); } function img1Fade(){ $('#bImg1').fadeOut('slow', function(){$('#bImg1').css({background: 'url(' + imageSet1[++currentImageSet1] + ')'})}); $('#bImg2').fadeIn('slow'); if (currentImageSet1 >= imageSet1.length - 1) { currentImageSet1 -= imageSet1.length; }; } function img2Fade(){ $('#bImg2').fadeOut('slow', function(){$('#bImg2').css({background: 'url(' + imageSet2[++currentImageSet2] + ')'})}); $('#bImg1').fadeIn('slow'); if (currentImageSet2 >= imageSet2.length - 1) { currentImageSet2 -= imageSet2.length; }; } $(document).ready(function(){ setInterval(changeBackgroundImages, 5000); });
You will need to bother with time so that it looks good. Make sure you set your urls in the images in the image array or when the stings are created in css.
source share