繁体   English   中英

如何在zurb基础上保持图像对固定高度的响应

[英]how to keep make image responsive with fixed height in zurb foundation

我希望将图像的高度保持在300px并自动隐藏左右尺寸的图像,同时调整图像大小以使图像中心始终可见。 在此输入图像描述 在此输入图像描述

<div class="large-12 columns">
    <img src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Auckland_airport_international_terminal.jpg/1024px-Auckland_airport_international_terminal.jpg"/><p></p>
</div>

我的方法是将图像用作背景图像。 这将使您能够将其居中并在调整大小时裁剪,同时仍保持所需的300px高度。

演示http://jsfiddle.net/NSS2T/4/

div {
    height: 300px;
    max-width: 100%;
    background: url('myImage.jpg') center center;
}

编辑

当您使用以编程方式生成的图像时,您需要通过jQuery添加计算样式,与我上面给出的CSS示例相对应。

演示http://jsfiddle.net/NSS2T/3/

HTML

<div>
    <img src="http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2014/3/10/1394464252285/5a3508c4-7dd0-4bac-90ed-f13c11b53cef-460x276.jpeg" class="upload" />
</div>

CSS 
// this will offset the image by 50%

img {
    display: block;
    position: absolute;
    left: 50%;
}

jQuery 
// this will loop through all images with a class of .upload and then apply a calculated margin offest to center the image

var imgWidth;

$('img.upload').each(function() {
    imgWidth = $(this).width() / 2;
    $(this).css('margin-left', '-'+imgWidth+'px');
}); 

暂无
暂无

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

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