<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abid Hussain</title>
    <description>The latest articles on DEV Community by Abid Hussain (@abid_hussain_ef21db69ecf0).</description>
    <link>https://dev.to/abid_hussain_ef21db69ecf0</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2854105%2F03545463-00ed-465b-b06c-5ba42c319c64.png</url>
      <title>DEV Community: Abid Hussain</title>
      <link>https://dev.to/abid_hussain_ef21db69ecf0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abid_hussain_ef21db69ecf0"/>
    <language>en</language>
    <item>
      <title>Infinite Scrolling</title>
      <dc:creator>Abid Hussain</dc:creator>
      <pubDate>Tue, 05 May 2026 18:12:04 +0000</pubDate>
      <link>https://dev.to/abid_hussain_ef21db69ecf0/infinite-scrolling-6dp</link>
      <guid>https://dev.to/abid_hussain_ef21db69ecf0/infinite-scrolling-6dp</guid>
      <description>&lt;h1&gt;
  
  
  Infinite Scrolling in Modern Web Apps
&lt;/h1&gt;

&lt;p&gt;Infinite scrolling is one of the most commonly asked topics in coding interviews and widely used in real-world applications like social media feeds, e-commerce sites, and content platforms.&lt;br&gt;
So for implementing the Infinite scroll we need to understand the intersection observer, so before jumping directly into Intersection Observer, it's important to understand some foundational concepts that power traditional infinite scrolling.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Understanding Viewport Height
&lt;/h3&gt;

&lt;h4&gt;
  
  
  What is window.innerHeight (Viewport Height)?
&lt;/h4&gt;

&lt;p&gt;The viewport height is the visible area of the browser where content is displayed.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;window.innerHeight&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This gives the height of the visible screen (excluding browser UI like address bar)&lt;/p&gt;

&lt;h4&gt;
  
  
  What is document.documentElement.scrollHeight?
&lt;/h4&gt;

&lt;p&gt;This represents the total height of the webpage, including content that is not currently visible (scrollable part).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;document.documentElement.scrollHeight&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  What is document.documentElement.scrollTop?
&lt;/h4&gt;

&lt;p&gt;This tells how much the user has already scrolled from the top.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;document.documentElement.scrollTop&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Traditional Infinite Scrolling Approach
&lt;/h3&gt;

&lt;p&gt;Before modern APIs, developers used scroll events + calculations.&lt;/p&gt;

&lt;h4&gt;
  
  
  Logic:
&lt;/h4&gt;

&lt;p&gt;When user scrolls near the bottom → Load more data&lt;/p&gt;

&lt;h4&gt;
  
  
  Condition:
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;window.innerHeight + document.documentElement.scrollTop &amp;gt;= document.documentElement.scrollHeight - 10&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This means:&lt;/p&gt;

&lt;p&gt;Visible screen height + Scrolled amount = Total page height&lt;/p&gt;

&lt;p&gt;If true → user reached bottom&lt;/p&gt;

&lt;h4&gt;
  
  
  Example Implementation:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scroll&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;documentElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;scrollHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;documentElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scrollHeight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;clientHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHeight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;clientHeight&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;scrollHeight&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Load more data...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// API call here&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Problems with Traditional Approach
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Performance Issues
&lt;/h4&gt;

&lt;p&gt;Scroll event fires many times (very frequently)&lt;br&gt;
Can cause lag if not optimized&lt;/p&gt;

&lt;h4&gt;
  
  
  Manual Calculations
&lt;/h4&gt;

&lt;p&gt;You have to calculate everything yourself&lt;/p&gt;

&lt;h4&gt;
  
  
  Needs Throttling/Debouncing
&lt;/h4&gt;

&lt;p&gt;Otherwise too many API calls&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Modern Approach: Intersection Observer
&lt;/h3&gt;

&lt;p&gt;Now comes the real game changer.&lt;/p&gt;

&lt;p&gt;Instead of listening to scroll events, we observe when an element enters the viewport.&lt;/p&gt;

&lt;h4&gt;
  
  
  What is Intersection Observer?
&lt;/h4&gt;

&lt;p&gt;It is a browser API that allows you to detect when an element is visible on the screen.&lt;/p&gt;

&lt;p&gt;No manual scroll calculations&lt;br&gt;
No performance issues like scroll events&lt;/p&gt;

&lt;h4&gt;
  
  
  Concept:
&lt;/h4&gt;

&lt;p&gt;Add a "sentinel" div at the bottom&lt;br&gt;
When it becomes visible → Load more data&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;IntersectionObserver&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;entry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isIntersecting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Load more data...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// API call here&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#load-more&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;observe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>programming</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
