Lazy-load images on scroll

Set the real src only when the image is close to the screen.

jQuery · Lazy-load images on scrollAdvanced
function lazyLoad() {
  const bottom = $(window).scrollTop() + $(window).height() + 200;
  $('img[data-src]').each(function () {
    if ($(this).offset().top < bottom) {
      $(this).attr('src', $(this).data('src')).removeAttr('data-src').addClass('loaded');
    }
  });
  if (!$('img[data-src]').length) $(window).off('scroll.lazy resize.lazy');
}

$(window).on('scroll.lazy resize.lazy', lazyLoad);
lazyLoad();
lazy loadimagesscrollperformance