当前位置: 首页 > article >正文

单页响应式 图片懒加载HTML页面

设计说明

  1. 响应式设计

    • 使用CSS Grid布局,根据屏幕宽度自动调整色块数量

    • 在不同设备上都有良好的显示效果

  2. 懒加载

    • 使用<img>标签的loading="lazy"属性实现原生懒加载

    • 图片在滚动到视口附近时才会加载

  3. 色块展示

    • 使用随机生成的色块作为内容展示

    • 每个色块都有独特的颜色和编号

    • 色块有悬停效果和阴影效果

  4. 分类展示

    • 将色块分为自然风光、城市建筑和抽象艺术三类

    • 每类都有独立的标题和网格布局

<!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;
            line-height: 1.6;
            background-color: #f4f4f4;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .section {
            margin-bottom: 40px;
        }
        
        .section-title {
            text-align: center;
            margin-bottom: 20px;
            color: #333;
            font-size: 24px;
        }
        
        .color-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 40px;
        }
        
        .color-block {
            aspect-ratio: 1;
            border-radius: 8px;
            overflow: hidden;
            position: relative;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease;
        }
        
        .color-block:hover {
            transform: translateY(-5px);
        }
        
        .color-block img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            display: block;
        }
        
        .color-block-placeholder {
            width: 100%;
            height: 100%;
            background-color: #ddd;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #999;
            font-size: 14px;
        }
        
        @media (max-width: 768px) {
            .color-grid {
                grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
            }
        }
        
        @media (max-width: 480px) {
            .color-grid {
                grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="section-title">响应式懒加载色块展示</h1>
        
        <div class="section">
            <h2 class="section-title">自然风光</h2>
            <div class="color-grid" id="nature-grid">
                <!-- 色块将通过JavaScript动态添加 -->
            </div>
        </div>
        
        <div class="section">
            <h2 class="section-title">城市建筑</h2>
            <div class="color-grid" id="architecture-grid">
                <!-- 色块将通过JavaScript动态添加 -->
            </div>
        </div>
        
        <div class="section">
            <h2 class="section-title">抽象艺术</h2>
            <div class="color-grid" id="art-grid">
                <!-- 色块将通过JavaScript动态添加 -->
            </div>
        </div>
    </div>

    <script>
        // 模拟图片数据
        const imageDatas = [
            { id: 1, color: '#FF5733', category: 'nature' },
            { id: 2, color: '#33FF57', category: 'nature' },
            { id: 3, color: '#3357FF', category: 'nature' },
            { id: 4, color: '#F3FF33', category: 'nature' },
            { id: 5, color: '#FF33F3', category: 'nature' },
            { id: 6, color: '#33FFF3', category: 'nature' },
            { id: 7, color: '#FF8C33', category: 'nature' },
            { id: 8, color: '#33FF8C', category: 'nature' },
            { id: 9, color: '#338CFF', category: 'nature' },
            { id: 10, color: '#8CFF33', category: 'nature' },
            { id: 11, color: '#FF338C', category: 'architecture' },
            { id: 12, color: '#338CFF', category: 'architecture' },
            { id: 13, color: '#8C33FF', category: 'architecture' },
            { id: 14, color: '#FF8C33', category: 'architecture' },
            { id: 15, color: '#33FF8C', category: 'architecture' },
            { id: 16, color: '#8CFF33', category: 'architecture' },
            { id: 17, color: '#33FF33', category: 'art' },
            { id: 18, color: '#FF3333', category: 'art' },
            { id: 19, color: '#3333FF', category: 'art' },
            { id: 20, color: '#FFFF33', category: 'art' },
            { id: 21, color: '#FF33FF', category: 'art' },
            { id: 22, color: '#33FFFF', category: 'art' },
            { id: 23, color: '#8C33FF', category: 'art' },
            { id: 24, color: '#FF8C33', category: 'art' },
            { id: 25, color: '#338CFF', category: 'art' },
            { id: 26, color: '#8CFF33', category: 'art' }
        ];

        // 创建色块元素
        function createColorBlock(item) {
            const block = document.createElement('div');
            block.className = 'color-block';
            block.style.backgroundColor = item.color;
            
            const img = document.createElement('img');
            img.src = `https://picsum.photos/seed/${item.id}/300/300`;
            img.alt = `色块 ${item.id}`;
            img.loading = 'lazy'; // 启用懒加载
            
            block.appendChild(img);
            
            return block;
        }

        // 初始化页面
        function initPage() {
            const grids = {
                'nature-grid': document.getElementById('nature-grid'),
                'architecture-grid': document.getElementById('architecture-grid'),
                'art-grid': document.getElementById('art-grid')
            };

            // 根据分类将色块添加到对应的网格中
            imageDatas.forEach(item => {
                const gridId = `${item.category}-grid`;
                const grid = grids[gridId];
                if (grid) {
                    const block = createColorBlock(item);
                    grid.appendChild(block);
                }
            });
        }

        // 页面加载完成后初始化
        window.addEventListener('load', initPage);
    </script>
</body>
</html>


http://www.kler.cn/a/596013.html

相关文章:

  • 【资料分享】全志科技T113-i全国产(1.2GHz双核A7 RISC-V)工业核心板规格书
  • 【力扣/代码随想录】数组
  • 国产AI编程工具,助力3D“微”引擎开发!——从一场直播到工业科技需求的革新实践
  • idea 编译打包nacos2.0.3源码,生成可执行jar 包常见问题
  • W80x使用WM IoT SDK 2.X 开发(二)驱动tft屏幕
  • 自定义对象处理请求参数
  • MySQL 性能优化方向
  • vpc网络之间的关系
  • react学习1.搭建react环境
  • 常用的git和linux命令有哪些?
  • Linux开机、重启与用户登录注销全解析
  • STM32学习-Day5-中断
  • OpenCV vs MediaPipe:哪种方案更适合实时手势识别?
  • Vue3 在组件中判断事件是否注册
  • Linux 系统运行 Android 应用的几种方案
  • 【小派项目书】sprintboot + vue 语言实现
  • Jenkins Pipeline
  • Hugo 生成静态网站并部署到 GitHub Pages 的完整流程
  • 基于32单片机的无人机直流电机闭环调速系统设计
  • M-LAG 技术全面解析