【发布时间】:2017-07-24 10:56:24
【问题描述】:
我正在使用 JQuery 使 DIV 具有与其宽度相同的高度并响应更新。
我还需要使另一个 DIV(具有不同的纵横比)保持与方形 div 相同的高度。
即使在调整浏览器窗口大小时,黑色 DIV 也应与红色和白色 div 的高度匹配。
function update() {
$(".match").each(function() {
var height = $(this).width();
console.log(height);
$(this).css('height', height + 'px');
});
}
update();
$(window).resize(function() {
update();
});
.main {
width: 100%;
max-width: 1200px;
margin: 0 auto;
height: 2000px;
background-color: #333333;
}
.square {
width: 10%;
background-color: #ffffff;
float: left;
}
.oblong {
width: 40%;
float: left;
max-height: 150px;
background-color: #000000;
}
.color {
background-color: red !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div class="main">
<div class="oblong match"></div>
<div class="square match"></div>
<div class="square match color"></div>
<div class="square match"></div>
<div class="square match color"></div>
<div class="square match"></div>
<div class="square match color"></div>
</div>
【问题讨论】:
-
所以你想要的输出
-
如果我理解正确,当您调整窗口大小时,您想将 div“长方形匹配”调整为与另一个相同的尺寸吗?