Observse intersection of a DOM element and viewport
It is not easy to know if specified DOM element is in viewport of current browser screen. This module observes element and fire show/hide event when the element intersects viewport. This implementation uses getBoundingClientRect() to inspect element position, so may not work fine on old browsers.
npm install viewport-intersection-observer
var observer = new ViewportIntersectionObserver();
// add elements to observe and its show/hide handler
observer.addListener(target1, {
show: function() {
console.log("target1 show");
},
hide: function() {
console.log("target1 hide");
}
});
observer.addListener(target2, {
show: function() {
console.log("target2 show");
},
hide: function() {
console.log("target2 hide");
}
});
observer.observe();
// You can set throttle setting
observer.observe({ throttle: 10 });
// You need to call observe() to check status of
// registered elements. It is NOT automatic.
window.addEventListener('scroll', function() {
observer.observe();
});
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
MIT