6

I am trying to fade in a div which has an inline background image in it.

I've tried it on images on page load, but how can we achieve that effect with background images in a div.

Calum
  • 1,889
  • 2
  • 18
  • 36
designerNProgrammer
  • 2,621
  • 5
  • 34
  • 46

4 Answers4

3

You Can Use a jQuery plugin called waitForImages that can detect when background images have downloaded.

$('selector').waitForImages({
    finished:function(){$(this).slideUp();},
    waitForAll:true
});
joelmdev
  • 11,083
  • 10
  • 65
  • 89
Mahmoude Elghandour
  • 2,921
  • 1
  • 22
  • 27
2

The second answer from the Question linked in the comments, Link here, provides a solution where you load a background-image to an image tag, then when it's ready you inject the image into a div. Here is an example similar, yet different:

html:

<img src="http://www.modernmythmedia.com/wp-content/uploads/2013/04/Iron-Man-wallpaper-2-2032-e1367196003357.jpg" id="dummy" style="display:none;" alt="" />
<div id="pic" style="height:100px;width:100px;display:none;"></div>

jQuery:

$('#dummy').ready(function() {
    $('#pic').css('background-image','url(http://www.modernmythmedia.com/wp-content/uploads/2013/04/Iron-Man-wallpaper-2-2032-e1367196003357.jpg)');
    $('#pic').fadeIn(1000);
});

With live preview here: Fiddle.

Hopefully this works better for you!

Community
  • 1
  • 1
Blundering Philosopher
  • 6,245
  • 2
  • 43
  • 59
  • hey buddy i need it to fade in after image has been loaded.thanks. – designerNProgrammer Feb 23 '14 at 07:27
  • @designerNProgrammer Good call. I updated my answer to be closer to what you're looking for. – Blundering Philosopher Feb 23 '14 at 07:51
  • I suggest also placing the background url to the div. You probaly can do that when you put together the template, whilst the JS method takes some resource: http://jsfiddle.net/unUc8/8/. I also took the liberty of removing the image after the load, minimizing the DOMsize (allthough one might argue about the CPU it takes to remove them) – Martijn Mar 25 '14 at 17:52
  • Updated the answer from `div#id` to `#id`. When using id's, it can use `getElementById()`, when you add the div, it also has to test if it is a div, and it can no longer use the fast functions :) – Martijn Mar 25 '14 at 17:55
0

HTML5 Animation will load a canvas context that allows for this and more.

Here is the CSS demo of the animation.

Kraken
  • 5,043
  • 3
  • 25
  • 46
-1

Can you try this?

HTML

<div id="mg" style="background-image:url(http://lorempixel.com/1920/1920/); display:none; width:1920px; height:1920px;"></div>

JS

var elem   = $('#mg'),
    bgImg  = elem.css('background-image'),
    imgUrl = bgImg.indexOf('"')!=-1 ? 
             bgImg.replace('url("','').replace('")','') : 
             bgImg.replace('url(','').replace(')',''),

     nwImg = new Image();
     nwImg.src = imgUrl;

nwImg.onload = function() {
    elem.fadeIn('fast');
}

nwImg.onerror = function() {
    alert('error');
}

JsFiddle

http://jsfiddle.net/EZg36/8/

If you change the URL incorrectly, you will see that the error message (for example: background-image:url(hERRRttp://lorempixel.com/1920/1920/);)

midstack
  • 2,105
  • 7
  • 44
  • 73