javascript - Jquery preload image dynamically -
wondering why not working...
function:
$.fn.preload = function() { this.each(function(){ $('<img/>')[0].src = this; }); }
code:
<?php $preimages = glob('images/backgrounds/*{.jpg, .png, .gif, jpeg}', glob_brace); ?> $([ <?php $imgcount = sizeof($preimages); for($i = 0; $i < $imgcount; $i++) { if($i == $imgcount-1) echo $preimages[$i]; else echo $preimages[$i] . ', '; } ?> ]).preload($("#load").fadeout());
i'm trying preload images in foler images/backgrounds folder... want callback $("#load").fadeout()...
however works when following:
img1 = new image(); img2 = new image(); img3 = new image(); img1loaded = false; img2loaded = false; img3loaded = false; img1.onload = function() { img1loaded = true; if(img1loaded && img2loaded && img3loaded) { $("#load").fadeout(); } }; img2.onload = function() { img2loaded = true; if(img1loaded && img2loaded && img3loaded) { $("#load").fadeout(); } }; img3.onload = function() { img3loaded = true; if(img1loaded && img2loaded && img3loaded) { $("#load").fadeout(); } }; img1.src = '<?php echo $baseurl; ?>images/backgrounds/img1.jpg'; img2.src = '<?php echo $baseurl; ?>images/backgrounds/img2.jpg'; img3.src = '<?php echo $baseurl; ?>images/backgrounds/img3.jpg';
how can above, dynamically?
thanks!
here's image preloader i've used before...
function onimagesloaded($container, callback) { var $images = $container.find("img"); var imgcount = $images.length; if (!imgcount) { callback(); } else { $("img", $container).each(function () { $(this).one("load error", function () { imgcount--; if (imgcount == 0) { callback(); } }); if (this.complete) $(this).load(); }); } }
call passing parent element (as jquery object, not dom element), , callback function, when images loaded...
onimagesloaded($("div.image-container"), imagesloaded);
that call imagesloaded()
when images in div.image-container
loaded.
it expanded upon handle errors, purposes treat error image complete, otherwise wouldn't run callback function if of images errored reason.
Comments
Post a Comment