ReferenceError on lazy

Viewed 52

I'm trying to add this simple image lazyload in my wordpress site because looks better and smaller than other js scripts but Im getting Uncaught ReferenceError: _ is not defined at window.addEventListener('scroll', _.throttle(lazyLoad, 16));

The full js code:

let lazyImages = [...document.querySelectorAll('.lazy')];
let inAdvance = 300;

function lazyLoad() {
  lazyImages.forEach(image => {
    if (image.offsetTop < window.innerHeight + window.pageYOffset + inAdvance) {
      image.src = image.dataset.src;
      image.onload = () => image.classList.add('loaded');
    }
  });

  // if all loaded removeEventListener
}

lazyLoad();

window.addEventListener('scroll', _.throttle(lazyLoad, 16));
window.addEventListener('resize', _.throttle(lazyLoad, 16));
0 Answers
Related