Hello, welcome toPeanut Shell B2B Foreign Trade Network_Professional B2B E-commerce Platform_Free B2B Information Publishing Network!
18951535724

Js performance progress: only seven lines of code can easily achieve an infinite scroll function

   2026-11-07 NetworkingName2020
Key Point:Infinite scrollAlso known as the falls stream, it has become a symbol of modern websites. It enhances user experience and makes browsing more fluid. It'll only take seven rows to share javasThe cript code can easily achieve the infinity of high-performance scrolls and deep-separate the back of itPerformance optimizationPrinciple。The pain of traditionBefore talking about optimising programmes, let us look at the problems that exist in the i

High performance falls optimization - css achieve falls flows - infinite rolling javas <i> /i>cript code

Infinite scrollAlso known as the falls stream, it has become a symbol of modern websites. It enhances user experience and makes browsing more fluid. It'll only take seven rows to share javasThe cript code can easily achieve the infinity of high-performance scrolls and deep-separate the back of itPerformance optimizationPrinciple。

The pain of tradition

Before talking about optimising programmes, let us look at the problems that exist in the implementation of the traditional infinite rolling:

Frequent dom operations: every time new content is loaded, a large number of dom nodes are created and inserted into the event mishandling: the scroll event triggers with a high frequency, leading to reduced performance and waste of resources: all content is kept in dom, even if it has been leaking through the visual vent: after long periods of use, memory occupancy continues to increase

These problems may not be apparent in data volume hours, but when users scroll in depth, the pages become more and more cartons and even collapse。

The magic of a seven-line code

The following is an optimised, infinite core scroll code:

Co= new interspecioNobserver
if (entries [0]. Isintersering &! Is loating)
is loating = true;
(a) load moreitems(). Then(() = is loating = false);
♪ i'm sorry ♪
i'm not sure. Observer. Observe(d)I'm sorry, i'm sorry#sentinelI'm sorry;

This short seven-line code addresses all traditional pain points and achieves the best performance of unlimited rolling. It seems simple, but it contains multiple performance optimization techniques。

Performance optimized resolution

1. Intersectionobserver instead of scroll

Traditional realization usually depends on the scroll event:

I don't know'scroll',()=>
/ / check to scroll bottom and load more
i'm not sure;

The problem is that the scroll events are triggered very often (dozens or even hundreds of times per second), and even the use of thrifts or tremors can be depleted。

Intersectionobserver, which is an api provided by the browser's originals, is able to observe the intersection between the target element and the viewer, and only triggers the echo when needed, significantly reducing unnecessary calculations。

2. Virtual list and dom recovery

A truly efficient unlimited rolling is not just loading new content, but, more importantly, managing already existing content. We need to:

Funciledom()
/ / remove elements that are already far from view
co=dQuery selectorall'. ItemI'm sorry.
@titles. Foreach
co= item. Getboundingclintrect();
if (rect. Bottom window. Innerheight + 1000)
// save data but remove dom
itemcache. Set;
no, item. Remove();
♪ i'm sorry ♪
i'm not sure.
♪ i'm sorry ♪

This technology is called "dom recovery" and ensures that the size of the dom tree is kept within control。

3. Status locks to avoid duplication of requests

Note that the isloating state lock in the code prevents a new request from being triggered before the previous data is loaded:

If (entries [0]. Isintersering &! Is loating)
is loating = true;
(a) load moreitems(). Then(() = is loating = false);
♪ i'm sorry ♪

This simple state management avoids duplication of data loading and reduces unnecessary network requests and dom operations。

4. Photo laziness

Photo processing is particularly critical in an infinite scroll. Images are lazied in conjunction with intersectionobserver:

Fraction setupimagelazyload()
co= new interspecioNobserver
i'm sorry, kids. If (entry. Is intersering)
co= entry. Target;
img. Src = img. Dataset. Src;
igobserver. Unobserve(img);
♪ i'm sorry ♪
i'm not sure. I'm not sure. DQuery selectorall'img [data-src]. Foreach (img = > imgobserver. Observe (img));
♪ i'm sorry ♪

This ensures that only pictures near the viewer are loaded and significantly reduces bandwidth consumption and initial loading time。

Performance test data

On a test page with 1,000 records, the traditional method is compared to the optimised method:

The data show that optimisation has almost reached the flow experience of 60fps, while memory occupancy is only about 1/9 of traditional methods。

Operational applications

Extend the core code to a complete, directly usable:

Class infoscroller
co{)
oh, this= container;
this. Loadcallback =loadcallback;
this. Is loating = false;
this. Options ={
i don't know. This post is part of our special coverage global voices 2011. Watch size: 20.
... Options
};
=dI'm sorry, i'm sorry'divI'm sorry. You know, this'sentinel';
this. Container. Appendchild;
this. Setupobserver();
this. Setuprecycling();
♪ i'm sorry ♪
setupobserver(){
this. Observer = new interspeccioNobserver
if (entries [0]. Isintersering &! This. Is loating)
this. Is loating = true;
this. Loadcallback
. The(()=>
this. Is loating = false;
this. Recycledom();
i'm not sure.
♪ i'm sorry ♪
i'm not sure. This. Observer. Observe;
♪ i'm sorry ♪
recycledom(){
coNo, no, no, no'. Item:not (#sentinel)I'm sorry.
@titles. Foreach
co= item. Getboundingclintrect();
if (rect. Bottom window. Innerheight + this. Options. Recyclethreshold)
no, item. Remove();
♪ i'm sorry ♪
i'm not sure.
♪ i'm sorry ♪
destroy(){
this. Observer. DiscoNnect();
♪ i'm sorry ♪
♪ i'm sorry ♪

Example:

CoI'm sorry=dI'm sorry, i'm sorry'. Content-contenterI'm sorry. Co= new infoscroller
coI don't know what you're talking about. Renderitems (newitems, container);
i'm not sure;
 
ReportFavorite 0Tip 0Comment 0
>Related Comments
No comments yet, be the first to comment
>SimilarEncyclopedia
Featured Images
RecommendedEncyclopedia
Table of Contents2 sections