diff --git a/src/organisms/Carousel.svelte b/src/organisms/Carousel.svelte index cb23f82..0ebc464 100644 --- a/src/organisms/Carousel.svelte +++ b/src/organisms/Carousel.svelte @@ -2,15 +2,75 @@ import { onMount } from 'svelte' import * as fn from '../functions' + // Dependencies + import dayjs from 'dayjs' + import advancedFormat from 'dayjs/plugin/advancedFormat' + dayjs.extend(advancedFormat) + // Components import IconArrow from '../atoms/IconArrow' - // Props and variables + // Props export let viewer = false export let photos = [] + + // Variables let hover - let photoPrev - let photoNext + let currentIndex = 1 + $: currentPhoto = photos[currentIndex] || null + $: prevPhoto = photos[currentIndex - 1] || photos[photos.length - 1] + $: nextPhoto = photos[currentIndex + 1] || photos[0] + + + /* + ** Go to a photo + */ + const goToPrev = () => { + currentIndex-- + currentIndex = (currentIndex < 0) ? photos.length - 1 : currentIndex + } + const goToNext = () => { + currentIndex++ + currentIndex = (currentIndex >= photos.length) ? 0 : currentIndex + } + + + /* + ** Drag and touch navigation + */ + let touchStartX = 0 + let touchStartY = 0 + let touchEndX = 0 + let touchEndY = 0 + + const swipeStart = event => { + touchStartX = event.changedTouches[0].screenX + touchStartY = event.changedTouches[0].screenY + } + const swipeEnd = event => { + touchEndX = event.changedTouches[0].screenX + touchEndY = event.changedTouches[0].screenY + if (touchEndX <= touchStartX) goToNext() + if (touchEndX >= touchStartX) goToPrev() + } + + // Keyboard navigation + const keyboardNav = event => { + const keyCode = event.keyCode + + switch (event.keyCode) { + case 37: case 80: case 74: + goToPrev() + break + case 39: case 78: case 75: + goToNext() + break + // case 27: case 67: + // document.getElementById('photo_close').click() + // break + default: break + } + } /* @@ -19,10 +79,15 @@ onMount(() => { // Hover function hover = event => { - const to = event.currentTarget.dataset.to const button = event.currentTarget.querySelector('button') - const photoActive = document.querySelector('.gallery .active') - const photoToHover = (to === 'prev') ? photoActive.previousSibling : photoActive.nextSibling + const photoActive = document.querySelector('.gallery__images .active') + + let photoToHover + if (event.currentTarget.dataset.to === 'prev') { + photoToHover = (photoActive.previousSibling != null) ? photoActive.previousSibling : photoActive.parentNode.lastChild + } else { + photoToHover = (photoActive.nextSibling != null) ? photoActive.nextSibling : photoActive.parentNode.firstChild + } switch (event.type) { case 'mouseenter': @@ -36,29 +101,30 @@ default: break } } - - // Go to previous photo - photoPrev = () => { - console.log('previous photo') - } - - // Go to next photo - photoNext = () => { - console.log('next photo') - } }) + /* + REVEAL ANIMATION ON CAROUSEL: + Fix the CSS priority? Cancel the transition delay and duration? + data-aos="{ + (photo === prevPhoto) ? 'carousel-prev' : + (photo === currentPhoto) ? 'carousel-active' : + (photo === nextPhoto) ? 'carousel-next' : '' + } + */ -