Created
June 16, 2023 14:15
-
-
Save johnmurch/14378029eb782bd9b4c2341a41bea71b to your computer and use it in GitHub Desktop.
Page Scroll depth tracking
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
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