Last active
November 6, 2019 07:41
-
-
Save prashantsani/15cefdce2327cb4a02f6817a6637233e to your computer and use it in GitHub Desktop.
JavaScript IntersectionObserver Utility Function - Trigger When Element in View
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
// View Comment section for explanation | |
function _$(elem){ return d.querySelectorAll(elem) } | |
function observer(trigger, func_vis, func_hidden, threshold){ | |
var t = threshold ? threshold : 1, | |
observable_var; | |
observable_var = new IntersectionObserver( | |
entries => { | |
if (entries[0].isIntersecting){ | |
func_vis() | |
} | |
else{ | |
func_hidden() | |
} | |
}, | |
{ | |
threshold: t | |
} | |
).observe(_$(trigger)[0]); | |
return observable_var; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Explanation:
.hero
element is visible, then it will triggeralert('visible')
alert('hidden')