Last active
June 22, 2022 16:56
-
-
Save tpage99/e63c279be092343f189af02bbcfc180f to your computer and use it in GitHub Desktop.
Using intersectionObserver to display an element when NOT in viewport
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
// setting up options for IntersectionObserver | |
// setting threshold to .9, in this instance, means when 90% of the element is out of view | |
let options = { | |
root: null, | |
threshold: [.9] | |
} | |
// function with ternary operator to check if threshold is met then display none but if not then update display | |
function showToATC(entryArray){ | |
let div = document.getElementById('scroll-to-atc'); | |
entryArray[0].isIntersecting ? div.style.display = 'none' : div.style.display = "block"; | |
} | |
// initialize the function | |
let observer = new IntersectionObserver(showToATC, options); | |
observer.observe(document.getElementById('atc-btn')); | |
// credit - handy video: https://www.youtube.com/watch?v=orP9zwnE6PA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment