Getting image width on image load fails on IE

后端 未结 8 1445
自闭症患者
自闭症患者 2020-12-31 09:32

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

8条回答
  •  感动是毒
    2020-12-31 10:30

    Man, I was looking for this for 2 days. Thanks.

    I'm using jQuery, but it doesnt matter. Problem is related to javascript in IE.

    My previous code:

    var parentItem = $('
    ') .hide(); // sets display to 'none' var childImage = $('') .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' });

    This is working in FF, Opera and Safari but no IE. I was getting '0' in IE.

    Workaround for me:

    var parentItem = $('
    ') .hide(); var childImage = $('') .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 });

提交回复
热议问题