- index.html
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<link rel="shortcut icon" href="favicon.ico" />
<head>
<style>
body {
font-family: Arial, sans-serif;
}
#gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 15px;
margin-top: 20px;
}
.thumbnail {
width: 100%;
height: 200px;
object-fit: cover;
border-radius: 8px;
transition: transform 0.2s;
cursor: pointer;
}
.thumbnail:hover {
transform: scale(1.05);
}
#loader {
text-align: center;
margin-top: 20px;
}
</style>
</head>
<body>
<div id="gallery"></div>
<div id="loader">...</div>
<script src="script.js"></script>
</body>
</html>
2. script.js
const gallery = document.getElementById('gallery');
const loader = document.getElementById('loader');
let page = 1;
const limit = 10; // 每次加载的图片数量
let imagesList = []; // 存储加载的图片路径
let currentIndex = 0; // 当前显示的图片索引
// 懒加载图片
async function loadImages() {
try {
const response = await fetch(`/api/images?page=${page}&limit=${limit}`);
const images = await response.json();
if (images.length === 0) {
loader.textContent = '没有更多图片了';
return;
}
// 将新加载的图片路径加入列表
images.forEach((image) => imagesList.push(image.path));
images.forEach((image, index) => {
const img = document.createElement('img');
img.src = image.path; // 图片路径
img.alt = image.name;
img.className = 'thumbnail';
// 点击图片查看大图
img.addEventListener('click', () => {
const actualIndex = imagesList.indexOf(image.path); // 实时获取索引
openModal(actualIndex);
});
gallery.appendChild(img);
});
page++;
} catch (error) {
console.error('加载图片失败', error);
loader.textContent = '加载失败,请重试';
}
}
// 打开模态框
function openModal(index) {
currentIndex = index;
// 检查图片列表和索引有效性
if (!imagesList.length || index < 0 || index >= imagesList.length) {
console.error('图片索引无效或图片列表为空');
return;
}
let modal = document.getElementById('modal');
if (!modal) {
modal = document.createElement('div');
modal.id = 'modal';
modal.style.cssText = `
position: fixed;
top: 0; left: 0;
width: 100%; height: 100%;
background: rgba(0, 0, 0, 0.9);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
`;
modal.addEventListener('click', (e) => {
if (e.target.id === 'modal') {
document.body.removeChild(modal);
}
});
document.addEventListener('keydown', handleKeyDown);
modal.addEventListener('remove', () => {
document.removeEventListener('keydown', handleKeyDown);
});
document.body.appendChild(modal);
}
modal.innerHTML = '';
const modalImg = document.createElement('img');
modalImg.src = imagesList[currentIndex];
modalImg.style.maxWidth = '90%';
modalImg.style.maxHeight = '90%';
modalImg.id = 'modal-img';
const prevButton = createButton('←', () => changeImage(-1));
const nextButton = createButton('→', () => changeImage(1));
modal.appendChild(prevButton);
modal.appendChild(modalImg);
modal.appendChild(nextButton);
}
// 创建按钮
function createButton(text, onClick) {
const button = document.createElement('button');
button.textContent = text;
button.style.cssText = `
position: absolute;
top: 50%;
transform: translateY(-50%);
background: none;
border: none;
font-size: 2rem;
color: white;
cursor: pointer;
user-select: none;
z-index: 10;
`;
button.style.left = text === '←' ? '10%' : 'auto';
button.style.right = text === '→' ? '10%' : 'auto';
button.addEventListener('click', (e) => {
e.stopPropagation(); // 防止冒泡触发关闭
onClick();
});
return button;
}
// 切换图片
function changeImage(direction) {
if (!imagesList.length) return;
currentIndex = (currentIndex + direction + imagesList.length) % imagesList.length;
const modalImg = document.getElementById('modal-img');
if (modalImg) {
modalImg.src = imagesList[currentIndex];
}
}
// 处理键盘事件
function handleKeyDown(event) {
if (event.key === 'ArrowLeft') {
changeImage(-1);
} else if (event.key === 'ArrowRight') {
changeImage(1);
} else if (event.key === 'Escape') {
const modal = document.getElementById('modal');
if (modal) {
document.body.removeChild(modal);
}
}
}
// 使用 Intersection Observer 实现懒加载
const observer = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) {
loadImages();
}
},
{
root: null,
threshold: 0.1,
}
);
// 监听加载器
observer.observe(loader);