Skip to content

Instantly share code, notes, and snippets.

@tpage99
Last active June 22, 2022 16:56
Show Gist options
  • Save tpage99/e63c279be092343f189af02bbcfc180f to your computer and use it in GitHub Desktop.
Save tpage99/e63c279be092343f189af02bbcfc180f to your computer and use it in GitHub Desktop.
Using intersectionObserver to display an element when NOT in viewport
// 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