Last active
October 26, 2018 17:45
-
-
Save nandaleite/8e72eb5c7c2c38d292b2950807f0543d to your computer and use it in GitHub Desktop.
Detect when footer is visible
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
| function intersectionCallback() { | |
| console.log('target is visible!'); | |
| } | |
| window.addEventListener('load', function(){ | |
| var wrapper = document.querySelector('#wrapper'); | |
| var options = { | |
| root: null, | |
| rootMargin: '0px', | |
| threshold: 0 | |
| } | |
| var observer = new IntersectionObserver(intersectionCallback, options); | |
| var target = document.querySelector('#footer'); | |
| observer.observe(target); | |
| // Scroll to the starting position | |
| document.scrollingElement.scrollTop = wrapper.getBoundingClientRect().top + window.scrollY; | |
| }); | |
| // More details at https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment