简体   繁体   中英

When All images loaded, execute a code

add.php

<script type="text/javascript">
$("img").load(function() {
     alert("all images loaded");
});
</script>
<?php 
function resim1(){
?>
<img height="150" weight="150" src="http://uyarer.com/linux/deneme.png"/>
<?php
}
?>

<?php 
function resim2(){
?>
<img height="150" weight="150" src="http://hunturk.net/resim-cnNtL2dhbGVyaS9iYXlyYWsvZTc4M2NlYzRlYi5qcGc=-1024-768.png"/>
<?php
}
?>
<?php 
 resim1();
 resim2();
?>

when executed this code.,the jquery alert for every loaded images. I imported this php page another php with ajax.

For example

1.jpg alert > loaded 2.jpg alert > loaded

but i want

1.jpg 2.jpg alert > all images loaded

i guess i change this code

$(**"img"**).load(function() {
 alert("all images loaded");

});

Thanks

Load event won't always fire when images are cached. There is a jquery plugin to address this issue.

var imgNum=$('img').length;
$('img').load(function(){
    if(!--imgNum){alert('All images loaded')}
})

I think this code can help you

var imgSize = 0;
var loaded = 0;
var _tim = 0;
$(document).ready(function()
{
 imgSize = $('img').size();
 $('img').load(function() {
   loaded++;
   clearTimeout(_tim);
  _tim =   setTimeout(function() { if(imgSize == loaded) { alert('all images are loaded'); }, 200);
});
});
var imagesPre = new Array;
var success = new Array;

$('img').each(function(){
  imagesPre.push(this.src);
});

for (i = 0; i < imagesPre.length; i++) {
    (function(path, success) {
        image = new Image();
        image.onload = function() {
          success.resolve();
        };
        image.src = path;
    })(imagesPre[i], success[i] = $.Deferred());
}

$.when.apply($, success).done(function() {
  alert("All image!");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM