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