return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - JS wait for CSS background image to load - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS wait for CSS background image to load - Stack Overflow

programmeradmin2浏览0评论

It's easy to keep javascript waiting for some images to load if those are classic HTML images.

But I can't figure how to do the same if the image is loaded as a CSS backuground-image!

Is it possible?

The jQuery .load() method doesn't seem to apply.. and I'm short of ideas

It's easy to keep javascript waiting for some images to load if those are classic HTML images.

But I can't figure how to do the same if the image is loaded as a CSS backuground-image!

Is it possible?

The jQuery .load() method doesn't seem to apply.. and I'm short of ideas

Share Improve this question edited Jan 28, 2012 at 3:49 Jason Gennaro 34.8k9 gold badges66 silver badges86 bronze badges asked Jan 28, 2012 at 3:44 ValentinoValentino 5427 silver badges23 bronze badges 7
  • The first thing that es to mind is to precache the image in an actual img element which you can hide. Something like: technosophos./content/…. There are probably better solutions though. – mowwwalker Commented Jan 28, 2012 at 3:47
  • 2 You may want to check out this answer: stackoverflow./questions/3489270/… – Jason Gennaro Commented Jan 28, 2012 at 3:48
  • I haven't tried this, but perhaps you could do an image pre-load from JS/jQuery (like for old-school image rollovers), check the loaded status of the image, and then after it has loaded assign the CSS background property to the same image before going on with whatever other JS you want to do when the image is ready? – nnnnnn Commented Jan 28, 2012 at 3:48
  • so the background loading doesn't trigger any event.. too bad. Then I guess I'll manage to get it working with a preloader. thanks for the hint guys – Valentino Commented Jan 28, 2012 at 3:55
  • you can use AJAX to do this, after success your request finish your waiting. – Ali Youhanaei Commented Jan 28, 2012 at 6:28
 |  Show 2 more ments

4 Answers 4

Reset to default 1

It looks for elements with src attribute or backgroundImage css property and calls an action function when theirs images loaded.

/**
 * Load and wait for loading images.
 */
function loadImages(images, action){
    var loaded_images = 0;
    var bad_tags = 0;        

    $(images).each(function() {
    //alert($(this).get(0).tagName+" "+$(this).attr("id")+" "+$(this).css("display"));
        var image = new Image();
        var src = $(this).attr("src");
        var backgroundImage = $(this).css("backgroundImage");            
        // Search for css background style
        if(src == undefined && backgroundImage != "none"){
            var pattern = /url\("{0,1}([^"]*)"{0,1}\)/;                
            src = pattern.exec(backgroundImage)[1];                
        }else{
            bad_tags++;
        }
        // Load images
        $(image).load(function() {
            loaded_images++;                
            if(loaded_images == ($(images).length - bad_tags))
                action();
        })
        .attr("src", src);
    });
}

One alternate approach would be to fetch the image data via AJAX as a base64 encoded png and apply it to the element's background-image property.

For example:

$.get('/getmyimage', function(data) {
    // data contains base64 encoded image
    // for ex: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==

    $('#yourElement').css('background-image', 'url("' + data + '")');
});

You will also need a server side script that reads the image, converts it into base64 encoded png (and cache it maybe) and return the same.

this is untested code but try this:

$(document).ready(
 function() 
 {
   var imgSrc = $('theTargerElement').css('background-image');
   var imgTag = $('<img>').attr('src',imgSrc).appendTo( 'body' );
 }
);

$(document)
 .load( 
   function() 
   {
     // do stuff
   } 
  );

Try this one...

Its a jQuery-Plugin which gives you control to wait for images to be loaded

Project-Home

Thread @ SO Official way to ask jQuery wait for all images to load before executing something

Answer @ SO (ShortLink)

发布评论

评论列表(0)

  1. 暂无评论