Skip to content

Commit

Permalink
Refactor: Move loadPictureSources() function into imageCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
astik-dev committed Apr 19, 2024
1 parent 162b174 commit bdac287
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
18 changes: 7 additions & 11 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ function toggleBurgerMenu() {
}


function loadPictureSources (picture) {
picture.querySelectorAll("source").forEach(source => {
source.srcset = source.dataset.src;
});
const pictureImg = picture.querySelector("img");
pictureImg.src = pictureImg.dataset.src;
}


function openCloseProjectPopup(eventTarget) {
if (eventTarget && eventTarget.classList.contains("projects__item_empty")) return;
Expand Down Expand Up @@ -150,7 +142,7 @@ function openCloseProjectPopup(eventTarget) {
setTimeout(() => {
// Load first two (if available) images
dqsa(".project-popup__image-slide:nth-child(-n+2) picture").forEach((slidePic, index) => {
loadPictureSources(slidePic);
imageCreator.loadPictureSources(slidePic);
if (index == 0) {
const picImg = slidePic.querySelector("img");
picImg.addEventListener("load", () => showScrollAnimation(picImg));
Expand Down Expand Up @@ -191,7 +183,9 @@ const swiperProjectPopup = new Swiper('.project-popup__image-swiper', {
if (nextNextPic) {
setTimeout(() => {
const picImg = nextNextPic.querySelector("img");
if (picImg.src != imageCreator.px1) loadPictureSources(nextNextPic);
if (picImg.src != imageCreator.px1) {
imageCreator.loadPictureSources(nextNextPic);
}
}, 300); // 300 - Default duration of transition between slides (in ms)
}
},
Expand Down Expand Up @@ -282,7 +276,9 @@ function generateProjects(mode) {
const newObserver = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) loadPictureSources(entry.target);
if (entry.isIntersecting) {
imageCreator.loadPictureSources(entry.target);
}
});
},
{
Expand Down
10 changes: 9 additions & 1 deletion src/js/modules/imageCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ const imageCreator = {
const fullPath = this.fullPath(source, path);
const src = lazy ? `"${this.px1}" data-src="${fullPath}"` : `"${fullPath}"`;
return `<img src=${src} alt="${alt}">`;
}
},

loadPictureSources: function (picture) {
picture.querySelectorAll("source").forEach(source => {
source.srcset = source.dataset.src;
});
const pictureImg = picture.querySelector("img");
pictureImg.src = pictureImg.dataset.src;
},
}

export default imageCreator;

0 comments on commit bdac287

Please sign in to comment.