gpt4 book ai didi

javascript - 在响应式布局上动态保持 2 个 div 的高度相同

转载 作者:太空宇宙 更新时间:2023-11-04 09:35:08 27 4
gpt4 key购买 nike

我想在加载时动态地保持 2 div 相同的高度,并且希望即使在响应式网页上窗口屏幕宽度发生变化时它也保持这种状态。我一直在使用下面粘贴的代码,但它只执行一次。我想要一个事件监听器。监听内部内容何时改变宽度,并相应地调整每个 div 的高度,而无需刷新页面。任何人都可以提供有关如何执行此操作的任何见解吗?

Here is the JSFiDDle

  <style>
.col1 {background:grey;}
.col2 {background:yellow;}
</style>

<div class="content col1 col-md-4">

</div>
<div class="content col2 col-md-4">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p>
</div>

<script>
var m = $('.col1').height();
var d = $('.col2').height();
var run = Math.max (m,d);
var execute = $('.content').height(run);

execute
</script>

最佳答案

所以我能够弄明白:

首先我把javascript放在body标签里来监听窗口的大小调整

<body onresize="resizeSameHeight()">

我制作了 2 个函数,一个在页面加载时执行,另一个在窗口调整大小时执行,移除高度。它非常适合我。

  var s = sameHeight()
var r = resizeSameHeight()


function sameHeight() {
$('section').each(function() {
var m = $(this).find('.master').height();
var d = $(this).find('.slave').height();
var run = Math.max(m, d);
var execute = $(this).find('.content').height(run);

execute

});

}

function resizeSameHeight() {
$('section').each(function() {
$(this).find('.content').css("height", "");
var m = $(this).find('.master').height();
var d = $(this).find('.slave').height();
var run = Math.max(m, d);
var execute = $(this).find('.content').height(run);

execute


});

}


$(document).ready(function() {
s
});

关于javascript - 在响应式布局上动态保持 2 个 div 的高度相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40409710/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com