Created
March 23, 2024 20:17
-
-
Save piyushsinghania/be3a969ec8cc56efc37df25c737b38da to your computer and use it in GitHub Desktop.
Implementing reverse infinite scroll with html, css and javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const list = document.getElementById('list'); | |
const items = document.getElementsByClassName('item'); | |
const generateElements = (lastItem) => { | |
const number = +lastItem.innerHTML; | |
const elements = []; | |
for (let i = 1; i <= 9; i++) { | |
const element = document.createElement('div'); | |
element.classList.add('item'); | |
element.innerHTML = number + i; | |
elements.push(element); | |
} | |
return elements; | |
}; | |
const isElementInScrollView = (el, parent) => { | |
const rect = el.getBoundingClientRect(); | |
const parentRect = parent.getBoundingClientRect(); | |
return ( | |
rect.top >= parentRect.top && | |
rect.left >= parentRect.left && | |
rect.bottom <= parentRect.bottom && | |
rect.right <= parentRect.right | |
); | |
}; | |
list.addEventListener('scroll', () => { | |
const lastItem = document.querySelector('.item:last-child'); | |
if (isElementInScrollView(lastItem, list)) { | |
const newElements = generateElements(lastItem); | |
list.append(...newElements); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Reverse Scroll</title> | |
<link rel="stylesheet" href="styles.css" /> | |
</head> | |
<body> | |
<div id="list"> | |
<div class="item">1</div> | |
<div class="item">2</div> | |
<div class="item">3</div> | |
<div class="item">4</div> | |
<div class="item">5</div> | |
<div class="item">6</div> | |
<div class="item">7</div> | |
<div class="item">8</div> | |
<div class="item">9</div> | |
</div> | |
<script src="app.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#list { | |
border: 1px solid black; | |
background-color: antiquewhite; | |
height: 250px; | |
overflow-y: scroll; | |
display: flex; | |
flex-direction: column-reverse; | |
} | |
.item { | |
flex: 1; | |
padding: 2em; | |
border: 1px dashed green; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment