Created
June 13, 2018 20:20
-
-
Save hankpillow/98eee6250d8cdca550cc4c5903c28c48 to your computer and use it in GitHub Desktop.
This file contains 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() { | |
var timer = 0; | |
var screenViewport = window.innerHeight; | |
var documentHeight = document.documentElement.scrollHeight; | |
var scrollPositions = { | |
0: 0, | |
25: parseInt((documentHeight / 100) * 25, 10), | |
50: parseInt((documentHeight / 100) * 50, 10), | |
75: parseInt((documentHeight / 100) * 75, 10), | |
100: parseInt(documentHeight - (screenViewport / 2), 10) | |
}; | |
var scrollTags = { | |
0: false, | |
25: false, | |
50: false, | |
75: false, | |
100: false | |
}; | |
window.addEventListener('scroll', function() { | |
clearTimeout(timer); | |
timer = setTimeout(function() { | |
calculatePageScroll(); | |
}, 300); | |
}); | |
function calculatePageScroll() { | |
var _height = documentHeight - screenViewport; | |
var _scrollTop = window.scrollY; | |
var _scrollPercent = parseInt((_scrollTop * 100) / _height); | |
trackScrollPercent(_scrollPercent); | |
} | |
function trackScrollPercent(tag_percent) { | |
for (var percent in scrollTags) { | |
if ((percent <= tag_percent) && (scrollTags[percent] === false)) { | |
if (scrollTags[percent] !== true) { | |
console.log('scroll:percentage', percent + 'p'); | |
} | |
scrollTags[percent] = true; | |
} | |
} | |
} | |
calculatePageScroll() | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment