Browse Source

feat: 增加轮播

main
betaqi 2 months ago
parent
commit
570977ee5c
  1. 8
      components/SwiperFlip.vue
  2. 40
      pages/index.vue

8
components/SwiperFlip.vue

@ -1,7 +1,7 @@
<template> <template>
<ClientOnly> <ClientOnly>
<div class="swiper-card-container"> <div class="swiper-card-container">
<swiper-container ref="containerRef" :init="false" :effect="'coverflow'"> <swiper-container ref="containerRef" :init="false">
<slot></slot> <slot></slot>
</swiper-container> </swiper-container>
<div class="swiper-cards-prev"></div> <div class="swiper-cards-prev"></div>
@ -12,6 +12,7 @@
<script setup> <script setup>
const containerRef = ref(null) const containerRef = ref(null)
useSwiper(containerRef, { useSwiper(containerRef, {
effect: 'cards', effect: 'cards',
grabCursor: true, grabCursor: true,
@ -22,7 +23,10 @@ useSwiper(containerRef, {
slideShadows: true, slideShadows: true,
perSlideOffset: 8, perSlideOffset: 8,
}, },
autoplay: {
delay: 2500,
disableOnInteraction: false,
},
pagination: { pagination: {
el: '.swiper-pagination', el: '.swiper-pagination',
clickable: true, clickable: true,

40
pages/index.vue

@ -157,9 +157,47 @@ function updateOnSelect() {
// state.canScrollNext = embla.value.canScrollNext() // state.canScrollNext = embla.value.canScrollNext()
} }
onMounted(() => { onMounted(() => {
embla.value = EmblaCarousel(emblaNode.value, { loop: true }) embla.value = EmblaCarousel(emblaNode.value, { loop: true }, [createAutoplay(4000)])
embla.value.on('select', updateOnSelect) embla.value.on('select', updateOnSelect)
}) })
function createAutoplay(delay = 4000) {
let embla: any = null
let timer: any = null
function autoplay() {
stop()
if (!embla) return
timer = setInterval(() => {
if (!embla) return
if (!embla.canScrollNext()) {
embla.scrollTo(0) //
} else {
embla.scrollNext()
}
}, delay)
}
function stop() {
if (timer) clearInterval(timer)
}
return {
name: 'autoplay',
options: {},
init(emblaApi: any) {
embla = emblaApi
autoplay()
embla.on('pointerDown', stop)
embla.on('pointerUp', autoplay)
embla.on('destroy', stop)
},
destroy() {
stop()
embla = null
},
}
}
</script> </script>
<style> <style>

Loading…
Cancel
Save