-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Gallery carousel * fixed format * minor edit - js naming
- Loading branch information
Showing
2 changed files
with
54 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const carousel = document.querySelector("div.js-carousel") | ||
const leftArrow = document.querySelector("button.js-left-arrow") | ||
const rightArrow = document.querySelector("button.js-right-arrow") | ||
const images = document.querySelectorAll("img.js-carousel-image") | ||
const totalImages = Object.keys(images).length | ||
|
||
let currentIndex = 0 | ||
let prevIndex | ||
|
||
images.forEach((img) => (img.style = `width: calc(100% / ${totalImages})`)) | ||
|
||
leftArrow.addEventListener("click", () => { | ||
// Save current index as prevIndex | ||
prevIndex = currentIndex | ||
|
||
// Go back one image, if we're at the start, go to the last image | ||
currentIndex = (currentIndex - 1 + totalImages) % totalImages | ||
carousel.insertBefore(images[currentIndex], carousel.firstChild) | ||
}) | ||
|
||
rightArrow.addEventListener("click", () => { | ||
prevIndex = currentIndex | ||
|
||
// Go to the next image, if we're at the end, go to first image | ||
currentIndex = (currentIndex + 1) % totalImages | ||
carousel.appendChild(images[prevIndex]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters