Encapsulate the multicast diagram components according to requirements
Requirements are tentatively determined as: width and height, background color, automatic rotation, rotation time interval
Encapsulated components:
<template>
<div class="xtx-carousel" @mouseenter="enter" @mouseleave="leave">
<ul class="carousel-body">
<li
class="carousel-item"
:class="{ fade: curIdx === idx }"
v-for="(item, idx) in sliders"
:key="item.id"
>
<RouterLink to="/">
<img :src="item.imgUrl" alt="" />
</RouterLink>
</li>
</ul>
<a href="javascript:;" @click="toggle(-1)" class="carousel-btn prev"
><i class="iconfont icon-angle-left"></i
></a>
<a href="javascript:;" class="carousel-btn next" @click="toggle(1)"
><i class="iconfont icon-angle-right"></i
></a>
<div class="carousel-indicator">
<span
v-for="(i, idx) in 5"
:key="i"
@click="curIdx = idx"
:class="{ active: idx === curIdx }"
></span>
</div>
</div>
</template>
<script>
import { onUnmounted, ref, watch } from 'vue'
export default {
props: {
sliders: {
type: Array.default: () = >[]},autoplay: {
type: Boolean.default: false
},
duration: {
type: Number.default: 2000}},name: 'XtxCarousel',
setup (props) {
const curIdx = ref(0)
// Auto play
let timer = null
const autoplay = () = > {
timer = setInterval(() = > {
curIdx.value++
if (curIdx.value >= props.sliders.length) curIdx.value = 0
}, props.duration)
}
// If there is data in sliders, it plays automatically
watch(() = > props.sliders, (newVal, oldVal) = > {
if (newVal.length > 0) {
if (props.autoplay) {
autoplay()
}
}
}, { immediate: true })
onUnmounted(() = > {
clearInterval(timer)
})
const leave = () = > {
if (props.autoplay) autoplay()
}
const enter = () = > {
clearInterval(timer)
}
// Toggle the previous and the next
const toggle = step= > {
curIdx.value += step
if (curIdx.value >= props.sliders.length) curIdx.value = 0
if (curIdx.value < 0) curIdx.value = props.sliders.length - 1
}
return { curIdx, leave, enter, toggle }
}
}
</script>
<style scoped lang="less">
.xtx-carousel {
width: 100%;
height: 100%;
min-width: 300px;
min-height: 150px;
position: relative;
.carousel {
&-body {
width: 100%;
height: 100%;
}
&-item {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
opacity: 0;
transition: opacity 0.5 s linear;
&.fade {
opacity: 1;
z-index: 1;
}
img {
width: 100%;
height: 100%;
}
}
&-indicator {
position: absolute;
left: 0;
bottom: 20px;
z-index: 2;
width: 100%;
text-align: center;
span {
display: inline-block;
width: 12px;
height: 12px;
background: rgba(0.0.0.0.2);
border-radius: 50%;
cursor: pointer;
~ span {
margin-left: 12px;
}
&.active {
background: #fff;
}
}
}
&-btn {
width: 44px;
height: 44px;
background: rgba(0.0.0.0.2);
color: #fff;
border-radius: 50%;
position: absolute;
top: 228px;
z-index: 2;
text-align: center;
line-height: 44px;
opacity: 0;
transition: all 0.5 s;
&.prev {
left: 20px;
}
&.next {
right: 20px; &}}}:hover {
.carousel-btn {
opacity: 1; }}}</style>
Copy the code
Here is a brief explanation of it:
Sliders are a list of data passed in from the upper level component in the form of an array
Autoplay is a Boolean value passed from a parent component, meaning whether auto rotation is enabled
Duration Interval passed from the parent component, meaning how often to cast, type number
props: {
sliders: {
type: Array.default: () = >[]},autoplay: {
type: Boolean.default: false
},
duration: {
type: Number.default: 2000}},Copy the code
Then pass the component definition to use, either globally or locally, and define it globally here
In the js file that defines the global component:
// Introduce the wheel map component
import XtxCarousel from './xtx-carousel.vue'
const myPlugin = {
install (app) {
// app is an instance of vue
// app.component.component.component.component.component.component.object
app.component(XtxCarousel.name, XtxCarousel)
}
}
export default myPlugin
Copy the code
Just import and use() in main.js
// Import global components
import myPlugin from '@/components/index'
createApp(App).use(store).use(router).use(myPlugin).mount('#app')
Copy the code
Vue2 and VUe3 introduce global component differences