Can I fade in a background image (CSS: background-image) with jQuery?

后端 未结 9 1065
一生所求
一生所求 2020-11-29 00:52

I have a div element with text in it and a background image, which is set via the CSS property background-image. Is it possible to fade in the back

相关标签:
9条回答
  • 2020-11-29 01:24

    It's not possible to do it just like that, but you can overlay an opaque div between the div with the background-image and the text and fade that one out, hence giving the appearance that the background is fading in.

    0 讨论(0)
  • 2020-11-29 01:26

    You can give opacity value as

    div {opacity: 0.4;}
    

    For IE, you can specify as

    div { filter:alpha(opacity=10));}
    

    Lower the value - Higher the transparency.

    0 讨论(0)
  • 2020-11-29 01:27

    I know i'm late, but I found a way using jquery which works on every browser(i tested it on chrome, firefox and Ie 9)and th fore-ground elements are always displayed instead of css3 transition property.

    create 2 absolute wrapper and using z-index.

    First set the elements that have to be in the fore-ground with the highest z-index property value, and the other elemets(all included in the body, so: body{}) with a lower z-index property value than the fore-ground elements'one , at least of 2 number 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;
             }
    
            #wrapper1{
                   z-index:-1; 
            }
    
    
            #wrapper2{
                     z-index:-2;
             }
             body{
    
                  height:100%;
                  width:100%;
                  margin:0px;   
                  display:cover;
                  z-index:-3                          //<=-3
             }
    

    than the javascript/jquery one:

    i needed to change the background image every three second so i used a set 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
    

    i hope that my answer is clear,if you need more explanation comment it.

    sorry for my english :)

    0 讨论(0)
提交回复
热议问题