【问题标题】:Can I fade in a background image (CSS: background-image) with jQuery?我可以使用 jQuery 淡入背景图像(CSS:背景图像)吗?
【发布时间】:2011-11-11 06:48:43
【问题描述】:

我有一个 div 元素,其中包含文本和一个背景图像,这是通过 CSS 属性 background-image 设置的。是否可以通过jQuery淡入背景图像?

div {
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
  background-image: url(http://upload.wikimedia.org/wikipedia/commons/8/84/Konqi_svg.svg);
  border: 1px solid #666;
  height: 10em;
}
<div>Text</div>

编辑

我制作了一个fiddle 来举例说明我的场景。基本上,这会配置一个初始的background-image 和一个transition,并使用jQuery 更改background-image。在我的测试中,两张图片之间没有动画过渡。

EDIT2

我修改后的fiddle 有效!

【问题讨论】:

  • Fading in background image with jQuery 的可能重复项以及大约一百万个其他问题。
  • @Juhana:这些问题是否与通过 CSS 属性 background-image 设置的背景图像有关?另外,它们是否涉及通过 JavaScript 更改 background-image
  • 是的,而且每个答案都是相同的(没有多个 div 是不可能的)。
  • 查看Fading in background image with jQuery 的答案,AFAICT 几乎没有定论。它只显示了 jQuery animate 可以做什么。它也没有涉及 CSS3 选项。

标签: jquery html css


【解决方案1】:

我一直在寻找年龄,最后我把所有东西放在一起找到我的解决方案。 人们说,你不能淡入/淡出 html-background 的 background-image-id。这绝对是错误的,你可以通过实现下面提到的演示来弄清楚

CSS:

html, body

height: 100%;   /* ges Hoehe der Seite -> weitere Hoehenangaben werden relativ hierzu ausgewertet */
overflow: hidden;   /*  hide scrollbars */
opacity: 1.0;
-webkit-transition: background 1.5s linear;
-moz-transition: background 1.5s linear;
-o-transition: background 1.5s linear;
-ms-transition: background 1.5s linear;
transition: background 1.5s linear;

现在可以使用 JavaScript 轻松更改 body 的背景图像:

switch (dummy)

case 1:

$(document.body).css({"background-image": "url("+URL_of_pic_One+")"});
waitAWhile();

case 2:
$(document.body).css({"background-image": "url("+URL_of_pic_Two+")"});
waitAWhile();

【讨论】:

  • 如果它对 jQuery 有帮助:$(document).ready(function () { var img_array = ['home-corps-2.jpg', 'home-epilation.jpg', 'home-corps.jpg', 'home-ongles.jpg']; var index = 0; function fadeToNext(){ index = (index + 1) % img_array.length; $('.headline').css({'background-image':'url(public/img/'+img_array[index]+')'}); waitNext(); } function waitNext(){ setTimeout(function() { fadeToNext(); },3000); } waitNext();});
  • @user2947794:没有“waitAWhile”代码?如果你有它,请发布它。谢谢。
  • 效果很好。唯一的批评是,当旋转不同尺寸的图像(纵向到横向,反之亦然)时,除了淡入淡出动画之外,还会出现拉伸动画。
  • @blumonde 你知道那部分不需要吗?
【解决方案2】:

这对我有用,它的纯 CSS


css

html {
    padding: 0;
    margin: 0;
    width: 100%;
    height: 100%;
  }

  body {
    padding: 0;
    margin: 0;
    width: 100%;
    height: 100%;
  }

  #bg {
    width: 100%;
    height: 100%;
    background: url('/image.jpg/') no-repeat center center fixed;

    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;

    -webkit-animation: myfirst 5s ; /* Chrome, Safari, Opera */
    animation: myfirst 5s ;
  }

  /* Chrome, Safari, Opera */
  @-webkit-keyframes myfirst {
    from {opacity: 0.2;}
    to {opacity: 1;}
  }

  /* Standard syntax */
  @keyframes myfirst {
    from {opacity: 0.2;}
    to {opacity: 1;}
  }

html

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
  <div id="bg">
    <!-- content here -->
  </div> <!-- end bg -->
</body>
</html>

【讨论】:

    【解决方案3】:

    对于现代浏览器,我更喜欢带有一点 Js 和 CSS3 的轻量级方法...

    transition: background 300ms ease-in 200ms;
    

    看这个演示:

    http://codepen.io/nicolasbonnici/pen/gPVNbr

    【讨论】:

      【解决方案4】:

      您可以将不透明度值设为

      div {opacity: 0.4;}
      

      对于IE,可以指定为

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

      降低值 - 提高透明度。

      【讨论】:

      • opacity 的问题在于它会影响所有孩子,而不仅仅是背景图片。
      【解决方案5】:

      这样做是不可能的,但是您可以在带有背景图像和文本的 div 之间覆盖一个不透明的 div,然后将其淡出,从而使外观看起来背景正在淡入。

      【讨论】:

      • 可以,但是您不能在生产环境中使用 CSS3。不过玩起来还是不错的。
      【解决方案6】:

      jquery:

       $("div").fadeTo(1000 , 1);
      

      css

          div {
           background: url("../images/example.jpg") no-repeat center; 
          opacity:0;
          Height:100%;
          }
      

      html

      <div></div>
      

      【讨论】:

        【解决方案7】:

        自 Firefox 4 起,您可以通过多种方式淡化背景图像 ...

        你的 CSS:

        .box {
            background: #CCCCCC;
            -webkit-transition: background 0.5s linear;
            -moz-transition: background 0.5s linear;
            -o-transition: background 0.5s linear;
            transition: background 0.5s linear;
            }
        .box:hover {
            background: url(path/to/file.png) no-repeat #CCCCCC;
            }
        

        你的 XHTML:

        <div class="box">
            Some Text …
        </div>
        

        就是这样。

        【讨论】:

          【解决方案8】:

          所以.. 我也在研究这个问题,发现这里的大多数答案都是要求淡化容器元素,而不是实际的背景图像。 然后我想到了一个黑客。我们可以给多个背景吗?如果我们覆盖其他颜色并使其透明怎么办,如下面的代码-

           background: url("//unsplash.it/500/400") rgb(255, 255, 255, 0.5) no-repeat center;
          

          此代码实际上是独立运行的。试试吧。我们给了一张背景图,并在图像顶部询问了其他具有透明度的白色,瞧。 提示- 我们可以提供不同的颜色和透明度来获得不同的滤镜效果。

          【讨论】:

          • 虽然我很喜欢这个主意,但这对我不起作用。我在 Chrome 86 上。
          【解决方案9】:

          我知道我迟到了,但我找到了一种使用 jquery 的方法,该方法适用于每个浏览器(我在 chrome、firefox 和 Ie 9 上对其进行了测试)并且总是显示前景元素而不是 css3 转换属性。

          创建 2 个绝对包装器并使用 z-index。

          首先将必须在前景中的元素设置为具有最高的 z-index 属性值,并设置其他元素(都包含在 body 中,因此:body{})具有较低的 z-index 属性值比前景元素的一个,至少低 2 个。

          HTML 部分:

                   <div class="wrapper" id="wrapper1"></div>
                   <div class="wrapper" id="wrapper2"></div>
          

          css 部分:

                  .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
                   }
          

          比 javascript/jquery 之一:

          我需要每三秒更改一次背景图像,所以我使用了设置的超时时间。

          这是代码:

            $(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
          

          我希望我的回答是清楚的,如果您需要更多解释,请评论它。

          对不起我的英语:)

          【讨论】:

            猜你喜欢
            • 2012-08-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-09-28
            相关资源
            最近更新 更多