diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-01-05 15:22:30 +0000 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-01-05 20:47:50 +0000 |
commit | 0992ff2903e60f0a2c267dd024b0049bb7c5f1b7 (patch) | |
tree | 311872c9015b8aa18edb7bb4073e3291b95b6c29 /web/js | |
parent | caf1482e72bd3f6aff641fe5a6b79ae6e660e577 (diff) | |
parent | 931f31f331b8b2ab0b7751abdfd3da547acd6961 (diff) |
Merge branch '1907-stop-tile-load-on-mappage'
Diffstat (limited to 'web/js')
-rw-r--r-- | web/js/lazyload.js | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/web/js/lazyload.js b/web/js/lazyload.js new file mode 100644 index 000000000..770fc62d4 --- /dev/null +++ b/web/js/lazyload.js @@ -0,0 +1,31 @@ +// jshint esversion: 6 + +(function(){ + if (!('IntersectionObserver' in window)) { + return; + } + + // Now we're here, we can assume quite modern JavaScript! + + const observer = new IntersectionObserver(onIntersection, { + rootMargin: "50px 0px" + }); + + const images = document.querySelectorAll(".js-lazyload"); + images.forEach(image => { + observer.observe(image); + }); + + function onIntersection(entries, observer) { + entries.forEach(entry => { + if (entry.intersectionRatio > 0) { + // Loading the image is the only thing we care about, so can + // stop observing. + observer.unobserve(entry.target); + // Removing this class (which is suppressing background-image) + // will trigger the image load + entry.target.classList.remove('js-lazyload'); + } + }); + } +})(); |