Skip to content

Commit

Permalink
Added function to click through image bubbles and changes the active …
Browse files Browse the repository at this point in the history
…image based on the corresponding bubble
  • Loading branch information
ARaza-13 committed Jun 10, 2024
1 parent 54208f5 commit 24676ed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ export default class Controller {
previousArrow.addEventListener('click', Controller.goToPreviousImage);
}

static initBubbleButtons() {
const imageBubbles = document.querySelectorAll('[data-bubble]');

imageBubbles.forEach((imageBubble) =>
imageBubble.addEventListener('click', Controller.switchImageBubble)
);
}

static goToNextImage() {
const bubbles = Array.from(document.querySelectorAll('[data-bubble]'));
const images = Array.from(document.querySelectorAll('[data-image]'));
const activeIndex = images.findIndex((image) =>
image.classList.contains('active')
Expand All @@ -16,6 +25,7 @@ export default class Controller {
// if the "active" image was found
if (activeIndex !== -1) {
images[activeIndex].classList.remove('active');
bubbles[activeIndex].classList.remove('active');

// determine index of next item
let nextIndex = activeIndex + 1;
Expand All @@ -25,10 +35,12 @@ export default class Controller {
}

images[nextIndex].classList.add('active');
bubbles[nextIndex].classList.add('active');
}
}

static goToPreviousImage() {
const bubbles = Array.from(document.querySelectorAll('[data-bubble]'));
const images = Array.from(document.querySelectorAll('[data-image]'));
const activeIndex = images.findIndex((image) =>
image.classList.contains('active')
Expand All @@ -37,6 +49,7 @@ export default class Controller {
// if the "active" image was found
if (activeIndex !== -1) {
images[activeIndex].classList.remove('active');
bubbles[activeIndex].classList.remove('active');

// determine index of previous item
let previousIndex = activeIndex - 1;
Expand All @@ -46,6 +59,26 @@ export default class Controller {
}

images[previousIndex].classList.add('active');
bubbles[previousIndex].classList.add('active');
}
}

static switchImageBubble(e) {
const bubbleIndex = e.target.getAttribute('data-bubble');

const bubbles = Array.from(document.querySelectorAll('[data-bubble]'));
const images = Array.from(document.querySelectorAll('[data-image]'));
const activeIndex = images.findIndex((image) =>
image.classList.contains('active')
);

// if the "active" image was found
if (activeIndex !== -1) {
images[activeIndex].classList.remove('active');
bubbles[activeIndex].classList.remove('active');

images[bubbleIndex].classList.add('active');
bubbles[bubbleIndex].classList.add('active');
}
}
}
1 change: 1 addition & 0 deletions src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default class DOMManager {
Controller.initArrowButtons();
DOMManager.loadImageBubbles();
DOMManager.loadFirstBubble();
Controller.initBubbleButtons();
}

static loadImages() {
Expand Down

0 comments on commit 24676ed

Please sign in to comment.