21

这是我的小提琴。


我只希望background-image:css中的“”完全加载并在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>

4

8 回答 8

15

通过一些小的改动,我可能只用 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;

于 2015-12-18T08:09:36.237 回答
5

可能像这样......虽然淡入很棘手 - AFAIK你不能淡入背景图像属性

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

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

于 2015-12-18T07:57:42.790 回答
3

另一种选择是拥有一个单独的类并将该 css 分配给该类。Chech 这个小提琴

首先你在 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");
  }
于 2015-12-18T08:00:08.657 回答
3

您可以使用背景图像属性创建一个绝对定位的 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";
});
于 2015-12-18T08:00:11.527 回答
3

这是解决方案,几秒钟后会出现背景图像。

演示

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);
});
于 2015-12-18T08:21:28.587 回答
2

jQuery

 $('.initials').css('background-image','url(http://static.tumblr.com/lxn1yld/Hnxnxaqya/space.gif)');
 $('.initials').fadeIn(3000);
于 2015-12-18T07:55:29.117 回答
2

你可以尝试这样的事情:

更新小提琴

注意:您可以通过将 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>

参考 - 使用 jQuery 异步加载图像

于 2015-12-18T08:29:41.973 回答
1

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

于 2015-12-18T10:54:55.600 回答