mirror of
https://github.com/cotes2020/jekyll-theme-chirpy.git
synced 2025-12-20 14:42:46 +00:00
perf(core): replace lazysizes with browser-level lazy loading (#1267)
This commit is contained in:
31
_javascript/modules/components/img-loading.js
Normal file
31
_javascript/modules/components/img-loading.js
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Setting up image lazy loading and LQIP switching
|
||||
*/
|
||||
|
||||
export function loadImg() {
|
||||
const $images = $('main img[loading="lazy"]');
|
||||
const $lqip = $('main img[data-lqip="true"]');
|
||||
|
||||
if ($images.length > 0) {
|
||||
$images.on('load', function () {
|
||||
/* Stop shimmer when image loaded */
|
||||
$(this).parent().removeClass('shimmer');
|
||||
});
|
||||
|
||||
$images.each(function () {
|
||||
/* Images loaded from the browser cache do not trigger the 'load' event */
|
||||
if ($(this).prop('complete')) {
|
||||
$(this).parent().removeClass('shimmer');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if ($lqip.length > 0) {
|
||||
$lqip.each(function () {
|
||||
/* Switch LQIP with real image url */
|
||||
const dataSrc = $(this).attr('data-src');
|
||||
$(this).attr('src', encodeURI(dataSrc));
|
||||
$(this).removeAttr('data-src data-lqip');
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user