Вот моя скрипка.
Я просто хочу, чтобы "background-image:
" в CSS полностью загружался и отображался через 3 секунды с эффектом быстрого затухания, пока весь div не должен быть черного цвета.
Как это возможно в css
или javascript
.
.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>
8 ответов
С некоторыми незначительными изменениями я мог бы достичь того, чего вы хотите, только с помощью 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 секунды и занимает 3 секунды. Вот скрипка: http://jsfiddle.net/w11r4o3u/1/
Чтобы отрегулировать «скорость» появления fadeIn, отредактируйте
-webkit-animation-duration: .3s;
Если вы хотите настроить задержку анимации для запуска, отредактируйте
-webkit-animation-delay: 3s;
Вы можете попробовать что-то вроде этого:
Примечание. Вы можете проверить его, заменив URL-адрес и URL-адрес изображения HD.
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.
Другой вариант - иметь отдельный класс и назначить этот класс этому классу. Проверьте эту скрипку
Сначала вы настраиваете метод в 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");
}
JQuery
$('.initials').css('background-image','url(http://static.tumblr.com/lxn1yld/Hnxnxaqya/space.gif)');
$('.initials').fadeIn(3000);
.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>
Как это, возможно, ... затухание сложно, хотя - AFAIK, вы не можете затушевывать свойство background-image
setTimeout(function(){
$('.initials').css('background-image', 'url("http://static.tumblr.com/lxn1yld/Hnxnxaqya/space.gif")');
}, 3000)
Другой вариант - разделить HTML на 3 секции, буквы с самым высоким z-индексом, затем черный слой над слоем звезд ... затем исчезнуть черный слой
Вы можете создать 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>
РЕДАКТИРОВАТЬ: ОП спросил в комментарии, как проверить, было ли загружено изображение. Я предполагаю, что вы не хотите использовать 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";
});
Вот решение, фоновое изображение появится через несколько секунд.
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);
});
Похожие вопросы
Связанные вопросы
Новые вопросы
javascript
По вопросам программирования на ECMAScript (JavaScript / JS) и его различных диалектах / реализациях (кроме ActionScript). Включите все соответствующие теги в свой вопрос; например, [node.js], [jquery], [json] и т. д.