From 8a4503c925f60709287ce6f39545583f7c48d747 Mon Sep 17 00:00:00 2001 From: ray Date: Thu, 21 May 2020 16:10:37 +0100 Subject: [PATCH] add onscreen function --- src/js/index.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/js/index.js b/src/js/index.js index 7d4cebb..59c5909 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -1,5 +1,22 @@ (function() { - window.addEventListener("DOMContentLoaded", event => { - // TODO - }); + window.addEventListener('DOMContentLoaded', event => { + function onScreenHandler(entries) { + entries.forEach(entry => { + if (entry.intersectionRatio > 0) { + entry.target.classList.add('is-showing', 'has-shown'); + } else { + entry.target.classList.remove('is-showing'); + } + }); + } + + document.querySelectorAll('.js-onscreen').forEach(el => { + const options = { + rootMargin: '0px', + threshold: [0, 1], + }; + const observer = new IntersectionObserver(onScreenHandler, options); + observer.observe(el); + }); + }); })();