简体   繁体   English

如何在我的页面中加载所有图像后运行jQuery代码?

[英]How to run a jQuery Code after loading all the images in my page?

如何在我的页面中加载所有图像后运行jQuery代码?

$(window).load(function(){})

Checking to see that all images have loaded is slightly involved, so unless you have a pressing need to be so precise, it is much easier to check that all image and everything else has loaded: 检查是否已经加载了所有图像 ,因此除非您需要非常精确,否则检查所有图像和其他所有图像都已加载更容易:

$(window).load(function() { ... });

This makes use of the jQuery .load() method. 这使用了jQuery .load()方法。

If you do really need to check for specifically only images, then things get a little trickier. 如果你真的需要特别检查图像,那么事情会变得有点棘手。 I initially wanted to do this: 我最初想要这样做:

$("img").load(function() { ... }); \\ THIS IS INCORRECT!!!!!

However the above creates a jQuery object that contains all images, and then it binds function() { ... } to each of these images. 但是,上面创建了一个包含所有图像的jQuery对象,然后它将function() { ... }绑定到这些图像中的每一个 So the above will trigger a function as many times as there are images on the page! 所以上面会触发一个函数,就像页面上有图像一样!

To get around this, we should check how many images there are on the page, and only fire the function once after all those images have been loaded: 为了解决这个问题,我们应该检查页面上有多少图像,并且只在加载了所有图像后才启动一次:

$(function() {  

      // To keep track of how many images have loaded
    var loaded = 0;

      // Let's retrieve how many images there are
    var numImages = $("img").length;

      // Let's bind a function to the loading of EACH image
    $("img").load(function() {

          // One more image has loaded
        ++loaded;

          // Only if ALL the images have loaded
        if (loaded === numImages) {

              // This will be executed ONCE after all images are loaded.
            function() { ... }   
        }
    });
});

jsFiddle example jsFiddle例子

$(function() {
 var length = $('img').length ;
 var counter = 0;
 $('img').each(function() {
     $(this).load(function(){
        counter++;
        if(counter == length) {
           Callback(); //do stuff
        }
     });
   });
});

I did something like this recently, but went about it differently. 我最近做了类似这样的事情,但却采用了不同的方式。

$('img').on('load', function(){
    $('img').off('load'); //detaches from load event, so code below only executes once
    // my code to execute
});

Not a direct answer. 不是直接的答案。 Still worth referring. 还是值得一提。

Refer 参考

  1. Run JavaScript Only After Entire Page Has Loaded 整页加载后仅运行JavaScript
  2. jQuery callback on image load (even when the image is cached) jQuery回调图像加载(即使图像被缓存)

Code

$(window).bind("load", function() {
   // code here
});

@Peter Ajtai answer will work except on IE's cached images. @Peter Ajtai的答案除IE浏览器的缓存图片外都有效。 To make it work with IE, here's a solution: https://stackoverflow.com/a/3877079 or by using the imagesLoaded plugin. 为了使它适用于IE,这里有一个解决方案: https//stackoverflow.com/a/3877079或使用imagesLoaded插件。

For anyone using jquery this little snippet does the trick (it ensures that all images are loaded before any script inside it are run)... 对于任何使用jquery的人来说,这个小片段可以解决问题(它可以确保所有图像在其中的任何脚本运行之前加载)...

$(window).bind("load", function() {
   // code goes here here
});

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

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