代码展示
HTML
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>瀑布流布局</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
width: 100%;
padding: 20px;
background-color: #f5f5f5;
}
.waterfall {
position: relative;
width: 100%;
}
.item {
position: absolute;
width: calc(25% - 20px);
margin: 10px;
background: #fff;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
}
.item:hover {
transform: translateY(-5px);
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}
.item img {
width: 100%;
display: block;
}
.item .content {
padding: 10px;
}
.item .title {
font-size: 16px;
margin-bottom: 5px;
}
.item .desc {
font-size: 14px;
color: #666;
}
@media screen and (max-width: 1200px) {
.item {
width: calc(33.33% - 20px);
}
}
@media screen and (max-width: 768px) {
.item {
width: calc(50% - 20px);
}
}
@media screen and (max-width: 480px) {
.item {
width: calc(100% - 20px);
}
}
</style>
</head>
<body>
<div class="container">
<div class="waterfall" id="waterfall"></div>
</div>
<script>
class Waterfall {
constructor(container) {
this.container = container;
this.items = [];
this.columns = 4;
this.gap = 20;
this.init();
}
init() {
this.createItems();
this.layout();
window.addEventListener('resize', () => this.layout());
}
createItems() {
// 模拟数据
const data = [
{ title: '图片1', desc: '描述文本1', height: 200 },
{ title: '图片2', desc: '描述文本2', height: 300 },
{ title: '图片3', desc: '描述文本3', height: 250 },
{ title: '图片4', desc: '描述文本4', height: 350 },
{ title: '图片5', desc: '描述文本5', height: 280 },
{ title: '图片6', desc: '描述文本6', height: 320 },
{ title: '图片7', desc: '描述文本7', height: 240 },
{ title: '图片8', desc: '描述文本8', height: 260 },
{ title: '图片9', desc: '描述文本9', height: 290 },
{ title: '图片10', desc: '描述文本10', height: 310 },
];
data.forEach(item => {
const div = document.createElement('div');
div.className = 'item';
div.innerHTML = `
<img src="https://picsum.photos/400/${item.height}" alt="${item.title}">
<div class="content">
<div class="title">${item.title}</div>
<div class="desc">${item.desc}</div>
</div>
`;
this.container.appendChild(div);
this.items.push(div);
});
}
layout() {
const containerWidth = this.container.clientWidth;
const itemWidth = (containerWidth - (this.columns - 1) * this.gap) / this.columns;
const columnHeights = new Array(this.columns).fill(0);
const columnItems = new Array(this.columns).fill().map(() => []);
this.items.forEach(item => {
// 找到最短的列
const minHeight = Math.min(...columnHeights);
const columnIndex = columnHeights.indexOf(minHeight);
// 设置元素位置
item.style.width = `${itemWidth}px`;
item.style.left = `${columnIndex * (itemWidth + this.gap)}px`;
item.style.top = `${columnHeights[columnIndex]}px`;
// 更新列高度
columnHeights[columnIndex] += item.offsetHeight + this.gap;
columnItems[columnIndex].push(item);
});
// 设置容器高度
this.container.style.height = `${Math.max(...columnHeights)}px`;
}
}
// 初始化瀑布流
const waterfall = new Waterfall(document.getElementById('waterfall'));
</script>
</body>
</html>
效果展示
屏幕截图 2025-05-06 180542.png