aboutsummaryrefslogtreecommitdiffstats
path: root/web/js
diff options
context:
space:
mode:
authorMatthew Somerville <matthew-github@dracos.co.uk>2018-01-03 18:13:11 +0000
committerMatthew Somerville <matthew-github@dracos.co.uk>2018-01-05 14:50:49 +0000
commitee0453b094c8168f43adc900e4510181dc61ac77 (patch)
treeba74cfdc81d187844455279fc9bfa10432f5c3e8 /web/js
parentb9ed5d04b3d04ec40973147c4da26755c6518337 (diff)
[fixmystreet.com] Lazy load images in the footer.
This uses an IntersectionObserver, which currently works in Chrome, Edge, and Firefox.
Diffstat (limited to 'web/js')
-rw-r--r--web/js/lazyload.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/web/js/lazyload.js b/web/js/lazyload.js
new file mode 100644
index 000000000..50cc8c46e
--- /dev/null
+++ b/web/js/lazyload.js
@@ -0,0 +1,29 @@
+(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');
+ }
+ });
+ }
+})();