简体   繁体   English

在 jQuery 图像 object 上绑定 onload 事件

[英]Binding onload event on image object in jQuery

I have following code on my site:我的网站上有以下代码:

 backgroundImages[bg_img_path_b]=new Image();
 backgroundImages[bg_img_path_b].src =     bg_img_path_b;
 backgroundImages[bg_img_path_b].loaded="loading";
 //jQuery(backgroundImages[lastImage]).unbind('load.backgroundImages');                                         

 jQuery(backgroundImages[bg_img_path_b]).bind('load.backgroundImages',function(){
      if(typeof callback=='function'){
           callback.call(this, bg_img_path_b);
           if(showLoading) hideLoadingDC();
      }
 }).bind('load.cache', function(){
                            backgroundImages[bg_img_path_b].loaded="true";
                        });;

There is large gallery for images used as background-images of page wrapper.有用作页面包装器背景图像的大型图像库。 I need to preload images because of speed (the images are quite large).由于速度原因,我需要预加载图像(图像非常大)。 So I have this code (actually is only a part of bigger function, which wraps caching and so on, but this few lines are fired when image is not in cache).所以我有这段代码(实际上只是更大的 function 的一部分,它包装了缓存等,但是当图像不在缓存中时会触发这几行)。

backgroundImages is large array of Image objects, key is the path is the path of image. backgroundImages是Image对象的大数组,key是path是图片的路径。 Every Image object has my property "loaded" which says if image has already been loaded, or is currently in state of loading.每个图像 object 都有我的属性“加载”,它表示图像是否已经加载,或者当前正在加载 state。

As you can see from my code I am calling callback function when the image is loaded (there are changes of the background etc.)正如您从我的代码中看到的那样,我在加载图像时调用回调 function(背景发生变化等)

But I have a problem with IE<9, the callback is not successfully fired up (but not every time)..When I load my page for first it loads properly, but anytime I call this function again, it goes correctly through without errors, but the load event doesn't fired..但是我有一个 IE <9 的问题,回调没有成功启动(但不是每次)..当我第一次加载我的页面时它加载正确,但是每当我再次调用这个 function 时,它会正确地通过而没有错误, 但加载事件没有触发..

I really don't know where could be an error, in all browsers except older IEs it works fine..我真的不知道哪里可能出错,在除旧版 IE 之外的所有浏览器中它工作正常..

Actually I need to debug if the event is bound correctly, but I can't see it in both IE and Chrome under load item in debugger:(实际上我需要调试事件是否正确绑定,但我无法在调试器的加载项下在 IE 和 Chrome 中看到它:(

Please help I am completely screwed, really don't know what to do.请帮助我完全搞砸了,真的不知道该怎么办。

So few days ago I finally found out a solution to my problem..It seems its matter if I at first bind the onload event and then set the url of Image, or at first set the url and then bind the event.. Example所以几天前我终于找到了解决我的问题的方法..如果我首先绑定onload事件然后设置Image的url,或者首先设置url然后绑定事件似乎很重要..示例

var photo = new Image();
photo.url = "http://www.something.com/something.jpg";
$(photo).bind('load',doSomething());


var photo = new Image();
$(photo).bind('load',doSomething());
photo.url = "http://www.something.com/something.jpg";

First variant runs usually ok, but sometimes it fails (in older IEs)..But the second variant is sure working propely..第一个变体通常运行正常,但有时会失败(在较旧的 IE 中)..但是第二个变体肯定可以正常工作..

which jQuery API version are you using?您使用的是哪个 jQuery API 版本? Try using the latest version of jQuery.尝试使用最新版本的 jQuery。

Otherwise use this simple JavaScript code to load images on calling your function on body onload:否则,使用这个简单的 JavaScript 代码在主体加载时调用 function 来加载图像:

var myimages = new Array();
var path = "images/gallery/";

function preloadimages() {
    for (i = 0; i < preloadimages.arguments.length; i++) {
        myimages[i]=new Image();
        myimages[i].src=path+preloadimages.arguments[i];
    }
}

// Enter name of images to be preloaded inside parenthesis with douable quotes. 
// Extend list as desired with comma.
preloadimages("gImg1.jpg","gImg2.jpg","gImg3.jpg","gImg4.jpg" /*, etc...*/);

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

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