Skip to content
the poor coder

onScrollBottom

Poor Man
Published
Reading time
2 min read
On this page

What is onScrollBottom?#

onScrollBottom is an event that fires when the user scrolls to the bottom of a web page or element.

How to detect onScrollBottom?#

To detect onScrollBottom, we can use JavaScript and add an event listener to the window or element.

Text

window.addEventListener('scroll', function() {
  if (window.innerHeight + window.pageYOffset >= document.body.offsetHeight) {
    // User has scrolled to the bottom
    console.log('onScrollBottom');
  }
});

The code above listens for the 'scroll' event on the window and checks if the sum of the current visible area and the current scroll position is greater than or equal to the total height of the body. If it is, then the user has scrolled to the bottom and 'onScrollBottom' is logged to the console.

We can also use jQuery to detect onScrollBottom:

Text

$(window).scroll(function() {
  if($(window).scrollTop() + $(window).height() == $(document).height()) {
    // User has scrolled to the bottom
    console.log('onScrollBottom');
  }
});

The code above listens for the 'scroll' event on the window and checks if the sum of the current scroll position and the window height equals the total height of the document. If it does, then the user has scrolled to the bottom and 'onScrollBottom' is logged to the console.

What can we do with onScrollBottom?#

With onScrollBottom, we can load more content dynamically as the user scrolls down the page. This technique is called infinite scrolling and is commonly used in social media and e-commerce websites.

Text

var page = 1;

window.addEventListener('scroll', function() {
  if (window.innerHeight + window.pageYOffset >= document.body.offsetHeight) {
    // User has scrolled to the bottom
    page++;
    loadMoreContent(page);
  }
});

function loadMoreContent(page) {
  // Make an AJAX request to load more content
  // Append the new content to the page
}

The code above listens for the 'scroll' event on the window and calls the 'loadMoreContent' function when the user scrolls to the bottom. The 'loadMoreContent' function makes an AJAX request to load more content and appends it to the page.

With onScrollBottom, we can also show a button or link to load more content instead of loading it automatically. This gives the user more control and prevents excessive server requests.

Text

var page = 1;
var loadMoreButton = document.getElementById('load-more-button');

loadMoreButton.addEventListener('click', function() {
  page++;
  loadMoreContent(page);
});

window.addEventListener('scroll', function() {
  if (window.innerHeight + window.pageYOffset >= document.body.offsetHeight) {
    // User has scrolled to the bottom
    loadMoreButton.style.display = 'block';
  } else {
    loadMoreButton.style.display = 'none';
  }
});

function loadMoreContent(page) {
  // Make an AJAX request to load more content
  // Append the new content to the page
}

The code above shows a 'Load More' button that calls the 'loadMoreContent' function when clicked. It also listens for the 'scroll' event on the window and shows or hides the button depending on whether the user has scrolled to the bottom or not.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC