<!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;
}
body {
font-family: 'Arial', sans-serif;
background-color: #f0f2f5;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
h1 {
text-align: center;
margin: 30px 0;
color: #333;
}
.waterfall {
column-count: 4;
column-gap: 15px;
}
.waterfall-item {
break-inside: avoid;
margin-bottom: 15px;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
animation: fadeIn 0.5s ease forwards;
opacity: 0;
background: white;
}
.waterfall-item:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
}
.waterfall-img {
width: 100%;
display: block;
transition: transform 0.5s ease;
}
.waterfall-item:hover .waterfall-img {
transform: scale(1.05);
}
.waterfall-content {
padding: 15px;
}
.waterfall-content h3 {
margin-bottom: 8px;
color: #333;
}
.waterfall-content p {
color: #666;
font-size: 14px;
line-height: 1.5;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@media (max-width: 992px) {
.waterfall {
column-count: 3;
}
}
@media (max-width: 768px) {
.waterfall {
column-count: 2;
}
}
@media (max-width: 480px) {
.waterfall {
column-count: 1;
}
}
.controls {
text-align: center;
margin: 30px 0;
}
.btn {
display: inline-block;
padding: 10px 20px;
background: #4a6fa5;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
margin: 0 10px;
transition: all 0.3s;
}
.btn:hover {
background: #3a5a8f;
transform: translateY(-2px);
}
.footer {
text-align: center;
margin-top: 50px;
padding: 20px;
color: #666;
}
.footer a {
color: #4a6fa5;
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h1>图片瀑布流动画效果</h1>
<div class="controls">
<button class="btn" id="add-btn">添加图片</button>
<button class="btn" id="shuffle-btn">随机排序</button>
</div>
<div class="waterfall" id="waterfall">
<!-- 图片将通过JavaScript动态添加 -->
</div>
<div class="footer">
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const waterfall = document.getElementById('waterfall');
const addBtn = document.getElementById('add-btn');
const shuffleBtn = document.getElementById('shuffle-btn');
// 图片数据
const imageData = [
{
url: 'https://source.unsplash.com/random/600x800?nature1',
title: '自然风光',
desc: '美丽的自然风景,让人心旷神怡'
},
{
url: 'https://source.unsplash.com/random/600x600?city1',
title: '城市建筑',
desc: '现代都市的繁华与建筑的魅力'
},
{
url: 'https://source.unsplash.com/random/600x900?animal1',
title: '野生动物',
desc: '大自然中的精灵,自由自在'
},
{
url: 'https://source.unsplash.com/random/600x700?food1',
title: '美食佳肴',
desc: '色香味俱全的美食诱惑'
},
{
url: 'https://source.unsplash.com/random/600x750?travel1',
title: '旅行摄影',
desc: '记录旅途中的美好瞬间'
},
{
url: 'https://source.unsplash.com/random/600x850?people1',
title: '人像摄影',
desc: '捕捉人物的情感与故事'
},
{
url: 'https://source.unsplash.com/random/600x650?art1',
title: '艺术创作',
desc: '创意与灵感的碰撞'
},
{
url: 'https://source.unsplash.com/random/600x950?technology1',
title: '科技创新',
desc: '改变世界的力量'
}
];
// 添加图片到瀑布流
function addImages() {
imageData.forEach((item, index) => {
setTimeout(() => {
const itemElement = document.createElement('div');
itemElement.className = 'waterfall-item';
itemElement.style.animationDelay = `${index * 0.1}s`;
itemElement.innerHTML = `
<img src="${item.url}" alt="${item.title}" class="waterfall-img">
<div class="waterfall-content">
<h3>${item.title}</h3>
<p>${item.desc}</p>
</div>
`;
waterfall.appendChild(itemElement);
}, index * 100);
});
}
// 随机排序图片
function shuffleImages() {
const items = Array.from(waterfall.children);
items.sort(() => Math.random() - 0.5);
// 清空容器
while (waterfall.firstChild) {
waterfall.removeChild(waterfall.firstChild);
}
// 重新添加排序后的元素
items.forEach((item, index) => {
item.style.animation = 'none';
void item.offsetWidth; // 触发重绘
item.style.animation = `fadeIn 0.5s ease ${index * 0.1}s forwards`;
waterfall.appendChild(item);
});
}
// 添加随机图片
function addRandomImage() {
const randomIndex = Math.floor(Math.random() * imageData.length);
const item = imageData[randomIndex];
const itemElement = document.createElement('div');
itemElement.className = 'waterfall-item';
itemElement.style.animation = 'fadeIn 0.5s ease forwards';
itemElement.innerHTML = `
<img src="${item.url}?${Date.now()}" alt="${item.title}" class="waterfall-img">
<div class="waterfall-content">
<h3>${item.title}</h3>
<p>${item.desc}</p>
</div>
`;
// 随机插入位置
const items = waterfall.children;
const randomPosition = Math.floor(Math.random() * (items.length + 1));
if (items.length === 0 || randomPosition >= items.length) {
waterfall.appendChild(itemElement);
} else {
waterfall.insertBefore(itemElement, items[randomPosition]);
}
}
// 事件监听
addBtn.addEventListener('click', addRandomImage);
shuffleBtn.addEventListener('click', shuffleImages);
// 初始化加载图片
addImages();
// 滚动加载更多
window.addEventListener('scroll', function() {
if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 500) {
addRandomImage();
}
});
});
</script>
</body>
</html>
318

被折叠的 条评论
为什么被折叠?



