一个简单的Vue3 轮播图组件

在Vue3中创建一个简单的轮播图组件,可以使用以下代码:

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
<script setup>
import { onMounted, onUnmounted, ref, useSlots } from 'vue'

const slots = useSlots()
const slides = ref(\[\])
const originalSlidesLength = ref(0)
const currentIndex = ref(0)
const isDragging = ref(false)
const startX = ref(0)
const currentTranslate = ref(0)
const prevTranslate = ref(0)
const props = defineProps({
autoPlay: {
type: Boolean,
default: false
}
})

onMounted(() => {
const originalSlides = slots.default()\[0\].children.map((slide, index) => ({ content: slide, key: index }))
originalSlidesLength.value = originalSlides.length
slides.value = \[...originalSlides, originalSlides\[0\]\]
startAutoPlay()
})

let autoPlayInterval
const startAutoPlay = () => {
if (!props.autoPlay) return
autoPlayInterval = setInterval(() => {
goToNextSlide()
}, 3000)
}

const goToNextSlide = () => {
if (currentIndex.value < slides.value.length - 2) {
currentIndex.value += 1
} else {
currentIndex.value = 0
updateTranslate(true)
return
}
updateTranslate()
}

const startDrag = (event) => {
isDragging.value = true
startX.value = event.clientX
prevTranslate.value = currentTranslate.value
clearInterval(autoPlayInterval)
}

const onDrag = (event) => {
if (isDragging.value) {
const currentX = event.clientX
const diff = currentX - startX.value
currentTranslate.value = prevTranslate.value + (diff / window.innerWidth) \* 100
}
}

const endDrag = () => {
if (isDragging.value) {
isDragging.value = false
const movedBy = currentTranslate.value - prevTranslate.value

if (movedBy < -10 && currentIndex.value < slides.value.length - 2) {
currentIndex.value += 1
} else if (movedBy > 10 && currentIndex.value > 0) {
currentIndex.value -= 1
} else if (movedBy > 10 && currentIndex.value === 0) {
currentIndex.value = slides.value.length - 2
} else if (movedBy < -10 && currentIndex.value === slides.value.length - 2) {
currentIndex.value = 0
}

updateTranslate()
startAutoPlay()
}
}

const updateTranslate = (instant = false) => {
prevTranslate.value = -currentIndex.value \* 100
currentTranslate.value = prevTranslate.value
if (instant) {
requestAnimationFrame(() => {
currentTranslate.value = prevTranslate.value
})
}
}

const goToSlide = (index) => {
currentIndex.value = index
updateTranslate()
}

onUnmounted(() => {
clearInterval(autoPlayInterval)
})
</script>

<template>
<div class="carousel-container">
<div
class="carousel"
@mousedown="startDrag"
@mousemove="onDrag"
@mouseup="endDrag"
@mouseleave="endDrag"
:style="{ transform: \`translateX(${currentTranslate}%)\`, transition: isDragging ? 'none' : 'transform 0.3s ease' }"
>
<div v-for="(slide, index) in slides" :key="slide.key" class="slide">
<component :is="slide.content"></component>
</div>
</div>

<div class="indicators">
<span
v-for="(indicator, index) in slides.slice(0, originalSlidesLength)"
:key="index"
class="indicator"
:class="{ active: currentIndex === index || (currentIndex === slides.length - 1 && index === 0) }"
@click="goToSlide(index)"
></span>
</div>
</div>
</template>

<style scoped>
.carousel-container {
position: relative;
width: 100%;
height: 100%;
margin: 0 auto;
overflow: hidden;
padding-bottom: 30px;
}

.carousel {
height: 100%;
display: flex;
}

.slide {
min-width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
font-size: 2em;
color: white;
user-select: none;
}

.indicators {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 10px;
}

.indicator {
width: 10px;
height: 6px;
background-color: rgba(255, 255, 255, 0.5);
cursor: pointer;
transition: background-color 0.3s ease;
}

.indicator.active {
background-color: rgba(255, 255, 255, 1);
}
</style>