【问题标题】:Hide <li> element when not fully visible在不完全可见时隐藏 <li> 元素
【发布时间】:2012-03-16 21:23:18
【问题描述】:

基本:我有一个包含许多列表项(包含图片和标题)的无序列表。不知道标题会持续多久,我不知道每个列表项的高度。此列表的左侧是一张大图,其标题“设置”了我列表的高度(取决于标题的高度和长度)。

我尝试做的事情:当我的列表的最后一个列表项无法完全显示时,隐藏整个列表项。

示例:http://d.pr/eAlK & http://d.pr/8MVO

在第二个示例中,第 4 篇文章被隐藏,因为它不会完全可见。

这可能吗?如果可能的话,我更喜欢我清理跨浏览器的方式......

【问题讨论】:

  • 如果你用你的标记设置一个小提琴会更好吗?
  • 您是在使用像 jQuery 这样的工具包还是想要一个纯 JavaScript 的答案?
  • @JeroenVdb - 确保将 jQuery 添加为问题的标签!我给你加了。

标签: javascript jquery html css


【解决方案1】:

基本上这个想法是计算所有子元素的高度,并与最大允许高度进行比较。

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

【讨论】:

  • 不错!你发帖的时候还在写类似的东西,所以我不会发帖。我会使用左侧容器元素的outerHeight() 值设置neededHeight 变量,以使其保持动态。
  • @JamesSouth,是的...最好将其包含在答案中
  • @Starx 非常感谢!如果有一个仅 css 的解决方案会很好,但这看起来是一个很棒且可靠的 jQuery 替代方案!谢谢!
  • @Starx,小错误修正:“totalHeight”变量应该是“totalChildHeight”
  • @JeroenVdb,我的天啊,我不敢相信那是什么错别字。现在修好了。谢谢
【解决方案2】:

找到了一个 noJS 解决方案,它可以在除 IE 之外的任何浏览器中运行(叹气!但会优雅地降级) 也许有一个解决方案:http://codeasily.com/jquery/multi-column-list-with-jquery

使用column-count CSS。
有没有像 html5.js 这样的列计数启用脚本(不起作用)?

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

【讨论】:

    【解决方案3】:

    使用 Starx Answer,您可以将 neededHeight 设置为 Image+title div 的高度

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      相关资源
      最近更新 更多