简体   繁体   English

使用 CSS 网格隐藏仅部分适合容器的项目

[英]Hide items that only partially fit in the container using CSS grid

I have a row of items in a container that varies in size depending on the screensize.我在一个容器中有一行项目,这些项目的大小取决于屏幕大小。 There will always be items left outside the grid and the last one is usually only half way.总会有项目留在网格之外,最后一个通常只有一半。

What I need to happen is to completely hide any item that is partially shown.我需要做的是完全隐藏任何部分显示的项目。 Unfortunately, I do not have a fixed number of items for certain screen-sizes, it is a case in which I need to display however many I can fit in my container.不幸的是,对于某些屏幕尺寸,我没有固定数量的项目,在这种情况下,我需要在我的容器中显示任意数量的项目。 How can this be done?如何才能做到这一点?

Here is a sample of what my current code looks like: Notice how the last item only fits partially in the screen.这是我当前代码的示例: 注意最后一项如何仅部分适合屏幕。 I would like to hide it if it can't completely fit in my container.如果它不能完全适合我的容器,我想隐藏它。

How can this be done?如何才能做到这一点?

 const Item = () => { return ( <div className="item"/> ); }; const App = () => { return ( <div> <div className="container"> {Array.from({ length: 20 }).map((_, index) => ( <Item key={index} /> ))} </div> <p> Objective: all items need to be within the container. See how the last one only fits half way. I would like it to be completely hidden. </p> </div> ); } ReactDOM.render( <App />, document.getElementById('app') );
 .item { width: 100px; height: 100px; background-color: red; border: 3px solid blue; }.container { padding: 10px; width: 60%; overflow: hidden; border: 3px solid black; /* CSS grid related properties */ display: grid; grid-template-columns: repeat(auto-fill, 120px); grid-auto-rows: 120; grid-auto-flow: column; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.0/umd/react-dom.production.min.js"></script> <div id="app"></div>

Edit: There is another complication in this mix, which is that I should all hidden items with a button that scroll over to the hidden items whenever pressed.编辑:这个组合还有另一个复杂之处,那就是我应该使用一个按钮来隐藏所有隐藏的项目,只要按下该按钮就可以滚动到隐藏的项目。 So basically I do still need all items hidden off to the side and not below the visible row.所以基本上我仍然需要将所有项目隐藏在一边,而不是在可见行的下方。

One (slightly laborious) method would be to attach an after pseudo element to the right hand side of the container which covers the partially-included item.一种(稍微费力的)方法是将后伪元素附加到容器的右侧,以覆盖部分包含的项目。

Here's a simplified JS version.这是一个简化的 JS 版本。 It runs through the items until it gets to the first that is not fully contained in the container, calculates how much is therefore left in the container and sets the width of the pseudo element to that.它遍历项目,直到它到达第一个未完全包含在容器中的项目,然后计算容器中剩余的数量并将伪元素的宽度设置为该值。

This way the remaining items are not affected in any way, they are still 'there' but it does mean that as soon as there is any horizontal scrolling or any resizing of the viewport the calculation has to be done again.这样,其余项目不会受到任何影响,它们仍然“存在”,但这确实意味着只要有任何水平滚动或任何视口大小调整,就必须再次进行计算。

 function hide() { const container = document.querySelector('.container'); const items = container.querySelectorAll('.item'); const containerW = window.getComputedStyle(container).width.replace('px', ''); for ( let i = 0; i < items.length; i++) { const item = items[i]; const w = Number(window.getComputedStyle(item).width.replace('px','')); const rhs = (w + item.offsetLeft); if (rhs > containerW) { container.style.setProperty('--w', w - (rhs - containerW) + 'px'); break; } } } hide(); window.onresize = hide;
 .container { padding: 10px; width: 60%; overflow: hidden; border: 3px solid black; display: grid; grid-template-columns: repeat(auto-fill, 120px); grid-auto-rows: 120; grid-auto-flow: column; position: relative; --w: 0; box-sizing: border-box; }.container::after { content: ''; width: var(--w); background: white; height: 100%; right: 0; top: 0; display: inline-block; z-index: 1; position: absolute; }.item { width: 100px; height: 100px; background-color: red; border: 3px solid blue; box-sizing: border-box; }
 <div class="container"><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div></div>

Note the container and items have been given box-siainv: border-box just to make the calculation easier.注意容器和项目已经给出 box-siainv:border-box 只是为了使计算更容易。 If this is removed then the borders and margin settings need to be accounted for.如果将其删除,则需要考虑边框和边距设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM