Zack
Zack

Reputation: 501

How to delay the display of the background image in a div

Here's my fiddle.


I just want the " background-image: " in the css to load fully and display after 3 seconds with a quick fade in effect, until then the entire div must be in black color.
How it is possible in css or 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>

Upvotes: 21

Views: 13153

Answers (8)

Zamani Zam
Zamani Zam

Reputation: 11

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

Upvotes: 1

Rajesh
Rajesh

Reputation: 24915

You can try something like this:

Updated Fiddle

Note: You can test it by replacing url to and HD image 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.

Upvotes: 2

valepu
valepu

Reputation: 3315

You can create an absolute positioned div with the background image property and then use animate.css to add an animation without having to create the keyframes yourself

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>

EDIT: OP asked in a comment how to check if the image has been loaded. I'm assuming you don't want to use jQuery, so you can use a library called imagesLoaded (which works both with and without jQuery) and check for image being loaded this way:

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";
});

Upvotes: 3

Inacio Schweller
Inacio Schweller

Reputation: 1986

With some minor changes, I might have achieved what you want with only CSS3. Check the fiddle: 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>

Edited:

Now the animation starts after 3 seconds and takes .3s to complete. Here is the fiddle: http://jsfiddle.net/w11r4o3u/1/

  • To adjust the "velocity" that fadeIn occurs, edit -webkit-animation-duration: .3s;

  • If you want to adjust the animation "delay" to start, edit -webkit-animation-delay: 3s;

Upvotes: 15

Lucas Rodriguez
Lucas Rodriguez

Reputation: 1203

Another option would be to have a separate class and assign that css to that class. Chech this fiddle

First you setup a method in document.ready:

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

Then in your css you change initials to:

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

And add:

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

Upvotes: 3

Arun Kumar M
Arun Kumar M

Reputation: 1601

Here is the solution, background image will appear after few seconds.

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;
}

Script

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

Upvotes: 3

StudioTime
StudioTime

Reputation: 23969

Like this perhaps...the fade in is tricky though - AFAIK you can't fade the background-image property

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

Another option is to split the HTML into 3 section, letters with highest z-index, then black layer over the stars layer...then fade out the black layer

Upvotes: 5

arun
arun

Reputation: 107

JQuery

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

Upvotes: 2

Related Questions