如何使用CSS和/或JQuery将div缩放以适应浏览器视口,但保留div的纵横比?
如何使用CSS和/或JQuery将div缩放以适应浏览器视口,但保留div的纵横比?
您不需要使用JavaScript。您可以使用纯CSS。
padding-top百分比相对于包含块的 宽度 进行解释。将其与子元素的position:absolute结合使用,您可以将几乎任何内容放入一个保持其宽高比的框中。
HTML:
<div class="aspectwrapper">
<div class="content">
</div>
</div>
CSS:
.aspectwrapper {
display: inline-block; /* shrink to fit */
width: 100%; /* whatever width you like */
position: relative; /* so .content can use position: absolute */
}
.aspectwrapper::after {
padding-top: 56.25%; /* percentage of containing block _width_ */
display: block;
content: '';
}
.content {
position: absolute;
top: 0; bottom: 0; right: 0; left: 0; /* follow the parent's edges */
outline: thin dashed green; /* just so you can see the box */
}
display: inline-block
会在.aspectwrapper
框的底部边缘留下一点额外的空间,因此它下面的另一个元素不会与之完全贴合。使用display:block
将解决此问题。<div class="aspectwrapper">
<img class="aspectspacer" src="http://chart.googleapis.com/chart?cht=p3&chs=160x90" />
<div class="content">
</div>
</div>
CSS:
.aspectwrapper {
width: 100%;
position: relative;
}
.aspectspacer {
width: 100%; /* let the enlarged image height push .aspectwrapper's bottom edge */
}
.content {
position: absolute;
top: 0; bottom: 0; right: 0; left: 0;
outline: thin dashed green;
}
padding-top: 100%
,这意味着高度应该是 .aspectwrapper
宽度的 100%。由于该宽度设置为 50%,因此它将随其父元素调整大小。我还添加了 overflow: hidden
,这样文本在小尺寸下不会扭曲框。 - ʇsәɹoɈ感谢 Geoff 提供的有关如何构建数学和逻辑结构的提示。以下是我的 jQuery 实现,我使用它来调整灯箱的大小,使其填充整个窗口:
var height = originalHeight;
var width = originalWidth;
aspect = width / height;
if($(window).height() < $(window).width()) {
var resizedHeight = $(window).height();
var resizedWidth = resizedHeight * aspect;
}
else { // screen width is smaller than height (mobile, etc)
var resizedWidth = $(window).width();
var resizedHeight = resizedWidth / aspect;
}
目前来说,这在笔记本电脑和移动设备的屏幕尺寸上都运行良好。
height < width
的检查只适用于1:1的宽高比。 - talljoshem
单位,并依赖于CSS min()函数加上一点数学运算。 <body>
<div class="viewport">
<p>
This should be a 16:9 viewport that fits the window.
</p>
</div>
</body>
CSS
body {
width: 100vw;
height: 100vh;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: white;
font-size: min(1vw, 1.778vh);
}
div.viewport {
width: 100em;
height: 56.25em;
background-color: lightblue;
}
div.viewport > p {
font-size: 3em;
text-align: center;
}
Javascipt:
//Responsive Scaling
let outer = document.getElementById('outer'),
wrapper = document.getElementById('wrap'),
maxWidth = outer.clientWidth,
maxHeight = outer.clientHeight;
window.addEventListener("resize", resize);
resize();
function resize(){
let scale,
width = window.innerWidth,
height = window.innerHeight,
isMax = width >= maxWidth && height >= maxHeight;
scale = Math.min(width/maxWidth, height/maxHeight);
outer.style.transform = isMax?'':'scale(' + scale + ')';
wrapper.style.width = isMax?'':maxWidth * scale;
wrapper.style.height = isMax?'':maxHeight * scale;
}
HTML:
<div id="wrap">
<div id="outer">
{{ fixed content here }}
</div>
</div>
样式:
/* Responsive Scaling */
#wrap {
position: relative;
width: 1024px;
height: 590px;
margin: 0 auto;
}
#outer {
position: relative;
width: 1024px;
height: 590px;
transform-origin: 0% 0%;
overflow: hidden;
}
这可以通过使用JQuery和一些数学知识来实现。
使用JQuery获取视口的宽度和高度以及div当前的尺寸。
$(document).width();
计算div当前的宽高比,例如宽度/高度
您需要一些逻辑来确定是先设置宽度还是高度,然后使用初始比率来计算另一侧。