假设我有一个flexbox容器,其内容可能会溢出父容器。我想要的是,如果任何项目比容器大了任何隐藏的数量。如果我设置overflow: hidden
,它只会隐藏该项目的溢出部分,而不是整个项目。
请考虑以下事项:
<nav id="top-nav">
<div id="main-nav-container">
<div class="menu">
<ul>
<li><a href="/">Item 1</a></li>
<li><a href="/">Item 2</a></li>
<li><a href="/">Item 3</a></li>
<li><a href="/">Item 4</a></li>
</ul>
</div>
<div class="menu">
<ul>
<li><a href="/">Other 1</a></li>
<li><a href="/">Other 2</a></li>
</ul>
</div>
</div>
</nav>
CSS:
#top-nav, #top-nav div.menu ul li {
background-color: #444;
}
#main-nav-container {
margin: 0 auto;
max-width: 1200px;
padding: 0 40px;
display: -webkit-inline-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-pack: justify;
-moz-justify-content: -moz-space-between;
-ms-justify-content: -ms-space-between;
justify-content: space-between;
}
#top-nav div.menu {
-webkit-flex: 1;
display: -webkit-inline-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
}
#top-nav div.menu:last-child {
display: -webkit-inline-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-justify-content: flex-end;
-webkit-flex-direction: row;
flex-direction: row;
justify-content: flex-end;
}
#top-nav div.menu,
#top-nav div.menu ul {
text-align: left;
alignment-baseline: baseline;
margin: 0;
padding: 0;
}
#top-nav div.menu ul {
list-style: none;
}
#top-nav div.menu > ul {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
white-space: nowrap;
}
#top-nav div.menu li {
-webkit-flex-shrink: 0;
-moz-flex-shrink: 0;
-ms-flex-shrink: 0;
flex-shrink: 0;
margin: 0
position: relative;
font-size: 1.1em;
cursor: pointer;
}
#top-nav div.menu ul li a {
color: #E6E6E6;
text-decoration: none;
display: block;
padding: 7px;
}
如果窗口缩小,我想要&#34;项目4&#34;一旦它开始被&#34;其他1&#34;
重叠,就会隐藏起来一旦我实现了这一点,我还需要一个能够锁定那些隐藏的选择器。
答案 0 :(得分:9)
您的问题的解决方法是添加以下CSS代码:
#top-nav div.menu > ul {
flex-wrap: wrap;
height: 34px;
overflow: hidden;
}
如果你想知道隐藏了哪些元素,你应该使用Javascript / jQuery来解决你的问题,因为你不知道使用CSS。
答案 1 :(得分:2)
我制作了一个多部分的代码笔,演示了上述答案(例如,使用隐藏的溢出和高度),以及如何扩展/折叠溢出的项目
示例1:https://codepen.io/Kagerjay/pen/rraKLB(使用其他人提到的aanswer的简单示例)
示例2:https://codepen.io/Kagerjay/pen/LBErJL(单个事件处理程序在溢出的项目上显示更多/不显示)
示例3:https://codepen.io/Kagerjay/pen/MBYBoJ(许多事件处理程序在溢出项目中显示更多/显示更少)
我在下面也附加了示例3,我使用了Jade / Pug,所以下面是它的详细示例
// Overflow boolean checker
function isOverflown(element){
return element.scrollHeight > element.clientHeight || element.scrollWidth > element.clientWidth;
}
// Jquery Toggle Text Plugin
$.fn.toggleText = function(t1, t2){
if (this.text() == t1) this.text(t2);
else this.text(t1);
return this;
};
// Toggle Overflow
function toggleOverflow(e){
e.target.parentElement.classList.toggle("grid-parent--showall");
$(e.target).toggleText("Show More", "Show LESS");
}
// Where stuff happens
var parents = document.querySelectorAll(".grid-parent");
parents.forEach(parent => {
if(isOverflown(parent)){
parent.lastElementChild.classList.add("btn-show");
parent.lastElementChild.addEventListener('click', toggleOverflow);
}
})
body {
background-color: #EEF0ED;
margin-bottom: 300px;
}
.grid-parent {
margin: 20px;
width: 250px;
background-color: lightgrey;
display: flex;
flex-wrap: wrap;
overflow: hidden;
max-height: 100px;
position: relative;
}
.grid-parent--showall {
max-height: none;
}
.grid-item {
background-color: blue;
width: 50px;
height: 50px;
box-sizing: border-box;
border: 1px solid red;
}
.grid-item:nth-of-type(even) {
background-color: lightblue;
}
.btn-expand {
display: none;
z-index: 3;
position: absolute;
right: 0px;
bottom: 0px;
padding: 3px;
background-color: red;
color: white;
}
.btn-show {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
<p>Any grid-parent over 10 child items has a "SHOW MORE" button to expand</p>
<p>Click "SHOW MORE" to see the results</p>
</section>
<radio></radio>
<div class="wrapper">
<h3>5 child elements</h3>
<div class="grid-parent">
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="btn-expand">Show More</div>
</div>
<h3>8 child elements</h3>
<div class="grid-parent">
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="btn-expand">Show More</div>
</div>
<h3>10 child elements</h3>
<div class="grid-parent">
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="btn-expand">Show More</div>
</div>
<h3>13 child elements</h3>
<div class="grid-parent">
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="btn-expand">Show More</div>
</div>
<h3>16 child elements</h3>
<div class="grid-parent">
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="btn-expand">Show More</div>
</div>
<h3>19 child elements</h3>
<div class="grid-parent">
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="btn-expand">Show More</div>
</div>
</div>
答案 2 :(得分:1)
这是使用flexbox的实现;
* {
box-sizing: border-box;
}
ul {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 400px;
width: 300px;
overflow: hidden;
border: solid 1px red;
margin: 0;
padding: 0;
list-style: none;
}
ul li {
height: auto;
padding: 15px;
width: 100%;
border: solid 1px #000;
flex: 0 0 auto;
}
&#13;
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
<li>F</li>
<li>G<br />klajlka</li>
<li>H</li>
</ul>
&#13;
答案 3 :(得分:0)
我希望有一个CSS解决方案,尽管我已经实现了一个javascript实现。我在这里使用过jQuery,虽然没有必要。
$(function() {
$(window).resize(function() {
var max = $("#top-nav div.menu:last-child>ul>li:first-child").position().left;
$("#top-nav div.menu:first-child > ul").children("li").each(function() {
// We have to show first so we can get position/size
var $this = $(this).show();
if ($this.position().left + $this.width() > max) {
$this.nextAll().andSelf().hide();
return false;
}
});
});
$(window).resize();
});
通过$("#top-nav div.menu:first-child>ul>li:hidden");
可以在其他位置获取隐藏节点。我可以选择添加一个类,可以单独定位,例如通过提供一个按钮来显示隐藏的项目。
答案 4 :(得分:0)
这不是解决确切问题的答案,但标题与我(略)不同的问题匹配,我通过谷歌搜索该词到达了这里。
如果您知道哪个元素将换行并且不需要显示(在我的情况下为“聪明”间隔符),则可以将其高度设置为0。它将换行,但不会增加任何高度。 / p>