Advertisement
Igor150195

Отложенные скрипты блоков

Apr 13th, 2021
1,002
0
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.             },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement