效果
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="header">MeowRain's Images</div>
<div class="content">
</div>
<div class="footer">CopyRight MeowRain 2024</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const content = document.querySelector('.content');
let page = 1; // 当前页面
const perPage = 9; // 每页加载的图片数量
function loadImages() {
for (let i = 0; i < perPage; i++) {
const img = document.createElement('img');
img.src = `https://www.dmoe.cc/random.php?t=${Date.now()}-${i + page * perPage}`;
img.alt = 'Image';
content.appendChild(img);
}
}
function onScroll() {
const { scrollTop, clientHeight, scrollHeight } = document.documentElement;
if (scrollTop + clientHeight >= scrollHeight - 5) {
page++;
loadImages();
}
}
// 初始化加载图片
loadImages();
// 监听滚动事件
content.addEventListener('scroll', onScroll);
});
</script>
</body>
</html>
css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f4f4f4;
overflow: hidden;
}
.container {
width: 100%;
display: flex;
flex-direction: column;
height: 100%;
}
.header {
width: 100%;
text-align: center;
padding: 20px;
font-size: 24px;
background-color: #333;
color: white;
flex-shrink: 0;
}
.content {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-flow: row dense;
/* column-count: 4; */
grid-gap: 10px;
padding: 20px;
overflow-y: auto;
}
.content img {
width: 100%;
height: auto;
display: block;
border-radius: 8px;
}
.footer {
text-align: center;
padding: 20px;
background-color: #333;
color: white;
flex-shrink: 0;
}