今天我在尝试操作图像时遇到了一个有趣的 IE8 情况。
我通过更改它们的 url 来替换加载的图像,当它们被加载时,我试图正确地挤压它们并以视口元素为中心(新图像不像它们的前辈那样是方形的)。但是在 IE8 中(还没有测试过 IE7,从同事那里听说过 IE9 - 一切都很好)图像没有缩放,它们只是以原来的大小下降,而我的
img.height(105);
img.css('margin-left', (shift?-shift:0));
被简单地忽略了。这是解决问题的代码。
$('.people-images img').each(function () {
var img = $(this);
var oUrl = img.attr('src');
oUrl = oUrl.replace(/[SM]Thumb/, 'LThumb');
img.bind('load', function () {
var img = $(this);
if (this.width > this.height) {
var shift = (this.width / (this.height / 105) - 105) / 2;
img.height(105);
img.css('margin-left', (shift?-shift:0));
}
else {
var shift = (this.height / (this.width / 105) - 105) / 2;
img.width(105);
img.css('margin-top', (shift?-shift:0));
}
});
img.attr('src', oUrl);
img.attr('style', null);
img.parent().attr('style', null);
});
请寻找我的解决方案的自我答案。