简体   繁体   中英

Dynamically keep 2 divs the same height on a responsive layout

I would like to dynamically keep 2 div the same height on load, and want it to remain that way even when window screen width changes on a responsive webpage. I have been using the code pasted below, however it only executes once. I would like to have an event listener. to listen for when the inner contents change width, and adjust the height of each div accordingly, without having to refresh the page. Can anyone provide any insight on how I can do this?

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>

So I was able to figure it out:

First I put javascript in the body tag to listen for the resizing of the window

<body onresize="resizeSameHeight()">

I made 2 functions, one that executes on page load and one that removes height and executes when window resizes. It works perfectly for me.

  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
            });

i dont get the question, you want the divs to stay the same height even when the width change or you want 2 divs with an specific height adaptable to width changes?

Edit:

Then you could just use css

Define a div container width a height an then inside that div define the divs you want to adapt.

heres the example

 .container{height: 500px;} .col1, .col2{height: 45%; overflow:hidden;margin:2% auto;} .col1 {background:grey;} .col2 {background:yellow;} 
 <div class="container"> <div class="content col1 col-md-4">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.</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. </p> </div> </div> 

The container have a defined height and the childs div have a height that is a % of the height, so even when the width change, both will maintain the same height. When the content gets to the point of overflowing you have to use mediaquerys to define the parent height depends on certain widths.

CSS can do that with Flexbox

 .col1 { background: grey; width: 33.33%; } .col2 { background: yellow; width: 33.33%; } .row { display: flex; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <div class="row"> <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> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM