【问题标题】:How to delay the display of the background image in a div如何延迟div中背景图片的显示
【发布时间】:2016-03-24 20:40:29
【问题描述】:

这是我的fiddle.


我只希望 css 中的“background-image:”完全加载并在 3 秒后显示并快速淡入效果,在此之前整个 div 必须为黑色。
如何在cssjavascript 中实现。

.initials {
    position:absolute;
    background:black;
    background-image: url("http://static.tumblr.com/lxn1yld/Hnxnxaqya/space.gif");
    color:white;
    margin-top:20px;
    padding-top:20px;
    padding-bottom:20px;
    width:60px;
    text-align:center;
    margin-left:20px;
    font-size:17px;
    letter-spacing:5px;
    box-shadow:0px 2px 3px rgba(0,0,0,.15);
    overflow: hidden;
    white-space: nowrap;
}
<div class="initials">A</div>

【问题讨论】:

  • 也许你可以使用CSS3 transition,否则你可能会考虑使用javascript (jQuery)。

标签: javascript jquery html css fiddle


【解决方案1】:

通过一些小的改动,我可能只用 CSS3 就可以实现你想要的。 检查小提琴:http://jsfiddle.net/w11r4o3u/

CSS:

.initials {
    position:relative;
    background:black;
    color:white;
    margin-top:20px;
    padding-top:20px;
    padding-bottom:20px;
    width:60px;
    text-align:center;
    margin-left:20px;
    font-size:17px;
    letter-spacing:5px;
    box-shadow:0px 2px 3px rgba(0,0,0,.15);
    overflow: hidden;
    white-space: nowrap;
}
.initials .text {
    position: relative;
}

@-webkit-keyframes test {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1
  }
}

.initials:before{
  content: "";
  background-image: url("http://static.tumblr.com/lxn1yld/Hnxnxaqya/space.gif");
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  -webkit-animation-name: test;
  -webkit-animation-duration: 3s;
  -webkit-animation-fill-mode: forwards;
  -webkit-animation-timing-function: ease-out;  
}

HTML:

<div class="initials"><div class="text">A</div></div>

已编辑

现在动画在 3 秒后开始,需要 0.3 秒才能完成。这是小提琴:http://jsfiddle.net/w11r4o3u/1/

  • 要调整淡入发生的“速度”,请编辑-webkit-animation-duration: .3s;

  • 如果要调整动画“延迟”启动,编辑-webkit-animation-delay: 3s;

【讨论】:

  • 请注意 OP 在 3 秒后要求快速淡入,而不是 3 秒淡入,可能需要相应地编辑您的答案
  • 已经进行了编辑,现在我还解释了哪些属性控制动画的“速度”和“延迟”。
  • 如何在显示之前完全加载图像,使淡入效果后没有图像缓冲区?
  • 图像已加载,即使由于不透明而未显示。在 Network 标签中检查您的 Chrome 开发工具
【解决方案2】:

也许像这样......但淡入很棘手 - AFAIK 你不能淡入背景图像属性

setTimeout(function(){
    $('.initials').css('background-image', 'url("http://static.tumblr.com/lxn1yld/Hnxnxaqya/space.gif")');
}, 3000)

另一种选择是将 HTML 分成 3 个部分,具有最高 z-index 的字母,然后是星星层上的黑色层...然后淡出黑色层

【讨论】:

    【解决方案3】:

    这里是解决方法,几秒钟后背景图片会出现。

    Demo

    HTML

    <div class="initials">A</div>
    

    CSS

    .initials {
        position:absolute;
        background-color:#000;
        color:white;
        margin-top:20px;
        padding-top:20px;
        padding-bottom:20px;
        width:60px;
        text-align:center;
        margin-left:20px;
        font-size:17px;
        letter-spacing:5px;
        box-shadow:0px 2px 3px rgba(0,0,0,.15);
        overflow: hidden;
        white-space: nowrap;
    }
    

    脚本

    $(document).ready(function() {
    setTimeout(function(){
        $('.initials').css('background-image', 'url("http://static.tumblr.com/lxn1yld/Hnxnxaqya/space.gif")');
    }, 3000);
    });
    

    【讨论】:

    • .fadeIn() 适用于您选择的 DOM 元素,而不适用于您通过 .css() 更改的属性;背景图像只是立即改变而没有适当的淡入淡出。
    • 如何让图像在显示之前完全加载?
    【解决方案4】:

    另一种选择是拥有一个单独的类并将该 css 分配给该类。看看这个fiddle

    首先你在 document.ready 中设置一个方法:

    $( document ).ready(function() {
           var changeClass = function() {
                 $(".initials").addClass("with-image");
           }
           setTimeout(changeClass, 3000);
    });
    

    然后在你的 css 中将首字母更改为:

     .initials {
         position:absolute;
         background:black;
         background-COLOR: black; 
     ...
    

    并添加:

     .with-image {
           background-color:none !important;
           background-image: url("http://static.tumblr.com/lxn1yld/Hnxnxaqya/space.gif");
      }
    

    【讨论】:

    • 谢谢!你现在看到了吗?
    • 如何让图片在显示前完全加载?
    • 我的网站包含大量图像,并且此图像与所有其他图像一起加载,使其在淡入淡出效果后加载一半。
    • 对于高速连接的人来说,它会很流畅,而对于中等连接的人来说,gif图像是逐帧加载的..
    • 当您在样式表中声明 background-image 时,无论您在文档中调用该类或不调用该类,浏览器解析您的样式表时都会立即下载该图像
    【解决方案5】:

    您可以使用背景图像属性创建一个绝对定位的 div,然后使用animate.css 添加动画,而无需自己创建关键帧

    http://jsfiddle.net/n9wd4ver/5/

    .initials {
      width: 100%;
      height: 100%;
    
    }
    
    .initials.initials-text {
      padding:20px 0px;
      position: relative;
      z-index: 2;
    }
    
    .initials.initials-background {
      position: absolute;
      background-image: url("http://static.tumblr.com/lxn1yld/Hnxnxaqya/space.gif");
      -webkit-animation-delay: 3s;
      -moz-animation-delay: 3s;
      -o-animation-delay: 3s;
      animation-delay: 3s;
      z-index: 1;
    }
    
    .initials-container {
        height: 100%;
        margin-top:20px;
        margin-left:20px;
        position:relative;
        background:black;
        color:white;
        width:60px;
        text-align:center;
        font-size:17px;
        letter-spacing:5px;
        box-shadow:0px 2px 3px rgba(0,0,0,.15);
        overflow: hidden;
        white-space: nowrap;
    }
    

    HTML:

    <div class="initials-container">
        <div class="initials initials-background animated fadeIn"></div>
        <div class="initials initials-text">A</div>
    </div>
    

    编辑: OP 在评论中询问如何检查图像是否已加载。我假设您不想使用 jQuery,因此您可以使用名为 imagesLoaded 的库(无论是否使用 jQuery)并检查以这种方式加载的图像:

    http://jsfiddle.net/n9wd4ver/7/

    CSS

    .initials {
      width: 100%;
      height: 100%;
    
    }
    
    .initials.initials-text {
      padding:20px 0px;
      position: relative;
      z-index: 2;
    }
    
    #initials-background {
      position: absolute;
      background-image: url("http://static.tumblr.com/lxn1yld/Hnxnxaqya/space.gif");
      -webkit-animation-delay: 3s;
      -moz-animation-delay: 3s;
      -o-animation-delay: 3s;
      animation-delay: 3s;
      z-index: 1;
      opacity: 0;
    }
    
    #initials-background.animated {
      opacity: 1;
    }
    
    
    
    .initials-container {
        margin-top:20px;
        margin-left:20px;
        height: 100%;
        position:relative;
        background:black;
        color:white;
        width:60px;
        text-align:center;
        font-size:17px;
        letter-spacing:5px;
        box-shadow:0px 2px 3px rgba(0,0,0,.15);
        overflow: hidden;
        white-space: nowrap;
    }
    

    HTML

    <div class="initials-container">
    <div id="initials-background" class="initials fadeIn">
    
    </div>
    <div class="initials initials-text">
    A
    </div>
    </div>
    

    Javascript

    imagesLoaded( '#initials-background', { background: true }, function() {
        console.log("image loaded");
      var d=document.getElementById("initials-background");
      d.className=d.className +" animated";
    });
    

    【讨论】:

    • 如何让图像在显示之前完全加载?
    • 我在答案末尾添加了一种在开始动画之前检查图像是否已加载的方法
    【解决方案6】:

    jQuery

     $('.initials').css('background-image','url(http://static.tumblr.com/lxn1yld/Hnxnxaqya/space.gif)');
     $('.initials').fadeIn(3000);
    

    【讨论】:

    • 这会将一切都淡入 - 不是 OP 想要的
    【解决方案7】:

    你可以试试这样的:

    Updated Fiddle

    注意:您可以通过替换 url 和高清图片 url 来测试它。

    function loadImage() {
      var url = 'http://www.webgranth.com/wp-content/uploads/2013/04/Ceric6.gif';
      var img = $("<img />").attr('src', url)
        .on('load', function() {
          $(".test").append(img).fadeIn(400);
        });
    }
    
    (function() {
      setTimeout(function() {
        $(".bgImage").fadeIn(400);
      }, 3000);
    
      loadImage()
    })()
    .content {
      z-index: 1;
      position: relative;
      margin-top: 20px;
      text-align: center
    }
    .initials {
      position: fixed;
      background: black;
      color: white;
      width: 60px;
      height: 60px;
      margin-top: 20px;
      text-align: center;
      margin-left: 20px;
      font-size: 17px;
      letter-spacing: 5px;
      box-shadow: 0px 2px 3px rgba(0, 0, 0, .15);
      overflow: hidden;
      white-space: nowrap;
    }
    .bgImage {
      background-image: url("http://static.tumblr.com/lxn1yld/Hnxnxaqya/space.gif");
      width: 100%;
      height: 60px;
      position: absolute;
      display: none;
      z-index: 0;
    }
    
    .test {
      display: none;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <div class="initials">
      <div class="bgImage"></div>
      <div class="content">A</div>
    </div>
    
    <div class="test"></div>

    Reference - Asychronously load images with jQuery.

    【讨论】:

    • 如何让图像在显示之前完全加载?
    • 理想的方法是使用Promise。在加载时调用一个加载图像的函数,一旦完成,这个函数将返回承诺。基于这个promise,你可以fadeIn div。不幸的是,即使我必须学习承诺部分,但会使用回调更新我的答案
    • @Zack 我已经更新了我的答案。我还找到了一个加载异步图像的帖子并给出了它的链接。希望这会有所帮助!
    【解决方案8】:

    .initials {
        position:absolute;
        background:black;
        background-image: url("http://static.tumblr.com/lxn1yld/Hnxnxaqya/space.gif");
        color:white;
        margin-top:20px;
        padding-top:20px;
        padding-bottom:20px;
        width:60px;
        text-align:center;
        margin-left:20px;
        font-size:17px;
        letter-spacing:5px;
        box-shadow:0px 2px 3px rgba(0,0,0,.15);
        overflow: hidden;
        white-space: nowrap;
    }
    &lt;div class="initials"&gt;A&lt;/div&gt;

    【讨论】:

    • 如果您围绕您的答案提供一些背景信息会有所帮助。为什么你的答案与上面的答案不同?它对解决问题有什么作用?它甚至解决了问题吗?
    猜你喜欢
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多