首先这是我正在寻找的图像:
我想在顶部放置一个标题,在标题下方放置一个段落,在该段落下方放置一个 SVG 图像。我希望它能够在任何尺寸的设备上正确缩放(320x480 是我要使用的最小尺寸,即 iPhone4 尺寸的设备)。
我有以下代码:
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
</head>
<body>
<div style="height:100%;position:absolute;top:0;left:0;bottom:0;right:0;">
<div>
<h2>Title</h2>
<p>Some long paragraph will be inserted here</p>
</div>
<div>
<img src="mySvg.svg" /> <!-- This image should scale to fit the remaining of the screen size. -->
</div>
</div>
</body>
</html>
问题是在小型设备上有滚动条,您需要向下滚动才能查看图像的其余部分。我希望它能够正确缩放,以便它完全适合屏幕,这样就不需要滚动。
编辑:我正在使用框架,由于该框架,我无法编辑 HTML 或 BODY 标签
最佳答案
尝试使用 Flexbox + 位置技巧的方法。
<强> jsFiddle
.container {
height: 100%;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
display: flex;
flex-direction: column;
}
.container div:last-child {
flex: 1;
position: relative;
}
.container div:last-child img {
position: absolute;
max-width: 100%;
max-height: 100%;
}
<div class="container">
<div>
<h2>Title</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
<div>
<img src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/w3c.svg" />
</div>
</div>
或者,使用 Flexbox + 尺寸包含的背景图像。
<强> jsFiddle
.container {
height: 100%;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
display: flex;
flex-direction: column;
}
.container div:last-child {
flex: 1;
background: url("https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/w3c.svg") 0 0 no-repeat;
background-size: contain;
}
<div class="container">
<div>
<h2>Title</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
<div></div>
</div>
关于javascript - 自动缩放图像以适应设备高度的其余部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36378611/