0

Hullo brothers!

I wanted to run a function when image loading completes. My code:-

HTML:-

<div id="s_pc" style="background-image:url(http://www.surfixe.com/img/surfixe_pc.png)"></div>
<button id="btn" style="display:none">This button should only show when image loading completes!</button>

jQuery:-

$('#s_pc').attr('style', 'background-image:url(www.surfixe.com/img/surfixe_pc.png)').load(function() {  
    alert('Image Loaded');
    $('#btn').show();
});

My current fiddle

Seems like there is an error in my coding. Any ideas?

Surfine
  • 392
  • 2
  • 5
  • 15
  • 1
    Some searching: http://stackoverflow.com/questions/2392410/jquery-loading-images-with-complete-callback – Daniel Apr 27 '13 at 03:08

2 Answers2

0

try this. to load the image using Javascript, and then set that image as the backgroud.

var bgImg = new Image();
bgImg.onload = function(){
   $('#s_pc').css('background-image', 'url(www.surfixe.com/img/surfixe_pc.png)');
    alert('Image Loaded');
    $('#btn').show();
};
bgImg.src = 'www.surfixe.com/img/surfixe_pc.png';
SivaGao
  • 1
  • 1
  • 1
-1

Some one here (I don't know the name) has answered my question but deleted it. So, I thought I should post it:-

$('<img/>').attr('src', 'http://www.surfixe.com/img/surfixe_pc.png').load(function() {
    alert('Image Loaded');
    $('#btn').show();
});

edit: jsfiddle

Surfine
  • 392
  • 2
  • 5
  • 15