最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getting the Width of an element without padding in Internet Explorer - Stack Overflow

programmeradmin1浏览0评论

I want to fit images on my site to the size of their containing element, so I have this:

if (userHasMicrositePhoto) {
    var width = $('micrositePhotoDiv').getComputedSize().width;
    $('micrositePhoto').src = "flash/userImage.ashx?type=micrositePhoto&id=" + userId + "&width=" + width;
}

My handler file userImage.ashx returns the image given by the ID, scaled to the width given as a parameter.

This works fine in firefox, chrome & co, but doesn't work in Internet explorer - the image returned is too large. I think this is because .getComputedSize().width reports a width that includes the size of the padding (but on the border or margin) in Internet explorer, but returns only the usable area in other browsers. As a result, the width given by internet explorer is too large.

I can't find any other fields accessable for.getComputedSize() that allows me to find the 'actual' width in Internet Explorer. I tried using .getComputedStyle() to get the padding so I could subtract it from the total width, but this returns a string, and I am styling the micrositePhotoDiv element as padding: 0.75em, so this doesn't work.

What do I need to do to get the right width in internet explorer?

I want to fit images on my site to the size of their containing element, so I have this:

if (userHasMicrositePhoto) {
    var width = $('micrositePhotoDiv').getComputedSize().width;
    $('micrositePhoto').src = "flash/userImage.ashx?type=micrositePhoto&id=" + userId + "&width=" + width;
}

My handler file userImage.ashx returns the image given by the ID, scaled to the width given as a parameter.

This works fine in firefox, chrome & co, but doesn't work in Internet explorer - the image returned is too large. I think this is because .getComputedSize().width reports a width that includes the size of the padding (but on the border or margin) in Internet explorer, but returns only the usable area in other browsers. As a result, the width given by internet explorer is too large.

I can't find any other fields accessable for.getComputedSize() that allows me to find the 'actual' width in Internet Explorer. I tried using .getComputedStyle() to get the padding so I could subtract it from the total width, but this returns a string, and I am styling the micrositePhotoDiv element as padding: 0.75em, so this doesn't work.

What do I need to do to get the right width in internet explorer?

Share Improve this question asked Oct 13, 2011 at 17:03 OliverOliver 11.6k18 gold badges75 silver badges127 bronze badges 1
  • still..no answer here to this riddle. – vsync Commented Mar 24, 2014 at 18:19
Add a ment  | 

2 Answers 2

Reset to default 3

jQuery width() does just that (see jQuery source code).

As you saw, usually getComputedStyle returns the width in pixels, so you can often do (except on old IE):

var width = parseFloat(window.getComputedStyle(elem).width);

You can make the padding 0, but checking puted styles for sizes and positions is often buggy and hard to reconcile acreoss browsers.

Use offsetWidth or clientWidth instead, on all browsers.

发布评论

评论列表(0)

  1. 暂无评论