Although I may be a bit delayed in my response, I have discovered a method utilizing jQuery that is effective across all browsers (tested on Chrome, Firefox, and IE 9). This approach ensures that the foreground elements are consistently displayed without relying on the CSS3 transition property.
The technique involves creating two absolute wrappers and strategically assigning z-index values. The key is to set the elements intended for the foreground with the highest z-index value, while placing all other elements (within the body) at a lower z-index value, at least two numbers lower than the foreground elements.
In the HTML section:
<div class="wrapper" id="wrapper1"></div>
<div class="wrapper" id="wrapper2"></div>
For the CSS styling:
.fore-groundElements {
z-index: 0;
}
.wrapper {
background-size: cover;
width: 100%;
height: 100%;
position: absolute;
}
#wrapper1 {
z-index: -1;
}
#wrapper2 {
z-index: -2;
}
body {
height: 100%;
width: 100%;
margin: 0px;
display: cover;
z-index: -3;
}
As for the JavaScript/jQuery implementation:
I needed to cycle through different background images every three seconds, so I utilized a set timeout function for this task.
The code snippet is as follows:
$(document).ready(main);
var main = function() {
var imgPath = [imagesPath1, ..., ...];
var newWrapper;
var currentWrapper;
var l = 2;
var imgNumber = imgPath.length;
var currentImg;
// Pre-set the initial background images
$('#wrapper1').css("background-image", 'url' + imgPath[0]);
$('#wrapper2').css("background-image", 'url' + imgPath[1]);
// Refresh the background image every three seconds
setInterval(myfunction, 3000);
function myfunction() {
if (l === imgNumber - 1) {
l = 0;
}
if (l % 2 == 0 || l % 2 == 2) {
currentWrapper = '#wrapper1';
newWrapper = '#wrapper2';
} else {
currentWrapper = '#wrapper2';
newWrapper = '#wrapper1';
}
currentImg = imgPath[l];
$(currentWrapper).fadeOut(1000, function() {
$(newWrapper).css("z-index", "-1");
$(currentWrapper).css("z-index", "-2");
$(currentWrapper).css("background-image", 'url' + currentImg);
$(currentWrapper).show();
l++;
});
};
}
If further explanation is required, feel free to leave a comment. Apologies for any language barriers!