JeroenVdb
JeroenVdb

Reputation: 808

Hide <li> element when not fully visible

Basic: I have an unordered list with many listitems (containing a picture and title). Not knowing how long the title will be I don't know the hight of each listitem. To the left of this list is a big picture with title that "sets" the hight of my list (depending of the height en length of the title).

What I try to do: When the last listitem of my list can't be fully displayed, hide the whole listitem.

Examples: http://d.pr/eAlK & http://d.pr/8MVO

In the second example the 4th article is hidden because it wouldn't be fully visible.

Is this possible? I prefer I clean way that works crossbrowser if possible...

Upvotes: 3

Views: 3515

Answers (3)

Starx
Starx

Reputation: 78991

Basically the idea is to calculate all the height of child elements together and compare with maximum allowed height.

Using jQuery

var neededHeight = $("#lhscontainer").outerHeight(); //you can also use static value 
var totalChildHeight = 0;
$("ul").children("li").each(function() {
    totalChildHeight+= $(this).outerHeight(); //add up to the total height 
    if(totalChildHeight> neededHeight) {  //compare if the height limit was exceeded
       $(this).hide(); // if it did, hide the current element
       $(this).nextAll().hide(); //hide all other list also
       return false; // break the loop to stop the further iteration
    }
});

Upvotes: 3

yunzen
yunzen

Reputation: 33439

Found a noJS solution which works in any browser except IE (sigh! But degrades gracefully) Maybe there is a solution with this one: http://codeasily.com/jquery/multi-column-list-with-jquery

Uses column-count CSS.
Any chance of having a column-count enabling script like html5.js (which doesn't work)?

Some awkward border-bug in Safari

http://jsfiddle.net/HerrSerker/f5Zjt/4/


HTML

<div class="wrap">
    <div class="wrap_1">
        <img src="http://lorempixel.com/400/200/sports/2" width="400" height="200" />
        <h2>Some text, can be multiple lines</h2>
    </div>
    <div class="wrap_2">
        <div class="inner">                
            <div class="wrap_3">
                <img src="http://lorempixel.com/40/40/sports/1" width="40" height="40" />
                <div class="detail">Some text, can be multiple lines, that is possible</div>
            </div>
            <div class="wrap_3">
                <img src="http://lorempixel.com/40/40/sports/1" width="40" height="40" />
                <div class="detail">Some text, can be multiple lines, that is possible</div>
            </div>
            <div class="wrap_3">
                <img src="http://lorempixel.com/40/40/sports/3" width="40" height="40" />
                <div class="detail">Some text, can be multiple lines, that is possible</div>
            </div>
            <div class="wrap_3">
                <img src="http://lorempixel.com/40/40/sports/1" width="40" height="40" />
                <div class="detail">Some text, can be multiple lines</div>
            </div>
            <div class="wrap_3">
                <img src="http://lorempixel.com/40/40/sports/1" width="40" height="40" />
                <div class="detail">Some text, can be multiple lines</div>
            </div>
        </div>
    </div>
</div>​

CSS

body {
    padding: 10px;
}
.wrap {
    width: 600px;
    border: 1px solid silver;
    padding: 5px;
    overflow: hidden;
    position: relative;
}
.wrap_1 {
    float: left;
    width: 400px;
    padding: 5px;
    border: 1px solid gold;
    overflow: hidden;
    text-overflow: ellipis;
}
.wrap_2 {
    top: 5px;
    bottom: 5px;
    right: 5px;
    position: absolute;
    width: 170px;
    padding: 5px;
    border: 1px solid gold;
    overflow: hidden;
}
.wrap_2 > .inner {
    position: absolute;
    top:5px;
    bottom: 5px;
    left: 5px;
    width: 350px;


  /***** THE MAGIC HAPPENS HERE ******/
    -moz-column-width: 170px; 
    -webkit-column-width: 170px; 
    -o-column-width: 170px; 
    -ms-column-width: 170px; 
    column-width: 170px; 

    -moz-column-gap: 5px; 
    -webkit-column-gap: 5px;
    -o-column-gap: 5px; 
    -ms-column-gap: 5px; 
    column-gap: 5px; 
}
.wrap_3 {
    width: 158px;
    padding: 5px;
    border: 1px solid brown;
    overflow: hidden; 
}
.wrap_3+.wrap_3 {
    margin-top: 5px;
}
.wrap_1 h2 {
    font-size: 24px;
    font-family: sans-serif;
}
.wrap_3 img {
    vertical-align: top;
    display: inline-block;
    *zoom: 1;
    *display: inline;
    width: 40px;
    margin-right: 5px;
}
.wrap_3 .detail {
    display: inline-block;
    *zoom: 1;
    *display: inline;
    overflow: hidden;
    font-size: 14px;
    font-family: sans-serif;
    width: 108px;
    vertical-align: top;
}
​

Upvotes: 2

CyrillC
CyrillC

Reputation: 557

Using Starx Answer you can set the neededHeight to the height of your Image+title div

Upvotes: 0

Related Questions