This repository has been archived by the owner on May 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Carousel.svelte
171 lines (145 loc) · 3.21 KB
/
Carousel.svelte
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
<div class="carousel">
<div class="slides" bind:this={siema}>
<slot></slot>
</div>
{#if controls}
<button class="left" on:click={left} aria-label="left">
<slot name="left-control"></slot>
</button>
<button class="right" on:click={right} aria-label="right">
<slot name="right-control"></slot>
</button>
{/if}
{#if dots}
<ul>
{#each {length: totalDots} as _, i}
<li on:click={() => go(i*currentPerPage)} class={isDotActive(currentIndex, i) ? "active" : ""}></li>
{/each}
</ul>
{/if}
</div>
<style>
.carousel {
position: relative;
width: 100%;
justify-content: center;
align-items: center;
}
button {
position: absolute;
width: 40px;
height: 40px;
top: 50%;
z-index: 50;
margin-top: -20px;
border: none;
background-color: transparent;
}
button:focus {
outline: none;
}
.left {
left: 2vw;
}
.right {
right: 2vw;
}
ul {
list-style-type: none;
position: absolute;
display: flex;
justify-content: center;
width: 100%;
margin-top: -30px;
padding: 0;
}
ul li {
margin: 6px;
border-radius: 100%;
background-color: rgba(255,255,255,0.5);
height: 8px;
width: 8px;
}
ul li:hover {
background-color: rgba(255,255,255,0.85);
}
ul li.active {
background-color: rgba(255,255,255,1);
}
</style>
<script>
import Siema from 'siema'
import { onMount, createEventDispatcher } from 'svelte'
export let perPage = 3
export let loop = true
export let autoplay = 0
export let duration = 200
export let easing = 'ease-out'
export let startIndex = 0
export let draggable = true
export let multipleDrag = true
export let dots = true
export let controls = true
export let threshold = 20
export let rtl = false
let currentIndex = startIndex;
let siema
let controller
let timer
const dispatch = createEventDispatcher()
$: pips = controller ? controller.innerElements : []
$: currentPerPage = controller ? controller.perPage : perPage
$: totalDots = controller ? Math.ceil(controller.innerElements.length / currentPerPage) : []
onMount(() => {
controller = new Siema({
selector: siema,
perPage: typeof perPage === 'object' ? perPage : Number(perPage),
loop,
duration,
easing,
startIndex,
draggable,
multipleDrag,
threshold,
rtl,
onChange: handleChange
})
if(autoplay) {
timer = setInterval(right, autoplay);
}
return () => {
autoplay && clearInterval(timer)
timer = null
controller.destroy()
}
})
export function isDotActive (currentIndex, dotIndex) {
if (currentIndex < 0) currentIndex = pips.length + currentIndex;
return currentIndex >= dotIndex*currentPerPage && currentIndex < (dotIndex*currentPerPage)+currentPerPage
}
export function left () {
controller.prev()
}
export function right () {
controller.next()
}
export function go (index) {
controller.goTo(index)
}
export function pause() {
clearInterval(timer);
timer = null
}
export function resume() {
if (autoplay && !timer) {
timer = setInterval(right, autoplay);
}
}
function handleChange (event) {
currentIndex = controller.currentSlide
dispatch('change', {
currentSlide: controller.currentSlide,
slideCount: controller.innerElements.length
} )
}
</script>