用网络上的二次元随机图API写一个瀑布流相册 使用grid布局 meowrain 收录于 前端2024-09-22 约 400 字 预计阅读 2 分钟 目录 效果 html css 注意 本文最后更新于 2024-09-22,文中内容可能已过时。 效果 html 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 <!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 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 * { 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; }