惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
爱范儿
爱范儿
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
B
Blog
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
D
DataBreaches.Net
B
Blog RSS Feed
小众软件
小众软件
人人都是产品经理
人人都是产品经理
I
InfoQ
P
Proofpoint News Feed
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - smile轉角

【js】ES5,ES6继承是如何实现的 【js】setTimeout、Promise、Async/Await 的区别 【面试题】思维逻辑方面 【js】js内置对象Error(错误机制) 【TS】学习笔记 【js】CommonJS、AMD、CMD三种规范 【其他】查看Animate.css官网动画没有效果 【vue3】父子组件通信之 vue3 defineProps,defineEmits ,defineExpose 【js】JS严格模式有什么特点 【css】使用弹性盒子布局时,省略号问题 【vue】 Failed to load resource: the server responded with a status of 404 (Not Found) 【第三方】富文本调研 【js】json的相关操作 【vue3】资料 【css】展示背景图片的底部部分 【html】 svg 【html5】html5中input 标签 type值为range时,修改其默认css 【js】forEach,for...in,for...of 区别 【js】map,reduce,filter的区别
【js】元素是否在可视区范围内
smile轉角 · 2022-10-27 · via 博客园 - smile轉角

公式:元素到顶部距离(最近的具有定位父级元素)- 滚动条高度  <= 可视区的高度  

  • offsetTop、scrollTop

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .div {
            height: 2000px;
        }

        p {
            height: 200px;
            background: red;
        }
    </style>
</head>

<body>
    <div class="div"></div>

    <p id="p">我出现啦</p>

    <!-- 通过元素位置关系方法-->
    <script>
        function isContain(dom) {
            // 获取可视窗口的盖度。
            const screenHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
            // 获取滚动条滚动的高度
            const scrollTop = document.documentElement.scrollTop;
            // 获取元素偏移的高度。就是距离可视窗口的偏移量。
            const offsetTop = dom.offsetTop;
            return offsetTop - scrollTop <= screenHeight;
        }
        const p = document.getElementById("p");
        window.onscroll = () => {
            if (isContain(p)) {
                document.body.style.backgroundColor = 'blue'
            } else {
                document.body.style.backgroundColor = 'green'
            }
        }
    </script>

    <!-- 通过 getBoundingClientRect方法-->
    <!-- <script>
        // 只有当子元素全部出现在父元素中时,才会返回true。
        function isContain(dom) {
            const totalHeight = window.innerHeight || document.documentElement.clientHeight;
            const totalWidth = window.innerWidth || document.documentElement.clientWidth;
            // 当滚动条滚动时,top, left, bottom, right时刻会发生改变。
            const { top, right, bottom, left } = dom.getBoundingClientRect();
            console.log(top, right, bottom, left)
            return (top >= 0 && left >= 0 && right <= totalWidth && bottom <= totalHeight);
        }

        const p = document.getElementById("p");
        window.onscroll = () => {
            if (isContain(p)) {
                document.body.style.backgroundColor = 'blue'
            } else {
                document.body.style.backgroundColor = 'green'
            }
        }
    </script> -->

    <!-- 通过new IntersectionObserver();  -->
    <!-- <script>
        const observer = new IntersectionObserver((entries) => {
            if (entries[0].isIntersecting) {
                document.body.style.backgroundColor = "blue"
            } else {
                document.body.style.backgroundColor = "green"
            }
        }, { threshold: .2 });
        const p = document.getElementById("p")
        observer.observe(p)
    </script> -->
</body>

</html>