我使用 jQuery 代码来扩展容器内 Div 的高度。这是 JSFiddle 链接。
但是,当调整浏览器宽度时,Divs 高度是固定的,并且文本会溢出容器。是否可以更改以下代码以扩展容器高度和其中的 Divs 以便文本保留在其中?
jQuery代码:
$(window).load(function () {
$(window).resize(function () {
$(document).ready(function () {
var heightArray = $(".container>div").map(function () {
return $(this).height();
}).get();
var maxHeight = Math.max.apply(Math, heightArray);
$(".container>div").height(maxHeight);
$(".container>div").height(maxHeight);
});
}).trigger('resize');
});
风格:
.container {
height: auto;
float:left; }
#half {
width:48%;
margin:0.5%;
padding:0.5%;
float:left;
background-color:#1589FF; }
HTML:
<div class="container">
<div id="half">
<h1>About</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam sodales urna non odio egestas tempor. Nunc vel vehicula ante. Etiam bibendum iaculis libero, eget molestie nisl pharetra in. In semper consequat est, eu porta velit mollis nec. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam sodales urna non odio egestas tempor. Nunc vel vehicula ante. Etiam bibendum iaculis libero, eget molestie nisl pharetra in. In semper consequat est, eu porta velit mollis nec. Curabitur posuere enim eget turpis feugiat tempor. Etiam ullamcorper lorem dapibus velit suscipit ultrices. Proin in est sed erat facilisis pharetra.</div>
<div id="half">
<h1>News</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam sodales urna non odio egestas tempor. Nunc vel vehicula ante. Etiam bibendum iaculis libero, eget molestie nisl pharetra in. In semper consequat est, eu porta velit mollis nec. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam sodales urna non odio egestas tempor. Nunc vel vehicula ante. Etiam bibendum iaculis libero, eget molestie nisl pharetra in.
</div>
</div>