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

单页响应式 图片懒加载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>

相关文章:

  • 2025年- G23-Lc97-104.二叉树的最大深度-java版(递归调用左右子树)
  • 基于Python编程语言实现“机器学习”,用于车牌识别项目
  • 林阳域管理系统功能简介
  • Oracle 数据库安全评估(DBSAT)简明过程
  • Java 大视界 -- Java 大数据在智能医疗远程会诊与专家协作中的技术支持(146)
  • Python在数据科学中的应用:完整指南
  • 【高并发内存池】第三弹---构建Central Cache的全方位指南——从整体设计到核心实现
  • 《C++11 基于CAS无锁操作的atomic原子类型》
  • 头歌 JAVA 桥接模式实验
  • UI数据处理新隐私保护:确保用户新信息安全
  • 固定公网 IP
  • 【浙大PTA:L1系列题目】
  • NFS 安装与测试
  • 如何在SQL中高效使用聚合函数、日期函数和字符串函数:实用技巧与案例解析
  • 001 你好LabVIEW
  • 如何理解java中Stream流?
  • 对IKFOM论文中一些关键内容的理解
  • github如何为开源项目作出贡献
  • 高防ip和高防服务器的区别?
  • MSE分类时梯度消失的问题详解和交叉熵损失的梯度推导
  • 在本轮印巴冲突的舆论场上也胜印度一筹,巴基斯坦靠什么?
  • 不是10点!乌克兰官员称尚未就俄乌谈判开始时间达成一致
  • 缅甸内观冥想的历史漂流:从心理治疗室到“非语言现场”
  • 坚持吃素,是不是就不会得高血脂了?
  • 为什么越来越多景区,把C位留给了书店?
  • 中国科学院院士、我国航天液体火箭技术专家朱森元逝世