Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- grLazyLoad: function(params) {
- var elem = 'gr_images_lazy_load';
- var defaultMargin = '10px';
- var initMargin = '500px';
- if (params) {
- if (params.selector) {
- elem = params.selector;
- };
- if (params.margin) {
- defaultMargin = params.margin;
- };
- if (params.initMargin) {
- initMargin = params.initMargin;
- };
- };
- var rootMarginVal = defaultMargin;
- /*if (shop2.mode == 'main') {
- if (readCookie('rootMarginCookie')) {
- var rootMarginVal = initMargin;
- } else {
- var rootMarginVal = defaultMargin;
- }
- createCookie('rootMarginCookie', 1, 1);
- };*/
- let options = {
- rootMargin: rootMarginVal
- };
- shop2_gr.settings.imageObserver = new IntersectionObserver((entries, imgObserver) => {
- entries.forEach((entry) => {
- if (entry.isIntersecting) {
- const lazyImage = entry.target // Текущий элемент
- if (lazyImage.tagName == 'IMG') { // для обычных картинок
- if (lazyImage.dataset.srcset) {
- // Чтобы загружать картинки маленьких размеров с помощью srcset
- lazyImage.srcset = lazyImage.dataset.srcset // Адрес картинки data-srcset=""
- lazyImage.classList.remove(elem);
- imgObserver.unobserve(lazyImage);
- } else {
- lazyImage.src = lazyImage.dataset.src // Адрес картинки data-src=""
- lazyImage.classList.remove(elem);
- imgObserver.unobserve(lazyImage);
- }
- } else if (lazyImage.dataset.bg) {
- if (window.innerWidth <= 768 && lazyImage.dataset.minbg) {
- lazyImage.style.backgroundImage = 'url(' + lazyImage.dataset.minbg + ')';
- lazyImage.classList.remove(elem);
- imgObserver.unobserve(lazyImage);
- } else {
- lazyImage.style.backgroundImage = 'url(' + lazyImage.dataset.bg + ')';
- lazyImage.classList.remove(elem);
- imgObserver.unobserve(lazyImage);
- }
- } else if (lazyImage.dataset.func) { // Если элемент содержит data-func
- if (typeof shop2_gr.methods[lazyImage.dataset.func] == 'function') {
- shop2_gr.methods[lazyImage.dataset.func](lazyImage); // Вызов функции
- lazyImage.classList.remove(elem);
- imgObserver.unobserve(lazyImage);
- }
- } else {
- lazyImage.classList.remove(elem);
- imgObserver.unobserve(lazyImage);
- }
- }
- })
- }, options);
- const arr = document.querySelectorAll('.' + elem);
- arr.forEach((v) => {
- shop2_gr.settings.imageObserver.observe(v);
- });
- },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement