Skip to content

Commit 4eba1a4

Browse files
committed
feat: add props to manage carousel
1 parent 999b86c commit 4eba1a4

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

vue3-carousel/src/components/Carousel.vue

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<slot :currentSlide="currentSlide" />
44

55
<!-- Navigation -->
6-
<div class="navigate">
6+
<div v-if="navigationEnabled" class="navigate">
77
<div class="toggle-page left" @click="prevSlide">
88
<i class="fas fa-chevron-left"></i>
99
</div>
@@ -13,7 +13,7 @@
1313
</div>
1414

1515
<!-- Pagination -->
16-
<div class="pagination">
16+
<div v-if="paginationEnabled" class="pagination">
1717
<span
1818
v-for="(slide, index) in getSlideCount"
1919
:key="index"
@@ -29,11 +29,22 @@
2929
import { ref, onMounted } from "vue";
3030
3131
export default {
32-
setup() {
32+
props: ["autoPlay", "timeout", "navigation", "pagination"],
33+
setup(props) {
3334
const currentSlide = ref(1);
3435
const getSlideCount = ref(null);
35-
const autoPlayEnabled = ref(true);
36-
const timeOutDuration = ref(5000);
36+
const autoPlayEnabled = ref(
37+
props.autoPlay === undefined ? true : props.autoPlay
38+
);
39+
const timeOutDuration = ref(
40+
props.timeout === undefined ? 5000 : props.timeout
41+
);
42+
const paginationEnabled = ref(
43+
props.pagination === undefined ? true : props.pagination
44+
);
45+
const navigationEnabled = ref(
46+
props.navigation === undefined ? true : props.navigation
47+
);
3748
3849
const nextSlide = () => {
3950
if (currentSlide.value === getSlideCount.value) {
@@ -55,14 +66,14 @@ export default {
5566
currentSlide.value = index + 1;
5667
};
5768
58-
const autoPlay = () => {
69+
const startAutoPlay = () => {
5970
setInterval(() => {
6071
nextSlide();
6172
}, timeOutDuration.value);
6273
};
6374
6475
if (autoPlayEnabled.value) {
65-
autoPlay();
76+
startAutoPlay();
6677
}
6778
6879
onMounted(() => {
@@ -75,6 +86,8 @@ export default {
7586
prevSlide,
7687
getSlideCount,
7788
goToSlide,
89+
paginationEnabled,
90+
navigationEnabled,
7891
};
7992
},
8093
};

vue3-carousel/src/views/Home.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<template>
22
<div class="home">
3-
<Carousel class="carousel" v-slot="{ currentSlide }">
3+
<Carousel
4+
:pagination="true"
5+
:navigation="true"
6+
:autoPlay="true"
7+
:timeout="1000"
8+
class="carousel"
9+
v-slot="{ currentSlide }"
10+
>
411
<Slide v-for="(slide, index) in caroselSlides" :key="index">
512
<div v-show="currentSlide === index + 1" class="slide-info">
613
<img :src="`https://picsum.photos/id/${slide}/1000/800`" alt="" />

0 commit comments

Comments
 (0)