简体   繁体   English

jQuery在窗口调整大小时调整对象大小并保持宽高比

[英]Jquery Resize object on window resize and keep aspect ratio

I am trying to make a "flip book" using jquery and have started with a plugin that I bought. 我正在尝试使用jquery制作“翻书”,并且从购买的插件开始。
I customized the JS to get the book to resize to 100% of the window when the browser is resized, and set max sizes because I do not want the images to grow larger than their original size. 我自定义了JS,以便在调整浏览器大小时将书的大小调整为窗口的100%,并设置了最大大小,因为我不希望图像的大小超过其原始大小。

With all that said, I have been trying to get the images to retain their aspect ratio when resized. 综上所述,我一直在尝试使图像在调整大小时保持其纵横比。 I looked around online but couldn't find anything to help me with this. 我在网上四处张望,但找不到任何可帮助我的方法。
Does anyone have any ideas on how to accomplish this? 是否有人对如何实现这一目标有任何想法? Please let me know if you need more info or clarification. 如果您需要更多信息或澄清,请告诉我。

EDIT: 编辑:
The book element is resized with JS up to the max size of the images. 用JS调整book元素的大小,直到图像的最大大小。
When the book is resized to anything smaller than its max size, the book's height and width become 100% of the window. 当将书的大小调整为小于其最大大小的大小时,书的高度和宽度将成为窗口的100%。
I need some JS to make the book keep its aspect ratio. 我需要一些JS来使这本书保持其长宽比。

EX- window is resized to be very wide (large width) but very short (small height). 将EX窗口调整为非常宽(大宽度)但非常短(小高度)。
Currently the book will stretch and fill the very large width which distorts the image because the height is too small. 目前,由于高度太小,这本书将拉伸并填充非常大的宽度,这会使图像失真。

How do I make the width scale down to compensate for the small height (and vice versa)? 如何缩小宽度以补偿较小的高度(反之亦然)?
Thanks to everyone in advance! 预先感谢大家!

Here is the Fiddle 这是小提琴

#mybook {
margin:0 auto;
}
body {
overflow:hidden;
}
img {
max-height:300px;
max-width:300px;
}

Look at this code: http://ericjuden.com/2009/07/jquery-image-resize/ 查看以下代码: http : //ericjuden.com/2009/07/jquery-image-resize/

$(document).ready(function() {
    $('.story-small img').each(function() {
        var maxWidth = 100; // Max width for the image
        var maxHeight = 100;    // Max height for the image
        var ratio = 0;  // Used for aspect ratio
        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height

        // Check if the current width is larger than the max
        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = width * ratio;    // Reset width to match scaled image
        }

        // Check if current height is larger than max
        if(height > maxHeight){
            ratio = maxHeight / height; // get ratio for scaling image
            $(this).css("height", maxHeight);   // Set new height
            $(this).css("width", width * ratio);    // Scale width based on ratio
            width = width * ratio;    // Reset width to match scaled image
            height = height * ratio;    // Reset height to match scaled image
        }
    });
}); 

Or you could do it with css: 或者,您也可以使用CSS执行此操作:

<div style="height: 100px">
<img src="http://www.getdigital.de/images/produkte/t4/t4_css_sucks2.jpg"
    style="max-height: 100%; max-width: 100%">
</div>

There is not really enough technical information in your question to be able to give you a good answer, but this is typically a problem that can be solved using CSS. 您的问题中确实没有足够的技术信息可以为您提供良好的答案,但这通常是可以使用CSS来解决的问题。 It shouldn't require any JS/jQuery. 它不需要任何JS / jQuery。

The tag naturally keeps the aspect ratio of the image if you set something like: 如果您进行以下设置,则标签自然会保留图像的长宽比:

img {
    width: 100%;
    height: auto;
}

Alternatively, you could set the image as a background image and use the CSS property 另外,您可以将图片设置为背景图片并使用CSS属性

.image {
    background-size: contain;
}

to make sure the image uses the most of the available width and height. 确保图像使用最大的可用宽度和高度。

As I see, you are resizing with JavaScript the container div of the images. 如我所见,您正在使用JavaScript调整图像的容器div的大小。 So, you can set the following CSS to make the image always fit in that div and maintain their aspect ratio. 因此,您可以设置以下CSS,以使图像始终适合该div并保持其宽高比。

img {
    max-height:100%;
    max-width:100%;
}

Here is a fiddle . 这是一个小提琴

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

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