【JS场景题】判断一个元素是否在可视区域内有哪些方法?

news/2024/10/6 11:44:33/

方法一、通过元素的位置信息和滚动条滚动的高度来判断

前置知识

  • clientWidth: 元素的内容区域宽度加上左右内边距宽度。
  • offsetTop: 元素的上外边框至包含元素的上内边框之间的像素距离。
  • document.documentElement.clientHeight: 获取视口高度(不包含滚动条的隐藏高度)。
  • document.documentElement.scrollHeight:获取浏览器窗口的总高度(包含滚动条的隐藏高度)。
  • document.documentElement.scrollTop: 获取滚动条滚动的高度

代码:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>* {margin: 0;padding: 0;}.div {height: 2000px;}p {height: 200px;color: white;background-color: blueviolet;}</style>
</head>
<body><!-- 1. 通过元素的位置信息和滚动条滚动的高度来判断 --><div class="div"></div><p>我出现啦</p><script>javascript">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.querySelector('p')window.onscroll = () => {if (isContain(p)) {document.body.style.backgroundColor = 'blue'} else {document.body.style.backgroundColor = 'skyblue'}}</script>
</body>
</html>

方法二、通过getBoundingClientRect方法来获取元素的位置信息,然后加以判断

前置知识

  • getBoundingClientRect方法: DOM对象的一个方法,返回一个DOMRect对象。

  • 详解:https://blog.csdn.net/weixin_61597234/article/details/134878221

  • 如果想要判断子元素是否在可视区域内,只需要:

    • top >= 0
    • left >= 0
    • bottom <= 视口高度
    • right <= 视口宽度

代码:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>* {margin: 0;padding: 0;}.div {height: 2000px;}p {height: 200px;color: white;background-color: blueviolet;}</style>
</head>
<body><!-- 2. 通过getBoundingClientRect方法来获取元素的位置信息,然后加以判断 --><div class="div"></div><p>我出现啦</p><script>javascript">function isContain(dom) {const totalHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;const totalWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;// 当滚动条滚动时,top、left、bottom、right时刻会发生改变const { top, right, bottom, left } = dom.getBoundingClientRect();return left >= 0 && top >= 0 && right <= totalWidth && bottom <= totalHeight;}const p = document.querySelector('p')window.onscroll = () => {if (isContain(p)) {document.body.style.backgroundColor = 'red'} else {document.body.style.backgroundColor = 'green'}}</script>
</body>
</html>

方法三、通过交叉检查器:Intersection Observer 来实现监听(推荐)

前置知识

MDN: https://developer.mozilla.org/zh-CN/docs/Web/API/Intersection_Observer_API

代码:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>* {margin: 0;padding: 0;}.div {height: 2000px;}p {height: 200px;color: white;background-color: blueviolet;}</style>
</head>
<body><!-- 3. 通过交叉检查器:Intersection Observer 来实现监听 --><div class="div"></div><p>我出现啦</p><script>javascript">const p = document.querySelector('p')const observer = new IntersectionObserver(entries => {if (entries[0].isIntersecting) {document.body.style.backgroundColor = 'skyblue'} else {document.body.style.backgroundColor = 'orange'}}, {threshold: .2,  // 表示当子元素和父元素覆盖多少时触发回调函数。});observer.observe(p)</script>
</body>
</html>

http://www.ppmy.cn/news/1473970.html

相关文章

Http 实现请求body体和响应body体的双向压缩方案

目录 一、前言 二、方案一(和http header不进行关联) 二、方案二(和http header进行关联) 三、 客户端支持Accept-Encoding压缩方式,服务器就一定会进行压缩吗? 四、参考 一、前言 有时请求和响应的body体比较大,需要进行压缩,以减少传输的带宽。 二、方案一(和…

Kubernetes集群性能测试之kubemark集群搭建

Kubernetes集群性能测试之kubemark集群搭建 Kubemark是K8s官方提供的一个对K8s集群进行性能测试的工具。它可以模拟出一个K8s cluster&#xff08;Kubemark cluster&#xff09;&#xff0c;不受资源限制&#xff0c;从而能够测试的集群规模比真实集群大的多。这个cluster中ma…

hadoop分布式中某个 节点报错的解决案例

前言 在分布式节点中&#xff0c;发现有个节点显示不可用状态&#xff0c;因此需要紧急修复。 hadoop版本 目前这套集群hadoop的版本如下&#xff1a; 集群报错详细日志&#xff1a; 1/1 local-dirs are bad: /kkb/install/hadoop-2.6.0-cdh5.14.2/hadoopDatas/tempDatas/n…

VPN 的入门介绍

VPN&#xff08;虚拟专用网络&#xff09; 简介 虚拟专用网络&#xff0c;简称虚拟专网&#xff08;VPN&#xff09;&#xff0c;其主要功能是在公用网络上建立专用网络&#xff0c;进行加密通讯。在企业网络中有广泛应用。VPN网关通过对数据包的加密和数据包目标地址的转换实…

Kubernetes RBAC 之 ServiceAccount

Kubernetes RBAC 之 ServiceAccount 定义 RABC 英文全称是 Role-Based Access Control&#xff0c;它通过角色绑定账户&#xff0c;来使得账户拥有某些操控 K8S 集群的权限。ServiceAccount 是集群内部 Pod 访问集群所使用的服务账户&#xff0c;它包括了 Namespace、Token、…

易备数据备份软件——数据安全的可靠卫士

在信息时代&#xff0c;数据的重要性不言而喻。易备数据备份软件&#xff0c;犹如一位忠诚的卫士&#xff0c;全方位守护您的数据安全。 易备的云备份功能强大&#xff0c;支持通过多种协议连接网络存储设备&#xff0c;无论是天翼云、华为云、阿里云等国内主流云服务&#xf…

数字货币高频交易策略解析:深入理解与实践指南

随着数字货币市场的蓬勃发展&#xff0c;高频交易&#xff08;High-Frequency Trading, HFT&#xff09;逐渐成为投资者关注的焦点。高频交易以其快速的交易执行和微小的利润累积而闻名&#xff0c;是量化交易领域中的一颗璀璨明珠。本文将为读者提供一份深入浅出的高频交易策略…

Mac本地部署Stable-Diffusion

之前有AI绘图场景时一直用的是封装好的软件&#xff0c;因为不用考虑背后繁琐的细节&#xff0c;但因为各种原因难免会有使用不了等问题&#xff0c;为了一劳永逸&#xff0c;决定直接使用Stable-Diffusion。之前因为图省事以及电脑硬盘所剩空间捉襟见肘就没部署(Stable-Diffus…