Skip to content

Instantly share code, notes, and snippets.

@johnmurch
Created June 16, 2023 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnmurch/14378029eb782bd9b4c2341a41bea71b to your computer and use it in GitHub Desktop.
Save johnmurch/14378029eb782bd9b4c2341a41bea71b to your computer and use it in GitHub Desktop.
Page Scroll depth tracking
function trackScrollDepth() {
const scrollPercentages = [25, 50, 75, 100];
window.addEventListener('scroll', () => {
const scrollHeight = document.documentElement.scrollHeight - window.innerHeight;
const scrollPosition = window.scrollY;
const scrollRatio = (scrollPosition / scrollHeight) * 100;
if (scrollPercentages.includes(Math.floor(scrollRatio))) {
// Perform scroll depth tracking actions
console.log(`Scrolled to ${Math.floor(scrollRatio)}%`);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment