Igor150195

lazyfunc

Apr 6th, 2021
553
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. grLazyFunc: function(selector, callback) {
  2.             var elem = selector;
  3.             var defaultMargin = '10px';
  4.            
  5.             let options = {
  6.                 rootMargin: defaultMargin
  7.             };
  8.            
  9.             const imageObserver = new IntersectionObserver((entries, imgObserver) => {
  10.                 entries.forEach((entry) => {
  11.                     if (entry.isIntersecting) {
  12.                         const lazyItem = entry.target // Текущий элемент
  13.                        
  14.                         if (callback) {
  15.                             callback();
  16.                         };
  17.                        
  18.                         imgObserver.unobserve(lazyItem);
  19.                     }
  20.                 })
  21.             }, options);
  22.            
  23.             if (typeof selector == 'string') {
  24.                 const arr = document.querySelectorAll(elem);
  25.                 arr.forEach((v) => {
  26.                     imageObserver.observe(v);
  27.                 });
  28.             } else {
  29.                 imageObserver.observe(elem);
  30.             };
  31.         },
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×