什么是无限滚动以及它的必要性?
滚动是水平或垂直移动网页上部分内容的用户操作(在大多数情况下)。
就像您在阅读本文时所做的那样。
无限意味着当您向下滚动网页时,新内容会自动加载。
好吧,但是为什么每个人都应该实现它?
可发现性
让我们想象一下您最喜欢的电子商务商店正在举办黑色星期五促销活动。
您在探索页面上找到了几个产品,但当您滚动到网页底部而不是更多产品时,您发现了一个按钮,可将您带到下一个产品列表。
您将能够看到新产品(但前提是您注意到该操作按钮)。
无限滚动只是帮助用户找到更多他们可能错过的内容。
执行
为了实现无限滚动,我们需要检查用户是否到达页面底部或容器。
但是检测滚动的位置是非常昂贵的,并且由于不同的浏览器和设备,其位置值不可靠。
所以一种方法是观看页面的最后内容(元素)及其与视口或容器的交点。
我们如何找到交点?
路口观察者
它是一个 Web API,允许观察内容或列表末尾的元素。
当这个元素(“哨兵”)变得可见(与视口相交时,它会触发回调函数。
通过这个函数我们可以获取更多数据并将其加载到网页中。
整个观察是异步发生的,这最小化对主线程的影响。
为了在 Reactjs 中实现 Intersection Observer,我们将以社交提要为例,我们将在帖子列表上进行无限滚动。
看一下这个组件,您就可以了解下面每个部分的详细情况。
import { useEffect, useRef, useState } from "react"; interface IIntersectionObserverProps {} const allItems = [ "https://picsum.photos/200", "https://picsum.photos/200", "https://picsum.photos/200", "https://picsum.photos/200", ]; const IntersectionObserverImplement: React.FunctionComponent = (props) => { const cardRefs = useRef([]); // Initialize as an empty array const containerRef = useRef<htmldivelement null>(null); const [listItems, setListItems] = useState(allItems); useEffect(() => { const options = { root: containerRef.current, rootMargin: "0px", threshold: 0.5, }; const observer = new IntersectionObserver((entries, observer) => { entries.forEach((entry) => { if (entry.isIntersecting) { setListItems((prevItems) => [ ...prevItems, "https://picsum.photos/200", ]); observer.unobserve(entry.target); // Stop observing the current element } }); }, options); // Observe the last card only const lastCard = cardRefs.current[listItems.length - 1]; if (lastCard) { observer.observe(lastCard); } return () => observer.disconnect(); // Clean up observer on unmount }, [listItems]); return ( <div classname="container" ref="{containerRef}"> {listItems.map((eachItem, index) => ( <div classname="card" ref="{(el)"> (cardRefs.current[index] = el)} // Assign refs correctly key={index} > <h5 id="Post-index">Post {index}</h5> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173443591411756.jpg?x-oss-process=image/resize,p_40" class="lazy" style="max-width:90%"200"}' height='{"150"}' alt="Reactjs 教程:使用 Intersection Observer 进行无限滚动。" > </div> ))} </div> ); }; export default IntersectionObserverImplement; </htmldivelement>
目标是检测提要列表中的最后一个帖子(称为哨兵)何时与视口相交。一旦发生这种情况,就会加载并显示更多帖子。
一个。初始化状态和引用
const cardRefs = useRef([]); // For storing references to each card const containerRef = useRef<htmldivelement null>(null); // Reference to the scrollable container const [listItems, setListItems] = useState(allItems); // State to hold the list of items </htmldivelement>
cardRefs 一个数组,用于跟踪表示列表中卡片的 DOM 元素。
containerRef 指的是可滚动容器。
listItems 保存页面上当前可见项目的数组。
b.渲染列表并分配引用
return ( <div classname="container" ref="{containerRef}"> {listItems.map((eachItem, index) => ( <div classname="card" ref="{(el)"> (cardRefs.current[index] = el)} // Assign a ref to each card key={index} > <h5 id="Post-index">Post {index}</h5> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173443591411756.jpg?x-oss-process=image/resize,p_40" class="lazy" style="max-width:90%"200"}' height='{"150"}' alt="Reactjs 教程:使用 Intersection Observer 进行无限滚动。" > </div> ))} </div> );
containerRef 标记将发生滚动的容器。
cardRefs 为列表中的每张卡片分配一个参考。这确保我们可以告诉观察者要监视哪个元素(例如,最后一张卡片)。
映射 listItems 以呈现列表中的每个项目。
每个 div 都被设计成一张卡片,并且有一个唯一的 React 键。
c.观察最后一个帖子(项目)。
import { useEffect, useRef, useState } from "react"; interface IIntersectionObserverProps {} const allItems = [ "https://picsum.photos/200", "https://picsum.photos/200", "https://picsum.photos/200", "https://picsum.photos/200", ]; const IntersectionObserverImplement: React.FunctionComponent = (props) => { const cardRefs = useRef([]); // Initialize as an empty array const containerRef = useRef<htmldivelement null>(null); const [listItems, setListItems] = useState(allItems); useEffect(() => { const options = { root: containerRef.current, rootMargin: "0px", threshold: 0.5, }; const observer = new IntersectionObserver((entries, observer) => { entries.forEach((entry) => { if (entry.isIntersecting) { setListItems((prevItems) => [ ...prevItems, "https://picsum.photos/200", ]); observer.unobserve(entry.target); // Stop observing the current element } }); }, options); // Observe the last card only const lastCard = cardRefs.current[listItems.length - 1]; if (lastCard) { observer.observe(lastCard); } return () => observer.disconnect(); // Clean up observer on unmount }, [listItems]); return ( <div classname="container" ref="{containerRef}"> {listItems.map((eachItem, index) => ( <div classname="card" ref="{(el)"> (cardRefs.current[index] = el)} // Assign refs correctly key={index} > <h5 id="Post-index">Post {index}</h5> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173443591411756.jpg?x-oss-process=image/resize,p_40" class="lazy" style="max-width:90%"200"}' height='{"150"}' alt="Reactjs 教程:使用 Intersection Observer 进行无限滚动。" > </div> ))} </div> ); }; export default IntersectionObserverImplement; </htmldivelement>
选项对象
const cardRefs = useRef([]); // For storing references to each card const containerRef = useRef<htmldivelement null>(null); // Reference to the scrollable container const [listItems, setListItems] = useState(allItems); // State to hold the list of items </htmldivelement>
root 指定滚动容器。
containerRef.current 指的是包裹所有卡片的 div。
如果 root 为 null,则默认观察视口。
rootMargin:定义根周围的额外边距。
“0px”表示没有多余的空间。您可以使用“100px”之类的值来提前触发观察者(例如,当元素即将出现时)。
阈值:确定观察者触发时目标元素必须可见的程度。
0.5 表示当最后一张卡片的 50% 可见时触发回调。
创建观察者
return ( <div classname="container" ref="{containerRef}"> {listItems.map((eachItem, index) => ( <div classname="card" ref="{(el)"> (cardRefs.current[index] = el)} // Assign a ref to each card key={index} > <h5 id="Post-index">Post {index}</h5> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173443591411756.jpg?x-oss-process=image/resize,p_40" class="lazy" style="max-width:90%"200"}' height='{"150"}' alt="Reactjs 教程:使用 Intersection Observer 进行无限滚动。" > </div> ))} </div> );
IntersectionObserver 接受回调函数和之前定义的选项对象。
每当观察到的元素满足选项中指定的条件时,回调就会运行。
entries 参数是观察到的元素的数组。每个条目都包含有关元素是否相交(可见)的信息。
如果entry.isIntersecting为true,则意味着最后一张卡片现在可见:
- 使用 setListItems 将新项目添加到列表中。
- 取消观察当前元素(entry.target)以防止冗余触发器。
观察最后一张牌
useEffect(() => { const options = { root: containerRef.current, rootMargin: "0px", threshold: 0.5, }; const observer = new IntersectionObserver((entries, observer) => { entries.forEach((entry) => { if (entry.isIntersecting) { setListItems((prevItems) => [ ...prevItems, "https://picsum.photos/200", ]); observer.unobserve(entry.target); // Stop observing the current element } }); }, options); // Observe each card const lastCard = cardRefs.current[listItems.length - 1]; if (lastCard) { observer.observe(lastCard); } return () => observer.disconnect(); // Clean up observer on unmount }, [listItems]);
cardRefs.current:跟踪对所有卡片的引用。
listItems.length - 1:标识列表中的最后一项。
如果lastCard存在,使用observer.observe(lastCard)开始观察它。
观察者会监听这张卡片,并在它可见时触发回调。
清理
const options = { root: containerRef.current, // Observe within the container rootMargin: "0px", // No margin around the root container threshold: 0.5, // Trigger when 50% of the element is visible };
observer.disconnect() 删除此 useEffect 创建的所有观察者。
这确保了当组件卸载或重新渲染时,旧的观察者被清理。
每个阶段会发生什么?
1。用户滚动
当用户滚动时,最后一张卡片进入视图
2。路口观察者触发器
当最后一张牌的50%可见时,观察者的回调
运行。
3。添加项目
回调将新项目添加到列表中 (setListItems)。
4。重复
观察者与旧的最后一张卡断开连接并附加到
新的最后一张卡。
这就是我们如何使用 Intersection Observer.
实现无限滚动希望这对您有帮助:)
谢谢。
以上是Reactjs 教程:使用 Intersection Observer 进行无限滚动。的详细内容。更多信息请关注PHP中文网其他相关文章!

理解JavaScript引擎内部工作原理对开发者重要,因为它能帮助编写更高效的代码并理解性能瓶颈和优化策略。1)引擎的工作流程包括解析、编译和执行三个阶段;2)执行过程中,引擎会进行动态优化,如内联缓存和隐藏类;3)最佳实践包括避免全局变量、优化循环、使用const和let,以及避免过度使用闭包。

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。

Python和JavaScript在社区、库和资源方面的对比各有优劣。1)Python社区友好,适合初学者,但前端开发资源不如JavaScript丰富。2)Python在数据科学和机器学习库方面强大,JavaScript则在前端开发库和框架上更胜一筹。3)两者的学习资源都丰富,但Python适合从官方文档开始,JavaScript则以MDNWebDocs为佳。选择应基于项目需求和个人兴趣。

从C/C 转向JavaScript需要适应动态类型、垃圾回收和异步编程等特点。1)C/C 是静态类型语言,需手动管理内存,而JavaScript是动态类型,垃圾回收自动处理。2)C/C 需编译成机器码,JavaScript则为解释型语言。3)JavaScript引入闭包、原型链和Promise等概念,增强了灵活性和异步编程能力。

不同JavaScript引擎在解析和执行JavaScript代码时,效果会有所不同,因为每个引擎的实现原理和优化策略各有差异。1.词法分析:将源码转换为词法单元。2.语法分析:生成抽象语法树。3.优化和编译:通过JIT编译器生成机器码。4.执行:运行机器码。V8引擎通过即时编译和隐藏类优化,SpiderMonkey使用类型推断系统,导致在相同代码上的性能表现不同。

JavaScript在现实世界中的应用包括服务器端编程、移动应用开发和物联网控制:1.通过Node.js实现服务器端编程,适用于高并发请求处理。2.通过ReactNative进行移动应用开发,支持跨平台部署。3.通过Johnny-Five库用于物联网设备控制,适用于硬件交互。

我使用您的日常技术工具构建了功能性的多租户SaaS应用程序(一个Edtech应用程序),您可以做同样的事情。 首先,什么是多租户SaaS应用程序? 多租户SaaS应用程序可让您从唱歌中为多个客户提供服务

本文展示了与许可证确保的后端的前端集成,并使用Next.js构建功能性Edtech SaaS应用程序。 前端获取用户权限以控制UI的可见性并确保API要求遵守角色库


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3汉化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版

禅工作室 13.0.1
功能强大的PHP集成开发环境