jQuery瀑布流布局

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery瀑布流布局</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f5f5f5;
            padding: 20px;
        }
        
        .container {
            width: 90%;
            max-width: 1200px;
            margin: 0 auto;
        }
        
        h1 {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
        }
        
        .waterfall {
            position: relative;
            margin: 0 auto;
        }
        
        .item {
            position: absolute;
            width: 230px;
            padding: 10px;
            background: #fff;
            border-radius: 5px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
            transition: all 0.3s ease;
        }
        
        .item:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }
        
        .item img {
            width: 100%;
            border-radius: 3px;
            margin-bottom: 10px;
        }
        
        .item h3 {
            font-size: 16px;
            margin-bottom: 8px;
            color: #333;
        }
        
        .item p {
            font-size: 14px;
            color: #666;
            line-height: 1.4;
        }
        
        .loading {
            text-align: center;
            padding: 20px;
            font-size: 16px;
            color: #999;
        }
        
        /* 插入的网址样式 */
        .website-link {
            display: block;
            text-align: center;
            margin: 30px 0;
            font-size: 14px;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>jQuery瀑布流布局演示</h1>
        
        <div class="waterfall"></div>
        
        <div class="loading">加载中...</div>
        

    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            // 模拟数据
            const mockData = [
                { title: "项目1", desc: "这是第一个项目的描述", img: "https://picsum.photos/300/200?random=1" },
                { title: "项目2", desc: "这是第二个项目的描述", img: "https://picsum.photos/300/250?random=2" },
                { title: "项目3", desc: "这是第三个项目的描述", img: "https://picsum.photos/300/180?random=3" },
                { title: "项目4", desc: "这是第四个项目的描述", img: "https://picsum.photos/300/220?random=4" },
                { title: "项目5", desc: "这是第五个项目的描述", img: "https://picsum.photos/300/300?random=5" },
                { title: "项目6", desc: "这是第六个项目的描述", img: "https://picsum.photos/300/240?random=6" },
                { title: "项目7", desc: "这是第七个项目的描述", img: "https://picsum.photos/300/260?random=7" },
                { title: "项目8", desc: "这是第八个项目的描述", img: "https://picsum.photos/300/190?random=8" },
                { title: "项目9", desc: "这是第九个项目的描述", img: "https://picsum.photos/300/280?random=9" },
                { title: "项目10", desc: "这是第十个项目的描述", img: "https://picsum.photos/300/230?random=10" }
            ];
            
            // 瀑布流布局函数
            function waterfallLayout() {
                const $waterfall = $('.waterfall');
                const itemWidth = 230; // 项目宽度
                const gap = 20; // 间距
                
                // 计算列数
                const containerWidth = $waterfall.width();
                const columnCount = Math.floor(containerWidth / (itemWidth + gap));
                const columnHeights = new Array(columnCount).fill(0);
                
                // 清空容器
                $waterfall.empty();
                
                // 遍历数据创建项目
                mockData.forEach((item, index) => {
                    // 创建项目元素
                    const $item = $(`
                        <div class="item">
                            <img src="${item.img}" alt="${item.title}">
                            <h3>${item.title}</h3>
                            <p>${item.desc}</p>
                        </div>
                    `);
                    
                    // 找到高度最小的列
                    let minHeight = Math.min(...columnHeights);
                    let minIndex = columnHeights.indexOf(minHeight);
                    
                    // 设置项目位置
                    const left = minIndex * (itemWidth + gap);
                    const top = columnHeights[minIndex] + gap;
                    
                    $item.css({
                        left: left + 'px',
                        top: top + 'px'
                    });
                    
                    // 添加到容器
                    $waterfall.append($item);
                    
                    // 更新列高度
                    columnHeights[minIndex] = top + $item.outerHeight();
                });
                
                // 设置容器高度
                $waterfall.height(Math.max(...columnHeights));
                
                // 隐藏加载提示
                $('.loading').hide();
            }
            
            // 初始化瀑布流
            waterfallLayout();
            
            // 窗口大小改变时重新布局
            $(window).resize(function() {
                waterfallLayout();
            });
            
            // 模拟加载更多数据
            $(window).scroll(function() {
                if ($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
                    $('.loading').show();
                    
                    // 模拟异步加载
                    setTimeout(function() {
                        // 添加更多模拟数据
                        for (let i = 11; i <= 15; i++) {
                            mockData.push({
                                title: `项目${i}`,
                                desc: `这是第${i}个项目的描述`,
                                img: `https://picsum.photos/300/${200 + Math.floor(Math.random() * 100)}?random=${i}`
                            });
                        }
                        
                        waterfallLayout();
                    }, 1000);
                }
            });
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值