Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Gallery, UX): added swipe animation for mobile users #960

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion src/components/Boxers/Gallery.astro
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const { id, name } = Astro.props
<img
loading="lazy"
decoding="async"
class="boxer-gallery-img z-10 h-full rounded object-contain object-center"
class="swipe-element boxer-gallery-img z-10 h-full rounded object-contain object-center"
alt={`Imagen de la galería de fotos de ${name}`}
src={`https://cdn.lavelada.dev/boxers/gallery/${id}/1.webp`}
/>
Expand Down Expand Up @@ -130,6 +130,67 @@ const { id, name } = Astro.props
next.click()
}

const swipeImage = (event: TouchEvent, swipeLeft: () => void, swipeRight: () => void) => {
const DECISION_THRESHOLD = 100 as const
const target = event.target as HTMLImageElement | null
if (target === null) return
let isSwiping = false
let pullDeltaX = 0

if (isSwiping) return
const isSwipeable = target.classList.contains("swipe-element")
if (!isSwipeable) return
target.style.transition = "none"
const initialX = event.touches[0].pageX

document.addEventListener("touchmove", onSwipe, { passive: true })
document.addEventListener("touchend", onSwipeEnd, { passive: true })

function onSwipe(event: TouchEvent) {
if (target === null) return
const currentX = event.touches[0].pageX
pullDeltaX = currentX - initialX
if (pullDeltaX === 0) return
isSwiping = true
target.style.transform = `translateX(${pullDeltaX}px)`
target.style.opacity = `${DECISION_THRESHOLD / Math.abs(pullDeltaX)}`
target.style.transition = "none"
}

function onSwipeEnd(_event: TouchEvent) {
if (target === null) return
document.removeEventListener("touchmove", onSwipe)
document.removeEventListener("touchend", onSwipeEnd)
document.removeEventListener("touchstart", (event) =>
swipeImage(event, swipeLeft, swipeRight)
)
const decision = Math.abs(pullDeltaX) >= DECISION_THRESHOLD
if (decision) {
const showNext = pullDeltaX > 0
target.style.transition = "none"
if (showNext) {
showNextImage()
} else {
showPrevImage()
}
} else {
target.style.transition = "transform 0.2s ease-in-out"
}
isSwiping = false
pullDeltaX = 0
target.style.transform = "none"
target.style.opacity = "100"
}
}

document.addEventListener(
"touchstart",
(event) => swipeImage(event, showPrevImage, showNextImage),
{
passive: true,
}
)

document.addEventListener("click", (event) => {
if (!$lightbox.open) return
const target = event.target as HTMLElement
Expand Down
Loading