Last active
June 22, 2022 16:57
-
-
Save tpage99/08efebd0df3b8a2278b50a3b1f225c9c to your computer and use it in GitHub Desktop.
An example of how to load a script on scroll in Shopify
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
<script type="text/javascript"> | |
window.addEventListener('scroll', scrollyScroll, false); | |
function scrollyScroll() { | |
let scrollPosition = 0; | |
scrollPosition = window.scrollY; | |
if (scrollPosition > 10) { | |
(function() { | |
var tidScript = document.createElement('script'); | |
tidScript.type = 'text/javascript'; | |
tidScript.async = true; | |
tidScript.src = 'https://scriptGoesHere'; | |
(document.getElementsByTagName('body')[0]).appendChild(tidScript); | |
})(); | |
stopIt(); | |
}}; | |
function stopIt (){ | |
window.removeEventListener('scroll', scrollyScroll) | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
previous iterations didn't properly apply the listener event. had to update to remove, otherwise was firing over and over again. won't impact performance on speed tests, but likely could have caused user errors from firing repetitively.