I know I'm late, but I found a way to use jquery that works in every browser (I tested it on chrome, firefox and Ie 9), and fore-ground elements are always displayed instead of the css3 transition property.
create 2 absolute wrapper and use z-index.
First set the elements that should be in the foreground with the highest value of the z-index property, and other elements (all of which are included in the body, like this: body {}) with a lower value of the z-index property than the front ground elements, at least , 2 numbers lower.
HTML part:
<div class="wrapper" id="wrapper1"></div> <div class="wrapper" id="wrapper2"></div>
css part:
.fore-groundElements{ //select all fore-ground elements z-index:0; //>=0 } .wrapper{ background-size: cover; width:100%; height:100%; background-size: 100% 100%; position:absolute; }
than javascript / jquery one:
I needed to change the background image every three seconds, so I used the dial timeout.
this is the code:
$(document).ready(main); var main = function(){ var imgPath=[imagesPath1,..,...]; // the array in which store the image paths var newWrapper; // the wrapper to display var currentWrapper; //the current displayed wrapper which has to be faded var l=2; // the next image index to be displayed, it is set to 2 because the first two position of the array(first two images) start already setted up var imgNumber= imgPath.length; //to know when the images are over and restart the carousel var currentImg; //the next image path to be displayed $('#wrapper1').css("background-image", 'url'+imgPath[0]); //pre set the first wrapper background images $('#wrapper2').css("background-image", 'url'+imgPath[1]); //pre set the first wrapper background images setInterval(myfunction,3000); //refresh the background image every three seconds function myfunction(){ if(l===imgNumber-1){ //restart the carousel if every single image has already been displayed l=0; }; if(l%2==0||l%2==2){ //set the wrapper that will be displaied after the fadeOut callback function currentWrapper='#wrapper1'; newWrapper='#wrapper2'; }else{ currentWrapper='#wrapper2'; newWrapper='#wrapper1'; }; currentImg=imgPath[l]; $(currentWrapper).fadeOut(1000,function(){ //fade out the current wrapper, so now the back-ground wrapper is fully displayed $(newWrapper).css("z-index", "-1"); //move the shown wrapper in the fore-ground $(currentWrapper).css("z-index","-2"); //move the hidden wrapper in the back ground $(currentWrapper).css("background-image",'url'+currentImg); // sets up the next image that is now shown in the actually hidden background wrapper $(currentWrapper).show(); //shows the background wrapper, which is not visible yet, and it will be shown the next time the setInterval event will be triggered l++; //set the next image index that will be set the next time the setInterval event will be triggered }); }; //end of myFunction } //end of main
Hope my answer is clear if you need to clarify the comment more.
sorry for my English:)
Muccagelato Sep 02 '16 at 12:28 2016-09-02 12:28
source share