页面滚动视差动画

<!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;
            color: #fff;
            overflow-x: hidden;
        }
        
        .parallax-container {
            position: relative;
            height: 100vh;
            overflow: hidden;
        }
        
        .parallax-bg {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 120%;
            background-size: cover;
            background-position: center;
            background-attachment: fixed;
            z-index: -1;
        }
        
        .content-section {
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            padding: 50px;
            text-align: center;
            position: relative;
            background-color: rgba(0, 0, 0, 0.7);
        }
        
        h1 {
            font-size: 3rem;
            margin-bottom: 20px;
            opacity: 0;
            transform: translateY(50px);
            transition: all 0.8s ease;
        }
        
        p {
            font-size: 1.2rem;
            max-width: 800px;
            margin-bottom: 30px;
            opacity: 0;
            transform: translateY(50px);
            transition: all 0.8s ease 0.2s;
        }
        
        .btn {
            display: inline-block;
            padding: 12px 30px;
            background: #ff6b6b;
            color: white;
            text-decoration: none;
            border-radius: 50px;
            font-size: 1rem;
            transition: all 0.3s;
            opacity: 0;
            transform: translateY(50px);
            transition: all 0.8s ease 0.4s;
        }
        
        .btn:hover {
            background: #ff5252;
            transform: translateY(-3px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
        }
        
        .visible {
            opacity: 1;
            transform: translateY(0);
        }
        
        .section-1 .parallax-bg {
            background-image: url('https://source.unsplash.com/random/1920x1080?mountain');
        }
        
        .section-2 .parallax-bg {
            background-image: url('https://source.unsplash.com/random/1920x1080?forest');
            top: -20%;
        }
        
        .section-3 .parallax-bg {
            background-image: url('https://source.unsplash.com/random/1920x1080?ocean');
            top: -40%;
        }
        
        .footer {
            padding: 20px;
            text-align: center;
            background-color: #222;
            color: #aaa;
            font-size: 0.9rem;
        }
        
        .footer a {
            color: #ff6b6b;
            text-decoration: none;
        }
        
        .footer a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="parallax-container section-1">
        <div class="parallax-bg"></div>
        <div class="content-section">
            <h1>探索自然之美</h1>
            <p>感受大自然的壮丽景观,体验前所未有的视觉盛宴。</p>
            <a href="#" class="btn">开始探索</a>
        </div>
    </div>
    
    <div class="parallax-container section-2">
        <div class="parallax-bg"></div>
        <div class="content-section">
            <h1>森林秘境</h1>
            <p>深入神秘的森林,发现隐藏在绿色世界中的奇妙生命。</p>
            <a href="#" class="btn">了解更多</a>
        </div>
    </div>
    
    <div class="parallax-container section-3">
        <div class="parallax-bg"></div>
        <div class="content-section">
            <h1>海洋奇观</h1>
            <p>潜入蔚蓝深海,探索地球上最神秘的水下世界。</p>
            <a href="#" class="btn">立即体验</a>
        </div>
    </div>
    
    <div class="footer">

    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 视差滚动效果
            window.addEventListener('scroll', function() {
                const scrollPosition = window.pageYOffset;
                const parallaxBg1 = document.querySelector('.section-1 .parallax-bg');
                const parallaxBg2 = document.querySelector('.section-2 .parallax-bg');
                const parallaxBg3 = document.querySelector('.section-3 .parallax-bg');
                
                parallaxBg1.style.transform = `translateY(${scrollPosition * 0.5}px)`;
                parallaxBg2.style.transform = `translateY(${scrollPosition * 0.3}px)`;
                parallaxBg3.style.transform = `translateY(${scrollPosition * 0.2}px)`;
            });
            
            // 滚动显示动画
            function checkVisibility() {
                const sections = document.querySelectorAll('.content-section');
                
                sections.forEach(section => {
                    const sectionTop = section.getBoundingClientRect().top;
                    const windowHeight = window.innerHeight;
                    
                    if (sectionTop < windowHeight * 0.75) {
                        const h1 = section.querySelector('h1');
                        const p = section.querySelector('p');
                        const btn = section.querySelector('.btn');
                        
                        h1.classList.add('visible');
                        p.classList.add('visible');
                        btn.classList.add('visible');
                    }
                });
            }
            
            // 初始检查
            checkVisibility();
            
            // 滚动时检查
            window.addEventListener('scroll', checkVisibility);
            
            // 预加载图片
            function preloadImages() {
                const images = [
                    'https://source.unsplash.com/random/1920x1080?mountain',
                    'https://source.unsplash.com/random/1920x1080?forest',
                    'https://source.unsplash.com/random/1920x1080?ocean'
                ];
                
                images.forEach(img => {
                    const image = new Image();
                    image.src = img;
                });
            }
            
            preloadImages();
        });
    </script>
</body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值