Skip to content

Commit

Permalink
343 set up gallery (#432)
Browse files Browse the repository at this point in the history
* Gallery carousel

* fixed format

* minor edit - js naming
  • Loading branch information
Motormary authored Apr 11, 2024
1 parent df1ec13 commit e861918
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 43 deletions.
27 changes: 27 additions & 0 deletions js/gallery-slider.mjs
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])
})
70 changes: 27 additions & 43 deletions pages/gallery-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<script type="module" src="../js/gallery-slider.mjs"></script>
</head>
<body>
<header class="flex h-24 items-center justify-between px-10 mt-6">
Expand Down Expand Up @@ -177,48 +178,31 @@ <h2 id="search-label" class="sr-only">Search Bar</h2>
<!-- Section one starts here -->
<section>
<h1 class="text-center text-[150px] font-justAnotherHand">Gallery</h1>
<div class="relative">
<!-- Add JS - function to make slider move between images when clicking previous/next buttons, make active/current image larger than the others -->
<button
class="prev-btn absolute text-4xl top-[45%] left-52"
title="previous image"
aria-label="previous"
>
<i class="fa-solid fa-angle-left text-sunset-red"></i>
</button>
<button
class="next-btn absolute text-4xl top-[45%] right-52"
title="next image"
aria-label="next"
>
<i class="fa-solid fa-angle-right text-sunset-red"></i>
</button>
<div class="flex justify-center gap-2">
<img
class="h-[250px]"
src="../assets/placeholder-images/3baa531a04b524d4090032444534fad9.webp"
alt=""
/>
<img
class="h-[250px]"
src="../assets/placeholder-images/IMG_7424.webp"
alt=""
/>
<img
class="h-[250px]"
src="../assets/placeholder-images/IMG_4202.webp"
alt=""
/>
<img
class="h-[250px]"
src="../assets/placeholder-images/IMG_6433.webp"
alt=""
/>
<img
class="h-[250px]"
src="../assets/placeholder-images/IMG_6437.webp"
alt=""
/>

<div class="flex items-center justify-center">
<div class="relative overflow-hidden flex justify-center w-[1500px]">
<!-- If number of images in carousel is changed, the nth-child selector has to be updated -->
<div
class="js-carousel flex items-center justify-center w-[1400px] h-[300px] gap-5 overflow-hidden [&>img]:object-center [&>img]:object-cover max-sm:[&>*:not(:nth-child(3))]:hidden [&>*:nth-child(3)]:!h-full [&>*:nth-child(3)]:!w-[400px]">
<img class="js-carousel-image h-[220px]" src="../assets/placeholder-images/3baa531a04b524d4090032444534fad9.webp"
alt="" />
<img class="js-carousel-image h-[220px]" src="../assets/placeholder-images/IMG_7424.webp" alt="" />
<img class="js-carousel-image h-[220px]" src="../assets/placeholder-images/IMG_4202.webp" alt="" />
<img class="js-carousel-image h-[220px]" src="../assets/placeholder-images/IMG_6433.webp" alt="" />
<img class="js-carousel-image h-[220px]" src="../assets/placeholder-images/IMG_6437.webp" alt="" />
</div>
<!-- <div class="absolute bg-gradient-to-r via-white from-white top-[50%] translate-y-[-50%] left-0 w-60 h-full"></div> -->
<!-- <div class="absolute bg-gradient-to-l via-white from-white top-[50%] translate-y-[-50%] right-0 w-60 h-full"></div> -->
<button
class="arrow-button bg-white/50 rounded-full w-[3rem] js-left-arrow absolute text-4xl top-[50%] translate-y-[-50%] left-0"
title="previous image" aria-label="previous">
<i class="fa-solid fa-angle-left text-sunset-red"></i>
</button>
<button
class="arrow-button bg-white/50 rounded-full w-[3rem] js-right-arrow absolute text-4xl top-[50%] translate-y-[-50%] right-0"
title="next image" aria-label="next">
<i class="fa-solid fa-angle-right text-sunset-red"></i>
</button>
</div>
</div>
<p class="text-center text-3xl py-10 max-w-[1400px] m-auto">
Expand Down Expand Up @@ -501,4 +485,4 @@ <h1 class="text-2xl font-semibold mb-6">
</footer>
<!--Footer end-->
</body>
</html>
</html>

0 comments on commit e861918

Please sign in to comment.