Ah, triggering an event after scrolling a certain distance down the page is a pretty common task. You can use the window.scrollY
and document.documentElement.scrollHeight
properties along with the scroll
event to achieve this in JavaScript.
Here’s some sample code to get you started:
// Add event listener for the scroll event
window.addEventListener('scroll', function() {
// Calculate the scroll percentage
const scrollHeight = document.documentElement.scrollHeight - window.innerHeight;
const scrollPosition = window.scrollY;
const scrollPercentage = (scrollPosition / scrollHeight) * 100;
// Check if scrolled more than 75%
if (scrollPercentage >= 75) {
// Trigger click or any other action here
document.querySelector('#yourElementId').click();
}
});
In this example, replace #yourElementId
with the actual ID of the element you want to click. This will trigger a click event on that element when the user scrolls 75% or more down the page.
Just drop this code into your JS file, and you should be good to go!
