library_components/components/Ml/SliderPoint/MlSliderPoint.vue

112 lines
2.7 KiB
Vue
Raw Normal View History

2024-07-05 12:22:31 +00:00
<template>
<div v-show="showSwiper" class="relative" :class="rootClasses">
<swiper
loop-add-blank-slides
:class="sliderClasses"
:direction="direction"
:space-between="50"
:breakpoints="{
2000: { slidesPerView: 1, spaceBetween: 40 },
}"
@swiper="onSwiper"
@slide-change="onSlideChange"
>
<swiper-slide
v-for="slide in 4"
:key="slide"
class="h-full hover:cursor-pointer"
>
<slot />
</swiper-slide>
</swiper>
2024-07-08 13:19:26 +00:00
<div :class="counterClasses">
2024-07-05 12:22:31 +00:00
<div
v-for="(slide, index) in 4"
:key="index"
2024-07-09 06:37:01 +00:00
class="bg-black"
:style="[
'height: 3px; transition: all 0.5s;',
2024-07-05 12:22:31 +00:00
index === currentSlideNumber
2024-07-09 06:37:01 +00:00
? 'padding-top : 8px; padding-bottom: 8px; width: 16px; border-radius : 50%;'
: 'height: 3px; width: 25px;',
2024-07-05 12:22:31 +00:00
]"
@click="goToSlide(index), stopAutoSlide()"
></div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed, defineProps, withDefaults, onMounted } from "vue";
import { Swiper, SwiperSlide } from "swiper/vue";
import type { Swiper as swiper } from "swiper/types";
import "swiper/css";
import { twMerge } from "tailwind-merge";
import type { PTAttribs } from "./types";
const showSwiper = ref(false);
const currentSlideNumber = ref(0);
let swiperInstance: swiper | null = null;
2024-07-09 06:37:01 +00:00
let intervalId: NodeJS.Timeout | null = null;
2024-07-05 12:22:31 +00:00
const onSwiper = (swiper: swiper) => {
swiperInstance = swiper;
showSwiper.value = true;
};
const onSlideChange = (swiper: swiper) => {
currentSlideNumber.value = swiper.realIndex;
};
function startAutoSlide() {
intervalId = setInterval(() => {
const nextSlide = (currentSlideNumber.value + 1) % 4;
goToSlide(nextSlide);
}, 5000);
}
function goToSlide(index: number) {
swiperInstance?.slideTo(index);
currentSlideNumber.value = index;
}
function stopAutoSlide() {
if (intervalId !== null) {
clearInterval(intervalId);
intervalId = null;
}
}
onMounted(() => {
startAutoSlide();
});
const props = withDefaults(
defineProps<{
direction: "horizontal" | "vertical";
pt?: PTAttribs;
}>(),
{
direction: "horizontal",
}
);
const classes = ref({
root: { class: `` },
slider: { class: `` },
2024-07-08 13:19:26 +00:00
counter: {
class: `mx-auto px-6 container flex justify-between items-center h-10 cursor-pointer`,
},
2024-07-05 12:22:31 +00:00
} as PTAttribs);
const rootClasses = computed(() =>
twMerge(classes.value.root?.class, props.pt?.root?.class)
);
const sliderClasses = computed(() =>
twMerge(classes.value.slider?.class, props.pt?.slider?.class)
);
const counterClasses = computed(() =>
twMerge(classes.value.counter?.class, props.pt?.counter?.class)
);
</script>