简体   繁体   English

使用jquery按比例调整图像大小时的小错误。

[英]Small bug when I resize image proportionally using jquery.

( 10 reputation for showing images??... really?...) (10个显示图像的声誉?? ...真的吗?...)

Hi guys. 嗨,大家好。

The problem I'm undergoing is that, 我正在经历的问题是,

when I resize images, they are resized more than once I believe.

For example, 例如,

I uploaded a square image, but the result is rectangle.

When the wrong result comes, I refresh the page with F5 key, and they look square. 当出现错误结果时,我用F5键刷新页面,它们看上去很方形。

only height og the resized image is the problem. 只有高度和调整大小后的图像才是问题。

Below is the code I used to resize those images. 下面是我用来调整这些图像大小的代码。

function thumbnail_resize(obj) {
    var maxW = 50;      var maxH = 50;
    var wid = obj.width();
    var hei = obj.height();

    if( wid > maxW ) {
        hei *= (maxW / wid);
        wid = maxW;
    }
    if( hei > maxH ) {
        wid *= (maxH / hei);
        hei = maxH;
    }
    obj.attr('width', wid);
    obj.attr('height', hei);

    delete maxW, maxH, wid, hei;
    return;
}

Currently I believe the resizing ratio ( ie => hei *= maxW / wid; ) is applied several times when resizing the height of those images. 目前,我相信在调整那些图像的高度时,会多次应用大小调整比率(即=> hei * = maxW / wid;)。

Please give me some wisedom!!! 请给我一些智慧!!!

Firstly if you want to keep aspect ratio, you don't need to apply height and width at the same time: 首先,如果要保持宽高比,则无需同时应用高度和宽度:

function thumbnail_resize(obj) {
    var maxW = 50;      var maxH = 50;
    var wid = obj.width();
    var hei = obj.height();

    if ( wid > hei ) {
       if( wid > maxW ) {
            obj.attr('width', maxW);
       }
    } else {
        if( hei > maxH ) {
            obj.attr('height', maxH);
        }
    }

    return;
}

You should see this article about image loading and manipulating them on load. 您应该看到有关图像加载和在加载时对其进行操作的本文 You can't simply expect it to work on document.ready, especially in IE. 您不能仅仅期望它可以在document.ready上工作, 尤其是在IE中。

It appears that part of your problem is that you are looking at the image's height and width in $(document).ready() and the images may not all be loaded yet which means their height and width may not yet be valid. 看来问题的部分原因是您正在$(document).ready()中查看图像的高度和宽度,并且图像可能尚未全部加载,这意味着它们的高度和宽度可能无效。 When you hit reload, the images will come from the browser cache and they will be loaded much faster and thus they may actually be ready in $(document).ready() after the reload. 当您单击reload时,图像将来自浏览器缓存,并且加载速度会更快,因此,在重新加载后,它们实际上可以在$(document).ready()准备好。

If these images are all in the original HTML of your page (and not inserted dynamically), then you can trigger your resize function with $(window).load() instead and the images will all be loaded when that triggers. 如果这些图像全部在页面的原始HTML中(而不是动态插入),则可以使用$(window).load()触发调整大小函数,并且在触发时将全部加载图像。

In addition, you can constrain your setting of the height and width to only the longest side - you don't have to set both. 此外,您可以将高度和宽度的设置限制为仅最长的一面-不必同时设置两者。 Setting just one dimension will allow the browser to resize the other and maintain the original aspect ratio. 仅设置一个尺寸将允许浏览器调整另一个尺寸,并保持原始的宽高比。 If your max height and max width are the same, it can be as simple as this: 如果最大高度和最大宽度相同,则可以这样简单:

function thumbnail_resize(obj) {
    var maxDimension = 50;
    var width = obj.width();
    var height = obj.height();
    // if either dimension is too big, then make the longer one smaller
    if (Math.max(width, height) > maxDimension) {
        if (height > width) {
            obj.height(maxDimension);
        } else {
            obj.width(maxDimension);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM