简体   繁体   中英

Increase a Div height based on another Div content and it's height

Current js fiddle Example

I have two Div .mainDiv and .sideDiv . .mainDiv has long content having toggle paragraph in it . When I click the Click to expand text ,FULL paragraph shows up and the height of this content increases .

WHAT I WANT : When I press the Click to expand text, the .mainDiv height increases and also .sideDiv height should increase equal to .mainDiv height . and when Again the main DIV decrease the height of .sideDiv also decrease .

Is there any way with jQuery to make that happen ? I am not good at jquery So I am finding it difficult to make it happen if someone help me with it would be great for me . and also sorry for bad English

Thanks

HTML

<div class="mainDiv">
<p class="click"> Click to expand </p>

<p class="hide">
    and scrambled it to make a type specimen book. It has survived not only five centuries, 
    but also the leap into electronic typesetting, remaining essentially unchanged. 
    It was popularised in the 1960s with the release of Letraset
    sheets containing Lorem Ipsum passages, and more recently with
    desktop publishing software like Aldus PageMaker 
    including versions of Lorem Ipsum.

</p>    

</div>

<div class="sideDiv">
  <p>Should Expand this Div</p>
</div>

CSS

.mainDiv {
background-color:#ccc;
float:left;
width:200px;
}

.sideDiv {
background:yellow;
float:left;
width:200px;
height:auto;
}

JavaScript

$('.hide').hide();

$('.click').on('click', function () {
$('.hide').toggle();
});

Yes , Please try this.

Demo : http://jsfiddle.net/M35xJ/5/

$('.click').on('click', function () {
    $('.hide').toggle();        
    $('.sideDiv').height($('.mainDiv').height());
});

More details : http://api.jquery.com/height/

Add this to your code

$('.hide').hide();

$('.click').on('click', function () {
    $('.hide').toggle();
    var x = $('.mainDiv').css('height');      // Added
    $('.sideDiv').css('height', x);           // Added
});

FIDDLE

I took this approach, which resizes when the toggle is complete.

$('.hide').hide();

$('.click').on('click', function () {
    $('.hide').toggle(0,function() {
        $('.sideDiv').height($('.mainDiv').height());
    });

});

http://jsfiddle.net/M35xJ/7/

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