我正在尝试使用 jquery 制作一本“动画书”,并已开始使用我购买的插件。
我自定义了 JS 以在调整浏览器大小时将书的大小调整为窗口的 100%,并设置最大尺寸,因为我不希望图像变得比原始尺寸大。
话虽如此,我一直在努力让图像在调整大小时保持其纵横比。我在网上四处张望,但找不到任何可以帮助我解决这个问题的东西。
有没有人对如何实现这个有任何想法?如果您需要更多信息或说明,请告诉我。
编辑:
book 元素使用 JS 调整大小到图像的最大大小。
当书的大小调整为小于其最大尺寸时,书的高度和宽度变为窗口的 100%。
我需要一些 JS 来使这本书保持其纵横比。
EX 窗口被调整为非常宽(大宽度)但非常短(小高度)。
目前这本书会拉伸(stretch)并填充非常大的宽度,这会因为高度太小而扭曲图像。
如何缩小宽度以补偿较小的高度(反之亦然)?
提前感谢大家!
Here is the Fiddle
#mybook {
margin:0 auto;
}
body {
overflow:hidden;
}
img {
max-height:300px;
max-width:300px;
}
最佳答案
看看这段代码: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
}
});
});
或者你可以用 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>
关于javascript - Jquery Resize对象在窗口调整大小并保持纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22081599/