Manage Carousel arrow rotation depending on the current slide

Right only when first and Left only when last
This commit is contained in:
2021-11-03 18:45:18 +01:00
parent 365b8f5390
commit 18f2e324c7
2 changed files with 29 additions and 13 deletions

View File

@@ -13,6 +13,8 @@
let carousel: EmblaCarouselType
let currentSlide = 0
let arrowDirection: string = null
$: isFirstSlide = currentSlide === 0
$: isLastSlide = currentSlide === slides.length - 1
/** Navigate to specific slide */
@@ -28,10 +30,19 @@
*
*/
/** Move arrow and define direction on mousemove */
const handleArrowMove = ({ offsetX, offsetY }: MouseEvent) => {
const handleArrowMove = (event: MouseEvent) => {
const { left, top, width } = carouselEl.getBoundingClientRect()
const offsetX = event.clientX - left
const offsetY = event.clientY - top
// Define direction
const { width } = carouselEl.getBoundingClientRect()
arrowDirection = offsetX < Math.round(width / 2) ? 'prev' : 'next'
if (isFirstSlide) {
arrowDirection = 'next'
} else if (isLastSlide) {
arrowDirection = 'prev'
} else {
arrowDirection = offsetX < Math.round(width / 2) ? 'prev' : 'next'
}
// Move arrow
arrowPosition.set({
@@ -70,15 +81,13 @@
})
</script>
<div class="carousel {$$props.class ? $$props.class : ''}">
<div class="carousel {$$props.class ? $$props.class : ''}"
on:mousemove={handleArrowMove}
on:click={handleArrowClick}
>
{#if slides.length}
<div class="carousel__viewport"
bind:this={carouselEl}
on:mousemove={throttle(handleArrowMove, 50)}
on:click={handleArrowClick}
>
<div class="carousel__slides"
>
<div class="carousel__viewport" bind:this={carouselEl}>
<div class="carousel__slides">
{#each slides as { id, alt }}
<Image
class="carousel__slide"
@@ -106,7 +115,7 @@
<span class="carousel__arrow" bind:this={arrowEl}
style="--x: {$arrowPosition.x}px; --y: {$arrowPosition.y}px;"
class:is-flipped={arrowDirection === 'prev'}
class:is-flipped={arrowDirection === 'prev' && !isFirstSlide || isLastSlide}
>
<svg width="29" height="32" viewBox="0 0 29 32" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M10.82 28.275a2.182 2.182 0 0 0 3.086 3.086l13.818-13.818a2.182 2.182 0 0 0 0-3.086L13.906.64a2.182 2.182 0 1 0-3.085 3.086l10.093 10.093H2.182a2.182 2.182 0 1 0 0 4.364h18.732L10.821 28.275Z" />