Skip to content

Commit

Permalink
Update gallery.js
Browse files Browse the repository at this point in the history
  • Loading branch information
valentyna88 committed Jul 26, 2024
1 parent 13cada5 commit e231f17
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions js/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,41 @@ const images = [
</li>
*/

const createImgGalleryTemplate = (image) => {
return `
const galleryContainer = document.querySelector(".gallery");

const createGalleryItemsMarkup = (images) => {
return images
.map(
({ preview, original, description }) => `
<li class="gallery-item">
<a class="gallery-link" href="${image.original}">
<a class="gallery-link" href="${original}">
<img
class="gallery-image"
src="${image.preview}"
data-source="${image.original}"
alt="${image.description}"
src="${preview}"
data-source="${original}"
alt="${description}"
/>
</a>
</li>
`;
`
)
.join("");
};

const imageTemplate = images
.map((imageInfo) => createImgGalleryTemplate(imageInfo))
.join("");
galleryContainer.innerHTML = createGalleryItemsMarkup(images);

const onGalleryContainerClick = (event) => {
event.preventDefault();

const target = event.target;
if (target.nodeName !== "IMG") return;

const imagesGalleryEl = document.querySelector(".gallery");
const largeImageURL = target.dataset.source;
const instance = basicLightbox.create(
`<img src="${largeImageURL}" alt="${target.alt}">`
);

instance.show();
};

imagesGalleryEl.innerHTML = imageTemplate;
galleryContainer.addEventListener("click", onGalleryContainerClick);

0 comments on commit e231f17

Please sign in to comment.