在IE上获取图像加载时的图像宽度失败 [英] Getting image width on image load fails on IE
问题描述
我有一个图像缩放功能,可以按比例调整图像大小。在每个图像上加载一个调用此函数的图像,如果其宽度或高度大于我的最大宽度和最大高度,则调整大小。我可以在FF Chrome Opera Safari中获得img.width和img.height,但是IE失败了。我该如何处理?
I have a image resizer function that resize images proportional. On every image load a call this function with image and resize if its width or height is bigger than my max width and max height. I can get img.width and img.height in FF Chrome Opera Safari but IE fails. How can i handle this?
让我用一段代码解释一下。
Let me explain with a piece of code.
<img src="images/img01.png" onload="window.onImageLoad(this, 120, 120)" />
function onImageLoad(img, maxWidth, maxHeight) {
var width = img.width; // Problem is in here
var height = img.height // Problem is in here
}
在myligted行中,img.width不适用于IE系列。
In my highligted lines img.width don't work on IE series.
有任何建议吗?
谢谢。
推荐答案
不要使用 width
和 height
。请改用 naturalWidth
和 naturalHeight
。它们提供了图像文件中未缩放图像尺寸的图像,可以在浏览器中使用。
Don't use width
and height
. Use naturalWidth
and naturalHeight
instead. These provide the image unscaled pixel dimensions from the image file and will work across browsers.
这篇关于在IE上获取图像加载时的图像宽度失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!