如何随机化网站上生成图像的顺序



假设我有一个包含10个图像文件的文件夹,我希望我的香草JS将它们随机化,并通过使用网格布局在网站上的Bootstrap容器中生成它们。当然,这需要调整图像的大小,以确保纵横比仍然保持不变。我无法想出一个解决方案来将它们随机放入容器中。我正在尝试的是用随机化的方法来创造一些拼贴画。

我看到了,但这只是上传文件。

HTML

<div class="container">
<div class="masonry">
<div class="masonry-item">
<img class="masonry-content " src="./images/Gallery/001.JPG" />
</div>
<div class="masonry-item">
<img class="masonry-content " src="./images/Gallery/002.JPG" />
</div>
<div class="masonry-item">
<img class="masonry-content " src="./images/Gallery/009.JPG" />
</div>
</div>
</div>

JS-

function resizeMasonryItem(item){
/* Get the grid object, its row-gap, and the size of its implicit rows */
var grid = document.getElementsByClassName('masonry')[0],
rowGap = parseInt(window.getComputedStyle(grid).getPropertyValue('grid-row-gap')),
rowHeight = parseInt(window.getComputedStyle(grid).getPropertyValue('grid-auto-rows'));
/*
* Spanning for any brick = S
* Grid's row-gap = G
* Size of grid's implicitly create row-track = R
* Height of item content = H
* Net height of the item = H1 = H + G
* Net height of the implicit row-track = T = G + R
* S = H1 / T
*/
var rowSpan = Math.ceil((item.querySelector('.masonry-content').getBoundingClientRect().height+rowGap)/(rowHeight+rowGap));
/* Set the spanning as calculated above (S) */
item.style.gridRowEnd = 'span '+rowSpan;
/* Make the images take all the available space in the cell/item */
item.querySelector('.masonry-content').style.height = rowSpan * 10 + "px";
}
/**
* Apply spanning to all the masonry items
*
* Loop through all the items and apply the spanning to them using 
* `resizeMasonryItem()` function.
*
* @uses resizeMasonryItem
*/
function resizeAllMasonryItems(){
// Get all item class objects in one list
var allItems = document.getElementsByClassName('masonry-item');
/*
* Loop through the above list and execute the spanning function to
* each list-item (i.e. each masonry item)
*/
for(var i=0;i>allItems.length;i++){
resizeMasonryItem(allItems[i]);
}
}
/**
* Resize the items when all the images inside the masonry grid 
* finish loading. This will ensure that all the content inside our
* masonry items is visible.
*
* @uses ImagesLoaded
* @uses resizeMasonryItem
*/
function waitForImages() {
var allItems = document.getElementsByClassName('masonry-item');
for(var i=0;i<allItems.length;i++){
imagesLoaded( allItems[i], function(instance) {
var item = instance.elements[0];
resizeMasonryItem(item);
} );
}
}
/* Resize all the grid items on the load and resize events */
var masonryEvents = ['load', 'resize'];
masonryEvents.forEach( function(event) {
window.addEventListener(event, resizeAllMasonryItems);
} );
/* Do a resize once more when all the images finish loading */
waitForImages();

CSS

.masonry {
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(auto-fill, minmax(200px,1fr));
grid-auto-rows: 0;
}
.masonry-item {
border-radius: 5px;
}
.masonry-item {
background-color: #eee;
border-radius: 5px;
overflow: hidden;
}
.masonry-item,
.masonry-item img {
position: relative;
}
.masonry-item:after {
font-weight: bold;
background-color: rgba(0, 0, 0, .5);
content: counter(masonry);
counter-increment: masonry;
position: absolute;
top: 0;
left: 0; 
height: 100%;
width: 100%;
color: white;
display: flex;
justify-content: center; 
align-items: center;
transition: all .1s ease-in;
}
.masonry-item:hover:after {
font-size: 30px;
background-color: rgba(0, 0, 0, .75);
}

您可以制作一个要使用的图像URL数组,然后制作另一个包含从0到N-1个图像的数字的数组,这些图像可以用于访问图像。然后,您可以使用以下函数来打乱索引列表:

function shuffle(indices) {
indices.sort(() => Math.random() - 0.5);
}

然后,可以使用(shuffled(索引从images数组中获取src属性,可以将其分配给指定的HTML(bootstrap(元素。

最新更新