Skip to content
Permalink
Browse files

feat: lazy loading images

  • Loading branch information...
movsb committed May 30, 2019
1 parent 8cc94c7 commit fc675b4062848dcb58b3cc8526f46fd51b8dc175
Showing with 35 additions and 1 deletion.
  1. +1 −1 themes/blog/statics/plugins/mathjax/mathjax.js
  2. +34 −0 themes/blog/statics/scripts/footer.js
@@ -15,7 +15,7 @@
.css('margin', '3px')
.html(html)[0];
MathJax.Hub.Typeset(wrap);
console.log('Typeset: ', e);
// console.log('Typeset: ', e);
$(e).replaceWith(wrap);
}
});
@@ -231,3 +231,37 @@
replace(img);
});
})();

// Lazy load images
(function() {
function setSrc(img) {
let src = img.getAttribute('data-src');
img.setAttribute('src', src);
return src;
}

let supportIntersectionObserver = !window.IntersectionObserver !== undefined;
let allDataSrcImgs = document.querySelectorAll('article img[data-src]');

if(!supportIntersectionObserver) {
console.warn("browser doesn't support IntersectionObserver");
allDataSrcImgs.forEach(img => {
setSrc(img);
})
return;
}

function onIntersection(entries, observer) {
entries.forEach(entry => {
if(entry.intersectionRatio != 0) {
let img = entry.target;
let src = setSrc(img);
observer.unobserve(img);
console.log("Lazy loading", src);
}
});
}

let observer = new IntersectionObserver(onIntersection);
allDataSrcImgs.forEach(img => { observer.observe(img); })
})();

0 comments on commit fc675b4

Please sign in to comment.
You can’t perform that action at this time.