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

推荐订阅源

V
V2EX
W
WeLiveSecurity
IT之家
IT之家
A
About on SuperTechFans
B
Blog
L
LangChain Blog
H
Help Net Security
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
Google Online Security Blog
Google Online Security Blog
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
N
News and Events Feed by Topic
Schneier on Security
Schneier on Security
GbyAI
GbyAI
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
S
SegmentFault 最新的问题
Cloudbric
Cloudbric
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Y
Y Combinator Blog
S
Security Affairs
The Last Watchdog
The Last Watchdog
H
Heimdal Security Blog
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
博客园 - 聂微东
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Privacy & Cybersecurity Law Blog
V
Visual Studio Blog
H
Hacker News: Front Page
Recorded Future
Recorded Future
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Blog — PlanetScale
Blog — PlanetScale
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
C
Cybersecurity and Infrastructure Security Agency CISA
AWS News Blog
AWS News Blog
Jina AI
Jina AI
N
News | PayPal Newsroom
S
Schneier on Security

博客园 - 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>