HTML+CSS实现懒加载,只需简单JS真的简单!

2024-08-2919:47:04WEB前端开发Comments418 views字数 1383阅读模式

HTML部分文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/65037.html

<h1>元素进入视口时的动画示例</h1>
    <p>向下滚动以查看元素动画效果。</p>

    <div class="box animate-on-scroll">Box 1</div>
    <div class="box animate-on-scroll">Box 2</div>
    <div class="box animate-on-scroll">Box 3</div>
    <div class="box animate-on-scroll">Box 4</div>
    <div class="box animate-on-scroll">Box 5</div>

CSS部分文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/65037.html

body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            line-height: 1.6;
        }

        .animate-on-scroll {
            opacity: 0;
            transform: translateY(50px);
            transition: opacity 0.5s, transform 0.5s;
        }

        .animate-on-scroll.animate {
            opacity: 1;
            transform: translateY(0);
        }

        .box {
            width: 200px;
            height: 200px;
            background-color: #4CAF50;
            margin: 50px auto;
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            font-size: 24px;
        }

JS文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/65037.html

<script>
        const observer = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    entry.target.classList.add('animate');
                }
            });
        }, {
            threshold: 0.1
        });

        document.querySelectorAll('.animate-on-scroll').forEach((el) => {
            observer.observe(el);
        });
    </script>

综上:效果实现啦!文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/65037.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/gcs/65037.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定