简体   繁体   English

当加载所有图像时,执行代码

[英]When All images loaded, execute a code

add.php 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.执行此代码时,每个加载的图像都会发出 jquery 警报。 I imported this php page another php with ajax.我用 ajax 导入了这个 php 页面另一个 php。

For example例如

1.jpg alert > loaded 2.jpg alert > loaded 1.jpg 警报 > 已加载 2.jpg 警报 > 已加载

but i want但我想要

1.jpg 2.jpg alert > all images loaded 1.jpg 2.jpg 警报 > 所有图片已加载

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.有一个jquery 插件可以解决这个问题。

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!");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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