您可以使用背景图像属性创建一个绝对定位的 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";
});