Caramba
Caramba

Reputation: 677

How to dynamically adjust DIV height?

I have the following structure:

        <div class="div1">          
            <div class="div2">
                <div class="jcarousel">
                    <ul>
                        <li>
                           <div class="item">ITEM 1</div>div>
                        </li>
                        <li>
                           <div class="item">ITEM n</div>div>
                        </li>
                    </ul>
                </div>
              <a href="#" class="jcarousel-control-prev">&lsaquo;</a>
              <a href="#" class="jcarousel-control-next">&rsaquo;</a>
            </div>
        </div>

Each item within the <ul> will have different heights though I cannot make it work unless the <div class="jcarousel"> has a fixed height, which is the opposite of what I want. I want the <div class="jcarousel"> to dynamically change its height depending on the height of the <div> inside each <li>.

Forgot to say, and it may be important. The .Jcarousel div is a carousel and and I have a next and prev controls. The .Jcarousel div should change its height according to the height of the next li to appear in the carousel.

CSS

       .div1 {
        height: auto;
        background: none repeat scroll 0% 0% rgb(255, 255, 255);
        border-width: medium 1px 1px;
        border-style: none solid solid;
        border-color: -moz-use-text-color rgb(255, 255, 255) rgb(255, 255, 255);
        padding: 20px 20px 0px;
        margin-bottom: 50px;
        box-shadow: 0px 1px 2px rgba(0, 0, 0, 0.46);
    }

    .div2 {
        margin-top: -50px;
    }

    .jcarousel {
        position: relative;
        overflow: hidden;
        height: 700px;
        margin: 0px -20px;
        padding: 0px 20px;
    }

    .jcarousel ul {
        width: 20000em;
        position: absolute;
        list-style: none outside none;
        margin: 0px;
        padding: 0px;
    }


    .jcarousel li {
        float: left;
        width: 480px;
    }

    .item {
        margin: 0px;
        padding: 0px 20px;
        height: 100%;
    }

Upvotes: 1

Views: 6998

Answers (5)

Caramba
Caramba

Reputation: 677

After trying some things I came up with the following solution:

I realised that creating in advance an array with all the heights of the <ul><li> it responds quick enough to update correctly the height of the .Jcarousel div.

JS

      <script type="text/javascript">
            $(document).ready(function(){


                  // checking the height of the first element in the <ul>
                 var count = 0;
                 var count_max = $(".jcarousel li").length;


                 var alturas = [];

                  $(".jcarousel li div.cenario").each(function(){
                        var height = $(this).height();
                        alturas.push(height);
                  });

                 $(".jcarousel").css('height',alturas[count]);


                 // changing height when pressing the prev control
                 $(document).on("click",".jcarousel-control-prev", function(){
                    if (count == 0) {

                    } else {
                        count = count-1;
                        $(".jcarousel").css('height',alturas[count]);
                    }
                 });

                // changing height when pressing the next control
                 $(document).on("click",".jcarousel-control-next", function(){
                    if (count !== count_max) {
                        count = count+1;
                       $(".jcarousel").css('height',alturas[count]);



                    } else {
                        // trying to update the counter when reach the last <li> element within the <ul>
                        count = 0;
                        $(".jcarousel").css('height',alturas[count]);
                    }


                });

            });
        </script>

Upvotes: 0

Nibin
Nibin

Reputation: 3960

Try the following code mate... :)

var totalHeight = 0;
$('.item').each(function(){
    totalHeight += $(this).height();
});

if(totalHeight < 40){
    $('.jcarousel').height(1000);
}

http://jsfiddle.net/JHZSF/

Upvotes: 3

mmpatel009
mmpatel009

Reputation: 941

Please use below jQuery code for set Dynamic height of DIV

$(document).ready(function(){
     var nHeight = 0;
     $(".jcarousel li div.item").each(function(){
            nHeight += $(this).height();
     });
     $(".jcarousel").height(nHeight);
});

Upvotes: 0

user2746831
user2746831

Reputation: 3

Maybe you can set a min-height to the class jcarousel. Like that:

.jcarousel {
        position: relative;
        overflow: hidden;
        min-height: 700px;
        margin: 0px -20px;
        padding: 0px 20px;
    } 

Upvotes: -1

matewka
matewka

Reputation: 10168

You gave height: 100% to the .item. It means it will set its height to 100% of its parent which is 700px. If you want the .jcarousel to be dependent on .item remove height: 700px from .jcarousel.

You also set float: left to li elements. It makes the ul to be 0px high. If you want to fix it add the following CSS

.jcarousel > ul:after {
    content: "";
    display: block;
    clear: both;
}

Upvotes: 0

Related Questions