简体   繁体   English

jQuery调整宽高比

[英]jQuery resize to aspect ratio

How would I resize a image in jQuery to a consistent aspect ratio. 我如何将jQuery中的图像调整为一致的宽高比。 For example setting maximum height and have the width resize correctly. 例如,设置最大高度并正确调整宽度。 Thanks. 谢谢。

Here's a useful function that might do what you want: 这是一个有用的功能,可以做你想要的:

jQuery.fn.fitToParent = function()
{
    this.each(function()
    {
        var width  = $(this).width();
        var height = $(this).height();
        var parentWidth  = $(this).parent().width();
        var parentHeight = $(this).parent().height();

        if(width/parentWidth < height/parentHeight) {
            newWidth  = parentWidth;
            newHeight = newWidth/width*height;
        }
        else {
            newHeight = parentHeight;
            newWidth  = newHeight/height*width;
        }
        var margin_top  = (parentHeight - newHeight) / 2;
        var margin_left = (parentWidth  - newWidth ) / 2;

        $(this).css({'margin-top' :margin_top  + 'px',
                     'margin-left':margin_left + 'px',
                     'height'     :newHeight   + 'px',
                     'width'      :newWidth    + 'px'});
    });
};

Basically, it grabs an element, centers it within the parent, then stretches it to fit such that none of the parent's background is visible, while maintaining the aspect ratio. 基本上,它抓取一个元素,将其置于父级中心,然后将其拉伸以使父级的背景都不可见,同时保持宽高比。

Then again, this might not be what you want to do. 然后,这可能不是你想要做的。

You could calculate this manually, 你可以手动计算,

ie: 即:

function GetWidth(newHeight,orginalWidth,originalHeight)
{
if(currentHeight == 0)return newHeight;
var aspectRatio = currentWidth / currentHeight;
return newHeight * aspectRatio;
}

Make sure you use the ORIGINAL values for the image otherwise it will degrade over time. 确保使用图像的ORIGINAL值,否则它会随着时间的推移而降级。

EDIT: example jQuery version (not tested) 编辑:示例jQuery版本(未测试)

jQuery.fn.resizeHeightMaintainRatio = function(newHeight){
    var aspectRatio = $(this).data('aspectRatio');
    if (aspectRatio == undefined) {
        aspectRatio = $(this).width() / $(this).height();
        $(this).data('aspectRatio', aspectRatio);
    }
    $(this).height(newHeight); 
    $(this).width(parseInt(newHeight * aspectRatio));
}

Use JQueryUI Resizeable 使用JQueryUI Resizeable

$("#some_image").resizable({ aspectRatio:true, maxHeight:300 });

aspectRatio: true -> maintain original aspect ratio aspectRatio:true - >保持原始宽高比

There's no accounting for the amount of copy and pasters out there eh! 没有考虑复制和粘贴的数量呃! I also wanted to know this and all I saw were endless examples of scaling width OR height.. who would want the other overflowing?! 我也想知道这一点,我看到的只是缩放宽度或高度的无数例子..谁会想要另一个溢出?!

  • Resize width AND height without the need for a loop 调整宽度和高度,无需循环
  • Doesn't exceed the images original dimensions 不超过图像原始尺寸
  • Uses maths that works properly ie width/aspect for height, and height*aspect for width so images are actually scaled properly up and down :/ 使用正常工作的数学,即高度的宽度/宽高度,以及宽度的高度*宽高比,因此图像实际上可以上下缩放:/

Should be straight forward enough to convert to javascript or other languages 应该直截了当地转换为javascript或其他语言

////////////// //////////////

private void ResizeImage(Image img, double maxWidth, double maxHeight)
{
    double srcWidth = img.Width;
    double srcHeight = img.Height;

    double resizeWidth = srcWidth;
    double resizeHeight = srcHeight;

    double aspect = resizeWidth / resizeHeight;

    if (resizeWidth > maxWidth)
    {
        resizeWidth = maxWidth;
        resizeHeight = resizeWidth / aspect;
    }
    if (resizeHeight > maxHeight)
    {
        aspect = resizeWidth / resizeHeight;
        resizeHeight = maxHeight;
        resizeWidth = resizeHeight * aspect;
    }

    img.Width = resizeWidth;
    img.Height = resizeHeight;
}

This is a good solution if you need perfect height and width aspect ratio after crop it will give perfect crop ratio 如果您在裁剪后需要完美的高度和宽度宽高比,这将是一个很好的解决方案,它将提供完美的裁剪比率

 getPerfectRatio(img,widthRatio,heightRatio){ if(widthRatio < heightRatio){ var height = img.scalingHeight - (img.scalingHeight % heightRatio); var finalHeight = height var finalWidth = widthRatio * (height/heightRatio); img.cropHeight = finalHeight; img.cropWidth = finalWidth } if(heightRatio < widthRatio){; var width = img.scalingWidth - (img.scalingWidth % widthRatio); var finalWidth = width; var finalHeight = heightRatio * (width/widthRatio); img.cropHeight = finalHeight; img.cropWidth = finalWidth } return img } 

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

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