[英]Image Displays Incorrectly On All Versions Of Internet Explorer
I have the following Javascript code that resizes images on a page: 我有以下Javascript代码调整页面上的图像大小:
var max_size = 498;
$(".slideimage").each(function(i) {
if ($(this).height() > $(this).width()) {
var h = max_size;
var w = Math.ceil($(this).width() / $(this).height() * max_size);
} else {
var w = max_size;
var h = Math.ceil($(this).height() / $(this).width() * max_size);
}
$(this).css({ height: h, width: w });
});
This code is contained within a $(document).ready() function. 此代码包含在$(document).ready()函数中。
The images display fine when viewed in Chrome, Firefox, Safari and Opera. 在Chrome,Firefox,Safari和Opera中查看时,图像显示正常。 However, when the same page is viewed in Internet Explorer 7, 8 and 9, sometimes (well, 50% of the time) when the page is first loaded the image appears at the correct width, but the height is really really small. 但是,当在Internet Explorer 7,8和9中查看同一页面时,有时(好的,50%的时间)首次加载页面时,图像以正确的宽度显示,但高度实际上非常小。 I would guesstimate the height to be around 100px high. 我估计高度约为100px。 But when the page is reloaded, the same image will display perfectly. 但是当重新加载页面时,相同的图像将完美显示。
It's a strange issue, and only happens in IE. 这是一个奇怪的问题,只发生在IE中。 Do I need to add or remove anything in that code? 我是否需要在该代码中添加或删除任何内容?
Cheers 干杯
document.ready() potentially occurs before all of the images have been loaded on a page, so the reported height and width can be 0 or, as in your case, 100. document.ready()可能在页面上加载了所有图像之前发生,因此报告的高度和宽度可以是0,或者在100的情况下。
You need to wait for the page's images to be loaded before you can find their sizes reliably, particularly on IE. 您需要等待页面的图像加载才能可靠地找到它们的大小,特别是在IE上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.