Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IE javascript图像大小调整不工作_Javascript_Jquery_Internet Explorer - Fatal编程技术网

IE javascript图像大小调整不工作

IE javascript图像大小调整不工作,javascript,jquery,internet-explorer,Javascript,Jquery,Internet Explorer,我有一点javascript在IE中不起作用 function resize($img) { var max_size_w = 200; var max_size_h = 200; var h = $img.height(); var w = $img.width(); if (h > max_size_h) { h = max_size_h; w = Math.ceil($img.width() / $img.hei

我有一点javascript在IE中不起作用

function resize($img) {
    var max_size_w = 200;
    var max_size_h = 200;
    var h = $img.height();
    var w = $img.width();
    if (h > max_size_h) {
        h = max_size_h;
        w = Math.ceil($img.width() / $img.height() * max_size_h);
    }
    if (w > max_size_w) {
        h = Math.ceil($img.height() / $img.width() * max_size_w);
        w = max_size_w;
    }
    $img.css({ height: h, width: w });
}

$(window).load(function() {
    // Size the images correctly
    $(".personPictureImage").each(function() {
        var $img = $(this).find("img");
        $img.load(function() { resize($(this)); });
        if($img.height())
            resize($img);
    });
});
在其他浏览器中,它会调整图像大小,以适合200x200框。在IE中,我得到的尺寸是30px×28px。在chrome中,我得到200像素乘以142像素

我知道IE有问题,通常是一个糟糕的浏览器,但无论如何我都在努力支持它。如何修复代码以在IE中工作?

尝试替换

var max_size_w = 200;
var max_size_h = 200;
var h = $img.height();
var w = $img.width();
if (h > max_size_h) {
    h = max_size_h;
    w = Math.ceil($img.width() / $img.height() * max_size_h);
}
if (w > max_size_w) {
    h = Math.ceil($img.height() / $img.width() * max_size_w);
    w = max_size_w;
}
根据这个计算

var MAX=200,wi=$img.width(),he=$img.height(),
r=MAX/Math.max(wi,he),
w=Math.round(wi*r),
h=Math.round(he*r);

从这里开始


注:r是比率&我也会使用纯javascript,因为所有浏览器都支持您所做的一切。

您是否尝试过一步一步地调试计算以了解它在哪里失败?哪个版本的IE?都是吗?
var MAX=200,
r=MAX/Math.max(this.width,this.height),
w=Math.round(this.width*r),
h=Math.round(this.height*r);