$(img).load() 和/或 img.onload=... 中的 img.width 和 img.height 在 IE8 中返回垃圾值
我知道这个话题被讨论了很多,但我找不到任何适合我的解决方案。所以,我的代码大致是:
var img =me.images[curID]
var f = function() {
var i = $(img);
img.onload = null;
$(img).unbind('load');
var iH = img.height; /*works for Firefox, Safari, Opera, Chrome*/
var iW = img.width;
if (jQuery.browser.msie) {
/*nothing works*/
iH = i.width(); /* returns 0*/
iH = i.attr('height'); /*returns undefined*/
iH = img.height; /*returns wrong 120:30 px instead of 1000:410 */
/*once page is reloaded - everything is fine */
}
}
src = img.src;
if ( (!img.complete) ||
(typeof img.naturalWidth != 'undefined' && img.naturalWidth == 0)
) { /*WAIT FOR LOAD*/
img.src = "";
$(img).load(f);
img.onload = f;
img.src = src;
} else { /*SHOW */
f.apply(img);
}
我做错了什么?
p/s/ 图像位于褪色的 div 内,所以我基本上想在淡入之前调整图像大小。
编辑1:关于我的数组的一些信息:
....
this.slides = this.slidesContainer.children();
this.n = this.slides.length;
for (var i=0;i<this.n;i++) {
var im = this.slides.eq(i).find('img:first')[0];
var sr = im.src;
this.images[i] = im;
this.imagesSrc[i] = sr;
}
...
编辑2:我重写了一点代码(添加了加载),所以现在它到处都会触发(IE / FF / Opera / Safari / ...) 程序流程(IE8)如下:
- 等待加载
- onload 被触发(如果我评论它 - jQuery().load 被触发)。
- 在f()内部:img.width = 120,img.height = 30(似乎没有加载。正确的宽度/高度是1000x410)
很难说是什么触发了它,因为它并不总是发生,甚至在图像时也不会总是发生没有兑现。但时不时地,我会看到那些奇怪的宽度/高度。
EDIT3:有一些关于如何加载图像的解决方案(这里 和 此处),在一个 .onload 中使用,在另一个 - .load 中,我同时使用两者,因此即使代码可能不是最佳且非常整洁,也应该没问题。
I know this topic is discussed a lot, but I could not find any solutions which would work for me. So, my code is roughly:
var img =me.images[curID]
var f = function() {
var i = $(img);
img.onload = null;
$(img).unbind('load');
var iH = img.height; /*works for Firefox, Safari, Opera, Chrome*/
var iW = img.width;
if (jQuery.browser.msie) {
/*nothing works*/
iH = i.width(); /* returns 0*/
iH = i.attr('height'); /*returns undefined*/
iH = img.height; /*returns wrong 120:30 px instead of 1000:410 */
/*once page is reloaded - everything is fine */
}
}
src = img.src;
if ( (!img.complete) ||
(typeof img.naturalWidth != 'undefined' && img.naturalWidth == 0)
) { /*WAIT FOR LOAD*/
img.src = "";
$(img).load(f);
img.onload = f;
img.src = src;
} else { /*SHOW */
f.apply(img);
}
What am I doing wrong?
p/s/ the image is inside div which is faded, so I basically want to resize an image before fadeIn.
EDIT1: some info on my arrays:
....
this.slides = this.slidesContainer.children();
this.n = this.slides.length;
for (var i=0;i<this.n;i++) {
var im = this.slides.eq(i).find('img:first')[0];
var sr = im.src;
this.images[i] = im;
this.imagesSrc[i] = sr;
}
...
EDIT2: I rewrote a code a little bit (added onload), so that now it fires everywhere (IE/FF/Opera/Safari/...)
And the program flow (IE8) is the following:
- wait for load
- onload fired (if I comment it -- jQuery().load is fired).
- inside f(): img.width = 120, img.height = 30 (it seems it's not loaded. correct width/height are 1000x410)
It's hard to say what triggers it, because it does not happen always, not even always when images are not cashed. But from time to time, I see those strange width/height.
EDIT3: there are some solution on how to get image loaded (here and here), in one .onload is used, in another - .load, I use both, so that should be fine even though the code might not be optimal and very neat.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 jQuery 尝试使用 jQuery 的方式获取图像。
而不是
var img =me.images[curID]
只需执行以下操作:If you are using jQuery Try using jQuery's way of getting the image.
Instead of
var img =me.images[curID]
Just do :