File size: 4,676 Bytes
ef1ad9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<script>

  import { onMount, afterUpdate } from "svelte";

  import { tweened } from "svelte/motion";

  import { cubicOut } from "svelte/easing";



  export let cards = [];



  let currentIndex = 0;

  let container;

  let bullets = [];

  let isDragging = false;

  let startX = 0;

  let currentTranslate = 0;

  let prevTranslate = 0;

  let animationID;



  const x = tweened(0, {

    duration: 400,

    easing: cubicOut,

  });



  function handleTouchStart(event) {

    startX = event.type.includes("mouse")

      ? event.pageX

      : event.touches[0].clientX;

    isDragging = true;

    animationID = requestAnimationFrame(animation);

    container.classList.add("grabbing");

  }



  function handleTouchMove(event) {

    if (isDragging) {

      const currentPosition = event.type.includes("mouse")

        ? event.pageX

        : event.touches[0].clientX;

      currentTranslate = prevTranslate + currentPosition - startX;

      setSliderPosition();

    }

  }



  function handleTouchEnd() {

    cancelAnimationFrame(animationID);

    isDragging = false;

    container.classList.remove("grabbing");



    const movedBy = currentTranslate - prevTranslate;



    if (movedBy < -100 && currentIndex < cards.length - 1) {

      currentIndex += 1;

    } else if (movedBy > 100 && currentIndex > 0) {

      currentIndex -= 1;

    }



    currentIndex = Math.min(Math.max(currentIndex, 0), cards.length - 1);

    setPositionByIndex();

    updateBullets();

  }



  function setPositionByIndex() {

    currentTranslate = -currentIndex * container.offsetWidth;

    prevTranslate = currentTranslate;

    x.set(currentTranslate);

  }



  function animation() {

    if (isDragging) requestAnimationFrame(animation);

  }



  function setSliderPosition() {

    // Calculate translateX based on currentTranslate, not directly on container

    const translateX = `translateX(${currentTranslate}px)`;

    container.style.transform = translateX;

  }



  afterUpdate(() => {

    bullets = Array.from(container.querySelectorAll(".bullet"));

  });



  function goToSlide(index) {

    currentIndex = index;

    setPositionByIndex();

    updateBullets();

  }



  function updateBullets() {

    bullets.forEach((bullet, i) => {

      bullet.classList.toggle("active", i === currentIndex);

    });

  }



  $: {

    if (bullets.length) {

      goToSlide(currentIndex);

    }

  }

</script>

<div class="flex items-center justify-center flex-col m-[50px_0]">
  <div class="slider-container" bind:this={container} style="padding: 0 16px;">
    <div class="slider" style="position: relative; overflow: hidden;">
      {#each cards as card, index}
        <div

          class="card"

          style="margin-right: 16px; transform: translateX({$x}px);"

        >
          <img src={card.image} alt={card.title} />
          <h4>{card.title}</h4>
          <p>{card.text}</p>
        </div>
      {/each}
    </div>
  </div>

  <div class="bullets">
    {#each cards as _, i}
      <div

        class="bullet"

        class:active={i === currentIndex}

        on:click={() => goToSlide(i)}
      ></div>
    {/each}
  </div>
</div>

<style>

  .slider-container {

    width: 320px; /* Display one card width */

    position: relative;

    /* box-shadow: 9px 3px 10px 1px #0000002b; */

    border-radius: 10px;

  }



  .slider {

    display: flex;

    transition: transform 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94);

    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);

  }



  .card {

    flex: 0 0 calc(320px - 16px); /* Adjust for spacing */

    height: 377px;

    width: 315px;

    background: white;

    border-radius: 8px;

    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);

    display: flex;

    flex-direction: column;

    align-items: center;

    overflow: hidden;

    margin-right: 16px; /* Space between cards */

    padding: 20px;

  }



  .card img {

    width: 223px;

    height: 139px;

    object-fit: cover;

    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);

  }



  .card h4 {

    margin: 16px 0;

    font-size: 12px;

    text-align: center;

    font-weight: 700;

  }



  .card p {

    padding: 0 16px;

    line-height: 18px;

    text-align: center;

    font-weight: 400;

    font-size: 10px;

  }



  .bullets {

    display: flex;

    justify-content: center;

    margin-top: 20px;

  }



  .bullet {

    width: 10px;

    height: 10px;

    background: #ccc;

    border-radius: 50%;

    margin: 0 5px;

    cursor: pointer;

  }



  .bullet.active {

    background: #1340ff !important;

  }



  .grabbing {

    cursor: grabbing;

  }

</style>