CSS3鼠标拖拽图片展示特效

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS3鼠标拖拽图片展示特效</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Arial', sans-serif;
            background: linear-gradient(135deg, #1a1a2e, #16213e);
            color: #fff;
            min-height: 100vh;
            overflow-x: hidden;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
            padding: 0 20px;
        }

        h1 {
            font-size: 2.8rem;
            margin-bottom: 20px;
            background: linear-gradient(90deg, #00dbde, #fc00ff);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
            text-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
        }

        .gallery-container {
            position: relative;
            width: 90vw;
            max-width: 1200px;
            height: 70vh;
            margin: 0 auto;
            perspective: 1000px;
        }

        .gallery {
            position: absolute;
            width: 100%;
            height: 100%;
            transform-style: preserve-3d;
            cursor: grab;
            user-select: none;
            transition: transform 0.1s;
        }

        .gallery:active {
            cursor: grabbing;
        }

        .gallery-item {
            position: absolute;
            width: 300px;
            height: 400px;
            left: 50%;
            top: 50%;
            transform-origin: center;
            border-radius: 15px;
            overflow: hidden;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.4);
            transition: all 0.5s cubic-bezier(0.25, 0.1, 0.25, 1);
        }

        .gallery-item img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.5s;
        }

        .gallery-item:hover img {
            transform: scale(1.1);
        }

        .gallery-item::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(to top, rgba(0, 0, 0, 0.7) 0%, transparent 50%);
            opacity: 0;
            transition: opacity 0.3s;
        }

        .gallery-item:hover::before {
            opacity: 1;
        }

        .gallery-info {
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            padding: 20px;
            transform: translateY(100%);
            transition: transform 0.3s;
        }

        .gallery-item:hover .gallery-info {
            transform: translateY(0);
        }

        .gallery-info h3 {
            font-size: 1.5rem;
            margin-bottom: 8px;
        }

        .gallery-info p {
            font-size: 0.9rem;
            opacity: 0.8;
        }

        .special-link {
            display: inline-block;
            margin-top: 40px;
            padding: 12px 30px;
            background: linear-gradient(45deg, #00dbde, #fc00ff);
            color: white;
            text-decoration: none;
            border-radius: 30px;
            font-weight: bold;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(252, 0, 255, 0.3);
            text-transform: uppercase;
            letter-spacing: 1px;
        }

        .special-link:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 25px rgba(252, 0, 255, 0.5);
        }

        .instructions {
            margin-top: 30px;
            text-align: center;
            opacity: 0.7;
            font-size: 0.9rem;
        }

        @media (max-width: 768px) {
            h1 {
                font-size: 2rem;
            }
            
            .gallery-item {
                width: 250px;
                height: 350px;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>3D拖拽图片画廊</h1>
    </div>

    <div class="gallery-container">
        <div class="gallery" id="gallery">
            <div class="gallery-item">
                <img src="https://source.unsplash.com/random/600x800?mountain" alt="山脉">
                <div class="gallery-info">
                    <h3>壮丽山脉</h3>
                    <p>探索世界最高峰的美景</p>
                </div>
            </div>
            <div class="gallery-item">
                <img src="https://source.unsplash.com/random/600x800?ocean" alt="海洋">
                <div class="gallery-info">
                    <h3>蔚蓝海洋</h3>
                    <p>深海的神秘与美丽</p>
                </div>
            </div>
            <div class="gallery-item">
                <img src="https://source.unsplash.com/random/600x800?forest" alt="森林">
                <div class="gallery-info">
                    <h3>神秘森林</h3>
                    <p>地球上最后的原始森林</p>
                </div>
            </div>
            <div class="gallery-item">
                <img src="https://source.unsplash.com/random/600x800?desert" alt="沙漠">
                <div class="gallery-info">
                    <h3>金色沙漠</h3>
                    <p>无边沙海中的奇迹</p>
                </div>
            </div>
            <div class="gallery-item">
                <img src="https://source.unsplash.com/random/600x800?cityscape" alt="城市">
                <div class="gallery-info">
                    <h3>现代都市</h3>
                    <p>人类文明的璀璨明珠</p>
                </div>
            </div>
            <div class="gallery-item">
                <img src="https://source.unsplash.com/random/600x800?wildlife" alt="野生动物">
                <div class="gallery-info">
                    <h3>野生动物</h3>
                    <p>大自然最珍贵的礼物</p>
                </div>
            </div>
        </div>
    </div>

    <p class="instructions">使用鼠标拖拽可以旋转查看所有图片</p>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const gallery = document.getElementById('gallery');
            const items = document.querySelectorAll('.gallery-item');
            const itemCount = items.length;
            const radius = Math.min(500, window.innerWidth / 2);
            let angle = 0;
            let isDragging = false;
            let startX = 0;
            let currentAngle = 0;
            
            // 初始化3D布局
            function arrangeItems() {
                const angleStep = (Math.PI * 2) / itemCount;
                
                items.forEach((item, index) => {
                    const itemAngle = angle + angleStep * index;
                    const x = Math.sin(itemAngle) * radius;
                    const z = Math.cos(itemAngle) * radius;
                    
                    item.style.transform = `translateX(${x}px) translateZ(${z}px) rotateY(${-itemAngle * (180 / Math.PI)}deg)`;
                    item.style.opacity = 1 - Math.abs(z) / radius * 0.5;
                    item.style.zIndex = Math.round(z);
                });
            }
            
            // 鼠标拖拽事件
            gallery.addEventListener('mousedown', function(e) {
                isDragging = true;
                startX = e.clientX;
                currentAngle = angle;
                gallery.style.cursor = 'grabbing';
                e.preventDefault();
            });
            
            document.addEventListener('mousemove', function(e) {
                if (!isDragging) return;
                
                const deltaX = e.clientX - startX;
                angle = currentAngle + deltaX * 0.01;
                arrangeItems();
            });
            
            document.addEventListener('mouseup', function() {
                isDragging = false;
                gallery.style.cursor = 'grab';
            });
            
            // 触摸事件支持
            gallery.addEventListener('touchstart', function(e) {
                isDragging = true;
                startX = e.touches[0].clientX;
                currentAngle = angle;
                e.preventDefault();
            });
            
            document.addEventListener('touchmove', function(e) {
                if (!isDragging) return;
                
                const deltaX = e.touches[0].clientX - startX;
                angle = currentAngle + deltaX * 0.01;
                arrangeItems();
            });
            
            document.addEventListener('touchend', function() {
                isDragging = false;
            });
            
            // 自动旋转
            let autoRotate = true;
            let rotateInterval = setInterval(function() {
                if (!isDragging && autoRotate) {
                    angle += 0.005;
                    arrangeItems();
                }
            }, 30);
            
            // 鼠标悬停时暂停自动旋转
            gallery.addEventListener('mouseenter', function() {
                autoRotate = false;
            });
            
            gallery.addEventListener('mouseleave', function() {
                autoRotate = true;
            });
            
            // 初始化布局
            arrangeItems();
            
            // 响应式调整
            window.addEventListener('resize', function() {
                arrangeItems();
            });
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值