12

我有一个图像调整器功能,可以按比例调整图像大小。在每个图像加载时,如果它的宽度或高度大于我的最大宽度和最大高度,则使用图像调用此函数并调整大小。我可以在 FF Chrome Opera Safari 中获取 img.width 和 img.height 但 IE 失败。我该如何处理?

让我用一段代码解释一下。

<img src="images/img01.png" onload="window.onImageLoad(this, 120, 120)" />

函数 onImageLoad(img, maxWidth, maxHeight) {
     var 宽度 = img.width; // 问题在这里
     var height = img.height // 问题在这里
}

在我的高亮行中,img.width 不适用于 IE 系列。

有什么建议吗?

谢谢。

4

8 回答 8

22

不要使用widthheight。使用naturalWidthandnaturalHeight代替。这些提供来自图像文件的图像未缩放的像素尺寸,并且可以跨浏览器工作。

于 2013-07-25T13:57:21.673 回答
4

伙计,我一直在寻找这个 2 天。谢谢。

我正在使用 jQuery,但这没关系。问题与 IE 中的 javascript 有关。

我之前的代码:

var parentItem = $('<div/>')
   .hide();      // sets display to 'none'

var childImage = $('<img/>')
   .attr("src", src)
   .appendTo(parentItem)   // sets parent to image
   .load(function(){
      alert(this.width);  // at this point image is not displayed, because parents display parameter is set to 'none' - IE gives you value '0'
   });

这适用于 FF、Opera 和 Safari,但没有 IE。我在 IE 中得到'0'。

我的解决方法:

var parentItem = $('<div/>')
   .hide();

var childImage = $('<img/>')
   .attr("src", src)
   .load(function(){
      alert(this.width);  // at this point image css display is NOT 'none'  - IE gives you correct value
      childImage.appendTo(parentItem);   // sets parent to image
   });
于 2011-07-18T14:32:56.403 回答
3

这就是我解决它的方法(因为它是我不想使用库的网站上唯一的 js)。

    var imageElement = document.createElement('img');
    imageElement.src = el.href; // taken from a link cuz I want it to work even with no script
    imageElement.style.display      = 'none';

    var imageLoader = new Image();
    imageLoader.src = el.href;
    imageLoader.onload = function() {
        loaderElement.parentElement.removeChild(loaderElement);
        imageElement.style.position     = 'absolute';
        imageElement.style.top          = '50%';
        imageElement.style.left         = '50%';
        // here using the imageLoaders size instead of the imageElement..
        imageElement.style.marginTop    = '-' + (parseInt(imageLoader.height) / 2) + 'px';
        imageElement.style.marginLeft   = '-' + (parseInt(imageLoader.width) / 2) + 'px';
        imageElement.style.display      = 'block';
    }
于 2012-11-26T18:23:34.747 回答
2

这是因为 IE 无法计算display: none图像的宽度和高度。改为使用visibility: hidden

于 2012-11-27T09:38:34.747 回答
1

尝试

 function onImageLoad(img, maxWidth, maxHeight) {
   var width = img.width; // Problem is in here
   var height = img.height // Problem is in here
   if (height==0  && img.complete){
       setTimeOut(function(){onImageLoad(img, maxWidth, maxHeight);},50);
   }

 }
于 2016-04-23T21:24:42.337 回答
0
    var screenW = screen.width;
    var screenH = screen.height;
    //alert( screenW );
    function checkFotoWidth( img, maxw )
    {
        if( maxw==undefined)
            maxw = 200;
        var imgW = GetImageWidth(img.src);
        var imgH = GetImageHeight(img.src);
            //alert(GetImageWidth(img.src).toString()); // img.width); // objToString(img));
        if (imgW > maxw || (img.style.cursor == "hand" && imgW == maxw))
        {
            if (imgW > screenW) winW = screenW;
            else winW = imgW;

            if (imgH > screenH) winH = screenH;
            else winH = imgH;

            img.width=maxw;
            img.style.cursor = "pointer";

            img.WinW = winW;
            img.WinH = winH;
            //alert("winW : " + img.WinW);
            img.onclick = function() { openCenteredWindow("Dialogs/ZoomWin.aspx?img=" + this.src, this.WinW, this.WinH, '', 'resizable=1'); }
            img.alt = "Klik voor een uitvergroting :: click to enlarge :: klicken Sie um das Bild zu vergrössern";
            //alert("adding onclick);
        }
    }

    function GetImageWidth(imgSrc) 
    {
        var img = new Image();
        img.src = imgSrc;
        return img.width;
    } 

    function GetImageHeight(imgSrc) 
    {
        var img = new Image();
        img.src = imgSrc;
        return img.height;
    } 
于 2010-12-17T15:43:25.687 回答
0

我会试试这个:

function onImageLoad(img, maxWidth, maxHeight) {
   var width, height;
   if ('currentStyle' in img) {
     width = img.currentStyle.width;
     height = img.currentStyle.height;
   }
   else {
     width = img.width;
     height = img.height;
   }
   // whatever
}

编辑— 显然,如果我尝试一下,我会发现它不起作用 :-) 好吧,<img>就 IE 而言,“宽度”和“高度”似乎肯定是元素的属性。也许问题在于“加载”事件在错误的时间为元素触发。要检查是否是这种情况,我会试试这个:

function onImageLoad(img, maxWidth, maxHeight) {
   var width, height;
   var i = new Image();
   i.onload = function() {
     width = i.width; height = i.height;
     // ... stuff you want to do ...
   };
   i.src = img.href;
}
于 2010-12-17T16:04:00.643 回答
-1
getDisplay().getImage().setUrl(imgURL);
final Image img = new Image(imgURL);
int w=img.getWidth();
int h=img.getHeight();
于 2014-07-23T13:57:13.030 回答