简体   繁体   English

通过CSS按比例缩放图像高度,并带有溢出功能:隐藏在Fluid Grid中

[英]Proportionally scale image height via CSS with overflow:hidden in Fluid Grid

I am using the 1140 grid by Andy Taylor (cssgrid.net), and have an image gallery. 我正在使用Andy Taylor(cssgrid.net)的1140网格,并具有图片库。 I am using max-width:100% & height:auto on the images which gets the width scaling I am looking for. 我在图像上使用max-width:100% & height:auto ,它获得了我正在寻找的宽度缩放比例。

What I would like to do is limit the height of the DIV that contains the image. 我想做的是限制包含图像的DIV的高度。 My first attempt was to set the height to a fixed size and use overflow:hidden . 我的第一次尝试是将高度设置为固定大小,并使用overflow:hidden This works when the page is viewed at full size, but as the page scales down, the image reduces in size, showing more of the image. 当以全尺寸查看页面时,此功能有效,但随着页面缩小,图像尺寸减小,显示出更多图像。 What I would like is for the height of the containing DIV with the overflow to scale proportionally with the width of the image. 我想要的是包含DIV的高度,其中溢出与图像的宽度成比例地缩放。

Any thoughts? 有什么想法吗? Thanks in advance. 提前致谢。

You can resize the container's height dinamically with JavaScript (jQuery) by checking the size of the image on window.onresize event and using the code provided in this answer . 您可以通过检查window.onresize事件上图像的大小并使用此答案中提供的代码,通过JavaScript(jQuery)动态地调整容器的高度。

This would be a mockup of the code: 这将是代码的模型:

HTML: HTML:

<div class="image-gallery">
    <div class="image"><img src="" /></div>
</div>

JS: JS:

window.onresize = function(event) {
    var imgwidth = $('.image img').width();
    var imgheight = $('.image img').height();
    var divwidth = $('.image').width();
    var divheight = divwidth*imheight/imgwidth; //Aspect Ratio conversion
    $('.image').height(divheight);
}

has not been tested 尚未测试

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

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