Skip to content

Instantly share code, notes, and snippets.

@sahilatahar
Last active June 15, 2024 09:13
Show Gist options
  • Save sahilatahar/613739397ff09fd92ab7fecbe781c8f6 to your computer and use it in GitHub Desktop.
Save sahilatahar/613739397ff09fd92ab7fecbe781c8f6 to your computer and use it in GitHub Desktop.
Implementing Scrolling/Infinite Scrolling to Dynamically Load Data on Webpages or Containers
// Scroll to end of container
// If you have id of container then replace #id with your container id (e.g. #imagesContainer)
// If you have class of container then replace #id with your container class (e.g. .imagesContainer)
const container = document.querySelector("#id")
const scrollToEnd = () => {
container.scrollIntoView({
// behavior: "smooth",
block: "end",
// inline: "end",
})
}
// Call this function to scroll to end of container only once
scrollToEnd()
// Scroll to end of container after every 2 seconds uncomment below code and comment above scrollToEnd() function call
// const interval = setInterval(() => {
// scrollToEnd()
// }, 2000) // Set time according to your need
// Call stopScrolling function to stop scrolling
// const stopScrolling = () => {
// clearInterval(interval)
// }
// Scroll to end of webpage only once
window.scrollTo(0, document.body.scrollHeight);
// Scroll to end of webpage after every 2 seconds uncomment below code and comment above code
// const interval = setInterval(() => {
// window.scrollTo(0, document.body.scrollHeight);
// }, 2000) // Set time according to your need
// Call stopScrolling function to stop scrolling
// const stopScrolling = () => {
// clearInterval(interval)
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment