Advertisement
Igor150195

Mason

Dec 5th, 2023
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. lpc_template.queue.masonGallery = function ($self) {
  2.         let $block = $self.find('.lpc-masonry-init');
  3.    
  4.         if ($block.length) {
  5.             $block.each(function () {
  6.                 let $this = $(this);
  7.                 let grid = $this.find(".lpc-image-mason__list");
  8.                 let allItems = $this.find(".lpc-image-mason__item");
  9.                 let lazyLoadItemCount = 8;
  10.    
  11.                 function resizeGridItem(item) {
  12.                     let rowHeight = parseInt(grid.css("grid-auto-rows"));
  13.                     let rowGap = parseInt(grid.css("grid-row-gap"));
  14.    
  15.                     let rowSpan = Math.ceil(
  16.                         (item.querySelector('.lpc-image-mason__image').getBoundingClientRect().height + rowGap) / (rowHeight + rowGap)
  17.                     );
  18.                     $(item).css("grid-row-end", "span " + rowSpan);
  19.    
  20.                     let image = $(item).find('.lpc-image-mason__image');
  21.                     image.css("height", "100%");
  22.                 }
  23.    
  24.                 function resizeAllGridItems() {
  25.                     allItems.each(function (index, element) {
  26.                         resizeGridItem(element);
  27.                     });
  28.                 }
  29.    
  30.                 /*function showLazyLoadedItems() {
  31.                     allItems.slice(0, lazyLoadItemCount).addClass('is-show'); // Показать первые 8 блоков
  32.                 }*/
  33.    
  34.                 /*function hideRemainingItems() {
  35.                     allItems.slice(lazyLoadItemCount).removeClass('is-show'); // Скрыть остальные блоки
  36.                 }*/
  37.  
  38.                 function showLazyLoadedItem(item) {
  39.                     $(item).addClass('is-show');
  40.                 }
  41.    
  42.                 // $(document).ready(function () {
  43.                 //  setTimeout(function () {
  44.                 //      imagesLoaded(grid[0], function () {
  45.                 //          resizeAllGridItems();
  46.                 //          showLazyLoadedItems(); // Показать первые 8 блоков после загрузки изображений
  47.                 //          hideRemainingItems(); // Скрыть остальные блоки
  48.                 //      });
  49.                 //  }, 500);
  50.                 // });
  51.                 const imageObserver = new IntersectionObserver((entries, imgObserver) => {
  52.                     entries.forEach((entry) => {
  53.                         if (entry.isIntersecting) {
  54.                             const lazyItem = entry.target;
  55.                             const img = lazyItem.querySelector('img[data-src]');
  56.                
  57.                             if (img) {
  58.                                 img.onload = function() {
  59.                                     imgObserver.unobserve(lazyItem);
  60.                                     resizeGridItem(lazyItem);
  61.                                     showLazyLoadedItem(lazyItem);
  62.                                 };
  63.                
  64.                                 img.src = img.dataset.src;
  65.                                 img.removeAttribute('data-src');
  66.                             }
  67.                         }
  68.                     });
  69.                 });
  70.                
  71.                 allItems.each(function (index, item) {
  72.                     imageObserver.observe(item);
  73.                 });
  74.                
  75.                 grid.addClass('after-grid-init');
  76.                
  77.                 /*const showBlocks = () => {
  78.                     const windowHeight = $(window).height();
  79.                     const windowTop = $(window).scrollTop();
  80.                     const windowBottom = windowTop + windowHeight;
  81.    
  82.                     allItems.each(function () {
  83.                         const itemTop = $(this).offset().top;
  84.                         const itemBottom = itemTop + $(this).outerHeight();
  85.    
  86.                         if ((itemTop >= windowTop && itemTop <= windowBottom) ||
  87.                             (itemBottom >= windowTop && itemBottom <= windowBottom)) {
  88.                             $(this).addClass('is-show');
  89.                         }
  90.                     });
  91.                 };*/
  92.    
  93.                 //showBlocks();
  94.    
  95.                 /*$(window).on('scroll', showBlocks);*/
  96.    
  97.                 /*if (!s3LP.is_cms) {
  98.                     let resizeTimeout;
  99.    
  100.                     $(window).on('resize', function () {
  101.                         if (resizeTimeout) {
  102.                             clearTimeout(resizeTimeout);
  103.                         }
  104.    
  105.                         resizeTimeout = setTimeout(function () {
  106.                             resizeAllGridItems();
  107.                         }, 50);
  108.                     });
  109.                 };*/
  110.             });
  111.         }
  112.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement