我正在一个实施砖石画廊的项目中工作。它工作得很好,但我需要在 JS 构造调用中动态调整列的数量......我尝试了不同的方法来调整大小事件、while 循环、条件 if ,甚至从 CSS 使用 mediaQueries 破解列号,一无所有,递归调用和无限循环,我还找不到解决方案......
const masonryLayout = (containerElem, itemsElems, columns) => {
// Crea Container para las columnas
containerElem.classList.add('masonry-layout', `columns-${columns}`)
// Crea n-columnas de acuerdo a "columns"
let columnsElements = []
for (let i = 1; i <= columns; i++) {
let column = document.createElement('div')
column.classList.add('masonry-column', `column-${i}`)
containerElem.appendChild(column)
columnsElements.push(column)
}
// Ubica cada imagen en la columna correspondiente
for (let m = 0; m < Math.ceil(itemsElems.length / columns); m++) {
for (let n = 0; n < columns; n++) {
let item = itemsElems[m * columns + n]
columnsElements[n].appendChild(item)
item.classList.add('masonry-item')
}
}
}
masonryLayout(document.getElementById("gallery"), document.querySelectorAll(".gallery-item"),5)
.gallery-item img {
max-width: 100%;
display: block;
}
.masonry-layout {
--columns: 5;
--gap: 0.6rem;
display: grid;
grid-template-columns: repeat(var(--columns), 1fr);
grid-gap: var(--gap);
}
.masonry-layout .masonry-item {
margin-bottom: var(--gap);
}
.masonry-layout.columns-1 {
--columns: 1;
}
.masonry-layout.columns-2 {
--columns: 2;
}
.masonry-layout.columns-3 {
--columns: 3;
}
.masonry-layout.columns-4 {
--columns: 4;
}
.masonry-layout.columns-5 {
--columns: 5;
}
<div class="gallery" id="gallery">
<div class="columnNbrClass"></div>
<div class="gallery-item">
<img src="https://picsum.photos/450/325?image=100" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/450?image=200" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/280?image=300" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/540?image=400" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/380?image=500" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/300?image=600" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/400?image=700" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/300?image=800" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/280?image=900" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/480?image=925" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/550?image=950" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/600?image=1000" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/325?image=25" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/450?image=50" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/280?image=75" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/540?image=100" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/380?image=125" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/300?image=161" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/400?image=175" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/300?image=200" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/280?image=225" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/480?image=250" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/550?image=275" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/600?image=300" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/325?image=13" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/450?image=26" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/280?image=39" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/540?image=52" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/380?image=65" alt="" class="">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/450/300?image=78" alt="" class="">
</div>
</div>
基本上,我需要 masonryLayout 构造函数中的列号在调整大小时动态更改,但我找不到方法,我什至尝试保存原始 html 文档但没有任何运气......如果有人有任何想法,它会很高兴听到和学习。
非常感谢您的交流!