MeetCode
Home
Doubts
Why is my C function not returning the …
Solved
1
49
Ask Doubt
View All Doubts
Solvers Leaderboard
Tutorials
Quiz
All Tests
Leaderboard
My Tasks
User Login
Dark
Enable Notifications
Toggle navigation
Infinite Scroll Add Items
JavaScript
Medium
3 views
Back to Questions
View Answer
0
Previous
Lazy Load Images
Medium
P
Next
Resizable Box (Drag Handle)
Medium
N
Problem Description
You have
. When user scrolls near bottom (within 200px), append 10 new
Item N
. Keep count with variable start.
Input Format
No input.
Output Format
No output (DOM change).
Constraints
Assume #list exists and page is scrollable.
Official Solution
const list=document.getElementById('list');let start=1;const add=()=>{const frag=document.createDocumentFragment();for(let i=0;i<10;i++){const d=document.createElement('div');d.className='item';d.textContent='Item '+start++;frag.appendChild(d);}list.appendChild(frag);};add();let busy=false;const onScroll=()=>{if(busy)return;const near=(window.innerHeight+window.scrollY) >= (document.body.scrollHeight-200);if(!near)return;busy=true;requestAnimationFrame(()=>{add();busy=false;});};window.addEventListener('scroll',onScroll,{passive:true});
Please
login
to submit solutions and comments.
Solutions (0)
No solutions submitted yet. Be the first!
Discussion (0)
No comments yet. Start the discussion!
Prev
Next
Fullscreen Editor
0 lines • 0 chars
Esc close • Tab indent
No comments yet. Start the discussion!