Guest Mode:

BCA Student Stories & UpdatesLive

You
Your StorySign in & share
Er. Manoj Kumar
D5
Er. ManojActive nowPages
Day 48Advanced JavaScript & Browser APIs2.5 Hours Practical
Push to GitHub to Complete

Modern Web Performance & Intersection Observer API (Lazy Loading & Infinite Scroll)

Web page speed directly dictates user engagement and SEO rankings. Instead of downloading 100 high-resolution images when a webpage loads, modern websites lazy-load assets only when they scroll into the viewport. The Intersection Observer API provides a high-performance native mechanism to track element visibility without heavy scroll event listeners.

Interactive Learning Roadmap

Day 48 Step-by-Step Practical Flow

🎯1. What You Will Learn Today

✔Understand how Intersection Observer detects when elements enter or exit the visible browser viewport.
✔Construct an IntersectionObserver instance with custom threshold and rootMargin settings.
✔Implement high-performance image lazy loading using placeholder data-src attributes.
✔Build an infinite-scroll feed that triggers asynchronous API loading when a sentinel element enters view.
✔Avoid layout thrashing and preserve 60fps scrolling performance.
Day 48 MasterclassAdvanced JavaScript & Browser APIs

Modern Web Performance & Intersection Observer API (Lazy Loading & Infinite Scroll) — Comprehensive Practical Guide

Har concept ko visual mental models, code anatomy aur practical industry tips ke sath samjhein.

Real-World Purpose Aur Zaroorat

Web page speed directly dictates user engagement and SEO rankings. Instead of downloading 100 high-resolution images when a webpage loads, modern websites lazy-load assets only when they scroll into the viewport. The Intersection Observer API provides a high-performance native mechanism to track element visibility without heavy scroll event listeners.

Observer Pattern: Create an observer instance with const observer = new IntersectionObserver(callback, options). Call observer.observe(element) to watch an element.
Entry Intersection: Inside the callback, entries.forEach(entry => ...) inspects entry.isIntersecting (boolean) and entry.intersectionRatio.
Lazy Image Technique: Set image src to a tiny lightweight placeholder, storing the real image in data-src. When intersecting, swap entry.target.src = entry.target.dataset.src.
Unobserving: Once an image is loaded, call observer.unobserve(entry.target) to release browser resources.
Root Margin: Set rootMargin: "200px" to start pre-loading images 200 pixels BEFORE they appear on screen for a seamless user experience.
Dimaag Mein Bithane Ka Model

"Imagine a physical supermarket: the stockroom manager does not pile 10,000 cartons of milk onto the checkout counter all at once. Instead, a floor worker observes the display shelf (observer), and when stock runs down to 2 bottles (threshold), brings out the next batch from the back."

💡 Pro Tip: Code ko ratta mat maaro; imagine karo ki browser har tag ko visually kaise draw karta hai.

👨‍🏫3. Teacher Demonstration Code

See how image cards download their heavy assets only when scrolled within 100px of the visible viewport.

Lazy Image Loading with Blur-Up Transition Consolehtml
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Intersection Observer Demo</title>
  <style>
    body { font-family: system-ui, sans-serif; background: #060c18; color: #fff; padding: 24px; text-align: center; }
    .card { max-width: 440px; margin: 40px auto; background: #0c1328; border: 1px solid #1e293b; padding: 16px; border-radius: 12px; }
  </style>
</head>
<body>
  <h2>⚡ Intersection Observer Lazy Loading</h2>
  <div class="card"><p>Scroll down to reveal lazy card</p></div>
  <div style="height:300px;"></div>
  <div class="card" id="targetCard"><p>🎯 Intersected Target Card Loaded</p></div>

  <script>
    const observer = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting) e.target.style.borderColor = "#04AA6D";
      });
    });
    observer.observe(document.getElementById("targetCard"));
  </script>
</body>
</html>

2. Hands-On Practical Lab: Follow Instructions in VS Code

Code like a professional software engineer! Follow the step-by-step instructions below in Visual Studio Code (VS Code) on your PC, test your output locally, and push to GitHub.

3 Coding Tasks
Interactive Coding TestAuto-Evaluates as you type

Day 48 Practical Task Test Studio

Test Score:0 / 3 Passed
1. Practical Task Specifications & Live Test Checklist:Type code below to pass tests
Task 1Instantiate IntersectionObserver

Create a new IntersectionObserver with a callback that inspects entries.

⏳ WAITING
Task 2Check isIntersecting and Swap data-src

Inside the entry callback, check entry.isIntersecting and set img.src = img.dataset.src.

⏳ WAITING
Task 3Observe Target Elements

Call observer.observe on each lazy image element.

⏳ WAITING
2. Type HTML & CSS Code:index.html
index.html
⚡ Real-Time Testing Active
Tab inserts 2 spaces • Live DOM refresh93 chars
Live Browser Output:
🔒 http://localhost:5500/index.html
Live
Renders live HTML & CSS typed on the left.
Interactive Bug Detective

Spot the Bug: Memory Leak from Never Unobserving Intersected Elements

Tap or click on the exact line below that contains the error:

Click any code line
⚠️ Broken Code Inspector
Tap buggy line
1
const observer = new IntersectionObserver((entries) => {
2
entries.forEach(entry => {
3
if (entry.isIntersecting) {
4
entry.target.src = entry.target.dataset.src;
5
// ❌ Missing unobserve! Observer keeps watching all 2,000 images forever!
6
}
7
});
8
});
Stuck? Need a detective clue before checking the solution?

Senior Developer Forensic Analysis:

Once an image has successfully loaded its high-resolution source, it has completed its lifecycle. Failing to call observer.unobserve(entry.target) keeps the DOM node attached to the observer pipeline, causing a severe memory leak as hundreds of elements accumulate.

Golden Rule: Always validate code syntax and security attributes before pushing to GitHub!

📝Independent Practice: Assignment

📄 index.html

High-Performance Infinite Scrolling Course Catalog

  • •Create a responsive course catalog that begins with 4 initial course cards.
  • •Place a dedicated sentinel element (#sentinel) at the very bottom of the card list.
  • •Use IntersectionObserver to watch #sentinel: when it enters view, simulate an asynchronous fetch that appends 3 new course cards.
  • •Display a smooth animated spinner during loading, terminating gracefully after reaching 20 total cards.
Submission Evaluation Criteria: Push infinite-scroll.html and catalog.js to your GitHub repository under day-48.
Exact Assignment Solution Code: index.html

Open Visual Studio Code on your PC (code .), create index.html, paste this complete step-by-step verified code, test in your browser, and push to your GitHub repo:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>BCA Day 48 - Infinite Scroll Catalog</title>
  <style>
    body { font-family: "Segoe UI", sans-serif; background: #060c18; color: #fff; padding: 24px; text-align: center; }
    .catalog { max-width: 600px; margin: auto; }
    .card { background: #0c1328; border: 1px solid #1e293b; padding: 20px; border-radius: 12px; margin-bottom: 16px; text-align: left; }
    .sentinel { padding: 20px; color: #38bdf8; font-weight: bold; }
  </style>
</head>
<body>
  <div class="catalog">
    <h2>📚 Infinite Course Catalog</h2>
    <div id="courseList"></div>
    <div class="sentinel" id="sentinel">Loading more... ⏳</div>
  </div>

  <script>
    let count = 0;
    const maxCourses = 12;
    const list = document.getElementById("courseList");
    const sentinel = document.getElementById("sentinel");

    function loadBatch() {
      for (let i = 0; i < 4; i++) {
        if (count >= maxCourses) {
          sentinel.textContent = "🎉 Reached end of catalog.";
          observer.unobserve(sentinel);
          return;
        }
        count++;
        const card = document.createElement("div");
        card.className = "card";
        card.innerHTML = "<h4>Course #" + count + ": BCA Practical</h4>";
        list.appendChild(card);
      }
    }

    const observer = new IntersectionObserver((entries) => {
      if (entries[0].isIntersecting) loadBatch();
    }, { rootMargin: "100px" });

    loadBatch();
    observer.observe(sentinel);
  </script>
</body>
</html>
Verified Assignment Code Solution (100% Tested & Ready to Run)

🎓4. Viva Check: Mandatory Conceptual Test

Which property on navigator reliably reports whether the client browser currently has an active network connection?

5. Mandatory Step: Push to GitHub to Unlock Next Lesson

Target Repo: html-css-projects

Windows 11 Terminal / VS Code
1$
mkdir day-48
Create a new project folder (day-48)
2$
cd day-48
Change working directory to day-48
3$
git checkout -b feature/day-48-intersection-observer
4$
git add index.html
5$
git commit -m "feat(day-48): implement intersection observer api for high-performance infinite scrolling catalog"
Commit staged changes with a descriptive message
6$
git push origin feature/day-48-intersection-observer
Push local commits directly to your remote GitHub repository
⌨️ Paste into terminal:Ctrl + V✨ Word wrap active (no horizontal cutoff)
Completion Prerequisites VerificationIncomplete Steps
Google Login
✔ Required
VS Code Implementation
Step 4 Viva Check
Own GitHub Project
Required

Exact GitHub Setup Guide & Expected Structure

Day 48 Requirements

Follow these exact folder and file names to prevent verification errors.

1. GitHub Repository
html-css-projects
Must be set to Public
2. Exact FolderNEW
day-48/ or bca-day-48/
Create inside repo root
3. Exact File NameREQUIRED
index.html
⚠️ No spaces or custom names
4. What to Submit Below
https://github.com/YOU/html-css-projects
Or direct link to index.html
Terminal Commands to Create & Push Day 48
1$
# 1. Enter your repository directory
2$
cd html-css-projects
Change working directory to html-css-projects
3$
# 2. Create the exact folder for Day 48
4$
mkdir day-48
Create a new project folder (day-48)
5$
cd day-48
Change working directory to day-48
6$
# 3. Create your code file: index.html
7$
# (Write your Day 48 homework code inside index.html)
8$
# 4. Save, stage, commit and push to GitHub:
9$
git add .
Stage all modified files and new assets for commit
10$
git commit -m "Day 48: Modern Web Performance & Intersection Observer API (Lazy Loading & Infinite Scroll)"
Commit staged changes with a descriptive message
11$
git push origin main
Push local commits directly to your remote GitHub repository
⌨️ Paste into terminal:Ctrl + V✨ Word wrap active (no horizontal cutoff)
How to Avoid "Could not find your practical code file" Errors:
1.Use Lowercase index.html: Avoid spaces or custom names like Alan Turing..html or Index.HTML. Linux/GitHub servers are strictly case-sensitive.
2.Separate Day Folders: Create a new folder day-48/ for today so you don't overwrite previous days.
3.Ensure Public Visibility: If your GitHub repo is Private, automated validation cannot inspect your code. Set it to Public.
4.Check on GitHub Website: Before clicking "Verify", open your repo in Google Chrome to confirm your files were pushed to branch main.
📁 github.com/YOUR_USERNAME/html-css-projects
Branch: main
README.md# BCA Practical Portfolio overview
day-01/Day 1 Prior Lesson
└──index.html
day-02/Day 2 Prior Lesson
└──index.html
day-03/Day 3 Prior Lesson
└──index.html
day-04/Day 4 Prior Lesson
└──index.html
day-05/Day 5 Prior Lesson
└──index.html
day-06/Day 6 Prior Lesson
└──index.html
day-07/Day 7 Prior Lesson
└──index.html
day-08/Day 8 Prior Lesson
└──index.html
day-09/Day 9 Prior Lesson
└──index.html
day-10/Day 10 Prior Lesson
└──index.html
└──styles.css
day-11/Day 11 Prior Lesson
└──index.html
day-12/Day 12 Prior Lesson
└──index.html
day-13/Day 13 Prior Lesson
└──index.html
day-14/Day 14 Prior Lesson
└──index.html
day-15/Day 15 Prior Lesson
└──index.html
day-16/Day 16 Prior Lesson
└──index.html
day-17/Day 17 Prior Lesson
└──index.html
day-18/Day 18 Prior Lesson
└──index.html
day-19/Day 19 Prior Lesson
└──index.html
day-20/Day 20 Prior Lesson
└──index.html
└──styles.css
day-21/Day 21 Prior Lesson
└──index.html
day-22/Day 22 Prior Lesson
└──index.html
day-23/Day 23 Prior Lesson
└──index.html
day-24/Day 24 Prior Lesson
└──index.html
day-25/Day 25 Prior Lesson
└──index.html
day-26/Day 26 Prior Lesson
└──index.html
day-27/Day 27 Prior Lesson
└──index.html
day-28/Day 28 Prior Lesson
└──index.html
day-29/Day 29 Prior Lesson
└──index.html
day-30/Day 30 Prior Lesson
└──index.html
└──styles.css
day-31/Day 31 Prior Lesson
└──index.html
day-32/Day 32 Prior Lesson
└──index.html
day-33/Day 33 Prior Lesson
└──index.html
day-34/Day 34 Prior Lesson
└──index.html
day-35/Day 35 Prior Lesson
└──index.html
day-36/Day 36 Prior Lesson
└──index.html
day-37/Day 37 Prior Lesson
└──index.html
day-38/Day 38 Prior Lesson
└──index.html
day-39/Day 39 Prior Lesson
└──index.html
day-40/Day 40 Prior Lesson
└──index.html
day-41/Day 41 Prior Lesson
└──index.html
day-42/Day 42 Prior Lesson
└──index.html
day-43/Day 43 Prior Lesson
└──index.html
day-44/Day 44 Prior Lesson
└──index.html
day-45/Day 45 Prior Lesson
└──index.html
day-46/Day 46 Prior Lesson
└──index.html
day-47/Day 47 Prior Lesson
└──index.html
day-48/✨ CREATE THIS FOLDER FOR TODAY (Day 48)
└──index.htmlMust contain:
Sequential Progression Enforced

You are welcome to read all notes, inspect code snippets, and practice all exercises of Day 48 freely! To officially submit your GitHub repository and unlock subsequent days in your portfolio, please complete Day 47: Client-Side SPA Routing & Browser History API (pushState & popstate) first.

Complete Day 47 First
Curriculum & Practical Systems Authored by Er. Manoj Kumar
NextSem Academy • BCA Practical Learning Portal