炫酷图片放大切换效果

<!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;
            background: #111;
            overflow-x: hidden;
        }
        
        .gallery-container {
            width: 100vw;
            height: 100vh;
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .gallery {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            grid-gap: 15px;
            width: 80%;
            max-width: 1200px;
        }
        
        .gallery-item {
            position: relative;
            width: 100%;
            height: 200px;
            overflow: hidden;
            border-radius: 8px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .gallery-item img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.5s ease;
        }
        
        .gallery-item:hover {
            transform: translateY(-10px);
            box-shadow: 0 15px 30px rgba(0, 0, 0, 0.5);
        }
        
        .gallery-item:hover img {
            transform: scale(1.1);
        }
        
        .gallery-item.active {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: 100;
            transform: none;
            border-radius: 0;
            animation: zoomIn 0.5s forwards;
        }
        
        .gallery-item.active img {
            object-fit: contain;
            background: #000;
        }
        
        .close-btn {
            position: fixed;
            top: 30px;
            right: 30px;
            color: white;
            font-size: 30px;
            z-index: 101;
            cursor: pointer;
            opacity: 0;
            transition: opacity 0.3s;
        }
        
        .gallery-item.active ~ .close-btn {
            opacity: 1;
        }
        
        .nav-btn {
            position: fixed;
            top: 50%;
            transform: translateY(-50%);
            color: white;
            font-size: 40px;
            z-index: 101;
            cursor: pointer;
            opacity: 0;
            transition: opacity 0.3s;
            background: rgba(0, 0, 0, 0.5);
            width: 60px;
            height: 60px;
            display: flex;
            justify-content: center;
            align-items: center;
            border-radius: 50%;
        }
        
        .prev-btn {
            left: 30px;
        }
        
        .next-btn {
            right: 30px;
        }
        
        .gallery-item.active ~ .nav-btn {
            opacity: 1;
        }
        
        @keyframes zoomIn {
            from {
                transform: scale(0.5);
                opacity: 0;
            }
            to {
                transform: scale(1);
                opacity: 1;
            }
        }
        
        .credit {
            position: fixed;
            bottom: 20px;
            left: 0;
            width: 100%;
            text-align: center;
            color: #666;
            font-size: 14px;
            z-index: 1;
        }
    </style>
</head>
<body>
    <div class="gallery-container">
        <div class="gallery">
            <div class="gallery-item">
                <img src="https://picsum.photos/800/600?random=1" alt="Image 1">
            </div>
            <div class="gallery-item">
                <img src="https://picsum.photos/800/600?random=2" alt="Image 2">
            </div>
            <div class="gallery-item">
                <img src="https://picsum.photos/800/600?random=3" alt="Image 3">
            </div>
            <div class="gallery-item">
                <img src="https://picsum.photos/800/600?random=4" alt="Image 4">
            </div>
            <div class="gallery-item">
                <img src="https://picsum.photos/800/600?random=5" alt="Image 5">
            </div>
            <div class="gallery-item">
                <img src="https://picsum.photos/800/600?random=6" alt="Image 6">
            </div>
            <div class="gallery-item">
                <img src="https://picsum.photos/800/600?random=7" alt="Image 7">
            </div>
            <div class="gallery-item">
                <img src="https://picsum.photos/800/600?random=8" alt="Image 8">
            </div>
            <div class="gallery-item">
                <img src="https://picsum.photos/800/600?random=9" alt="Image 9">
            </div>
        </div>
        
        <div class="close-btn">×</div>
        <div class="nav-btn prev-btn">‹</div>
        <div class="nav-btn next-btn">›</div>
        

    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            let currentIndex = 0;
            const items = $('.gallery-item');
            const totalItems = items.length;
            
            // 点击缩略图放大
            items.on('click', function() {
                currentIndex = $(this).index();
                $(this).addClass('active');
                $('body').css('overflow', 'hidden');
            });
            
            // 关闭大图
            $('.close-btn').on('click', function(e) {
                e.stopPropagation();
                items.removeClass('active');
                $('body').css('overflow', 'auto');
            });
            
            // 上一张
            $('.prev-btn').on('click', function(e) {
                e.stopPropagation();
                items.removeClass('active');
                currentIndex = (currentIndex - 1 + totalItems) % totalItems;
                items.eq(currentIndex).addClass('active');
            });
            
            // 下一张
            $('.next-btn').on('click', function(e) {
                e.stopPropagation();
                items.removeClass('active');
                currentIndex = (currentIndex + 1) % totalItems;
                items.eq(currentIndex).addClass('active');
            });
            
            // 键盘导航
            $(document).on('keydown', function(e) {
                if ($('.gallery-item.active').length) {
                    switch(e.keyCode) {
                        case 27: // ESC
                            $('.close-btn').click();
                            break;
                        case 37: // 左箭头
                            $('.prev-btn').click();
                            break;
                        case 39: // 右箭头
                            $('.next-btn').click();
                            break;
                    }
                }
            });
            
            // 点击大图区域关闭
            $(document).on('click', function(e) {
                if ($(e.target).hasClass('gallery-item active')) {
                    $('.close-btn').click();
                }
            });
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值