【发布时间】:2012-07-01 20:18:00
【问题描述】:
以下代码在 Firefox 和 Chrome 中运行良好,但在 Safari 中无法运行(在 Mac 和 iPad 上测试):http://jsfiddle.net/eFd87/。
<div id="wrapper">
<div id="content">
<img src="http://farm3.staticflickr.com/2783/4106818782_cc6610db2c.jpg">
</div>
</div>
#wrapper {
position: relative;
padding-bottom: 33.33%; /* Set ratio here */
height: 0;
}
#content {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: green;
text-align: center;
}
#content img {
max-height: 100%;
}
目标是让包装 div 保持固定的纵横比(在我的 Web 应用程序中它是一个轮播),并且其中的图像调整大小以适合 div(和中心)。
在 Safari 上,由于 height: 0 (这将使 img 的高度为 0),图像不会显示。使用height: 100% 显示图像但不适合 div(它溢出)。
您是否看到任何解决此问题的方法?我已经做了好几个小时了……
【问题讨论】:
-
Matthieu,您可能需要再次查看链接页面。我的回答提出了两种方法,其中一种是纯 CSS,两种都是无 javascript 的。
-
看,最好的办法是在 JS 上动态计算宽度/高度,因为让它在 Safari 上工作非常头疼,主要是当你因为导航栏而有动态高度时,等例如,对于 9/16(手机)的纵横比,请执行:height: window.innerHeight;宽度:${
${window.innerHeight * 9 / 16}px}
标签: css image safari aspect-ratio