简体   繁体   English

CSS 显示网格中的响应式全宽 div 插入

[英]Responsive full width div insert in CSS Display Grid

.grid-content {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    grid-auto-rows: auto;
  }
  .grid-content > div > div > div {
    background: black;
    display: grid;
  }
  
  .grid-content > div > div > div::before {
    content: "";
    display: block;
    padding-bottom: 100%;
    grid-area: 1 / 1 / 2 / 2;
  }

the grid has the div class grid-content and the items in it have a div class clickable_item网格具有 div 类grid-content并且其中的项目具有 div 类clickable_item

Clicking on any item on the grid will move this div block to below that selected item.单击网格上的任何项目都会将此 div 块移动到所选项目的下方。 This div occupies the full width of the row and needs to adjust its grid-column: 1 / span n property to stay responsive to the number of row columns in the grid.这个 div 占据了行的全部宽度,需要调整它的grid-column: 1 / span n属性以保持对网格中行列数的响应。

Currently the grid-column 1 / span 8 property is fixed and doesnt dynamically adjust to changing columns in the wrapping grid.目前grid-column 1 / span 8属性是固定的,不会动态调整以改变环绕网格中的列。

Items should have a unique id and also must show or hide a full width div below its row when it is clicked项目应该有一个唯一的 id,并且还必须在单击时在其行下方显示或隐藏全宽 div

https://codepen.io/ridhwaans/pen/jOrpeNW https://codepen.io/ridhwaans/pen/jOrpeNW

update: using更新:使用

gridComputedStyle.getPropertyValue("grid-template-columns").replace(' 0px', '').split(' ').length;

from a resize listener to update style property.从调整大小侦听器更新样式属性。
The full width div messes with other items during wrapping in the codepen demo.在 codepen 演示中包装期间,全宽 div 会与其他项目混淆。 How do I move the div every time to span the columns in grid?如何每次移动 div 以跨越网格中的列?

I believe this is what you want:我相信这就是你想要的:

https://codepen.io/riza-khan/pen/VwjBqVd https://codepen.io/riza-khan/pen/VwjBqVd

Basically, use the function insertAdjacetElement which will allow you to insert an element in the position of your choosing.基本上,使用函数insertAdjacetElement它将允许您在您选择的位置插入一个元素。

And also note, you need to traverse the DOM in this case so:还要注意,在这种情况下,您需要遍历 DOM,因此:

e.target.parentElement.parentElement.insertAdjacentElement('afterend', some_node)

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

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