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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

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