开发者

swapping images of different sizes [Javascript vs IE]

开发者 https://www.devze.com 2023-01-22 03:55 出处:网络
I have the following simple preloading function which substitute an image \"src\" attribute with another image (an animated GIF \"Loading\"). The problem arises only in IE: if the \"loading\" GIF is s

I have the following simple preloading function which substitute an image "src" attribute with another image (an animated GIF "Loading"). The problem arises only in IE: if the "loading" GIF is smaller than the actual image src, that will be resized. For example if I have a square 100px image and preload it, the image is temporarly substituted by an animated GIF of 50x50px. Whem the original image is fully loaded it is NOT displayed at its size, but at the smaller 50px. Here is the code, if you need it

_preload = function(url, placeholderUrl) {
 var img = new Image();
 loading = true;
 var placeholder = new Element("img", {
  src: placeholderUrl
 });
 img.placeholder = placeholder;
 img.onload = function(evt) {
  this.placeholder.src = this.src;
  loading = false;
 }
 img.src = url;
 return placeholder;
}

swapping images of different sizes [Javascript vs IE]

Here you can see the visu开发者_开发百科al error


You should be able to adjust the width/height of the image within the callback function:

img.onload = function(evt) {
  this.placeholder.src = this.src;
  this.placeholder.width = this.width;
  this.placeholder.height = this.height;
  loading = false;
}

Example: Resizing image onLoad


I guess replacing placeholder with img (the img-elements inside the dom), instead of simply changing the src-attribute of placeholder, should fix this issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消