app挂载
创建swiper组件
<div id="app">
<swiper :gallery="gallery" style="width:100%"></swiper>
</div>
css部分
<style type="text/css">
.swiper {
position: relative;
}
.prev {
position: absolute;
left: 10px;
top: 50%;
transform: translateY(-50%);
color: #ccc;
font-size: 35px;
}
.next{
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
color: #ccc;
font-size: 35px;
}
.dot {
width: 100px;
height: 20px;
border-radius: 10px;
background-color: rgba(0,0,0,0.5);
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
justify-content: space-around;
align-items: center;
}
.dot-con {
width: 15px;
height: 15px;
border-radius: 50%;
background-color: #fff;
}
.on{
background-color: #F05B1A;
}
.prev,
.next{
cursor: pointer;
}
</style>
js部分
<script>
const swiper = {
template: `
<div class="swiper" @mouseover="stop()" @mouseout="play()">\
<span class="prev" @click="current-- ;check()"><</span>
<div class="swiper-item" v-for="(item,index) in gallery" v-show="index===current">
<img :src="item" style="width:100%">
</div>
<span class="next" @click="current++ ;check()">></span>
<div class="dot">
<div class="dot-con" v-for="(item,index) in gallery" :class="index===current?'on':''"></div>
</div>
</div>`,
props: {
gallery: {
type: Array,
default: function() {
return []
}
},
interval: {
type: Number,
default: 2000
}
},
data() {
return {
current: 0,
interId: null,
}
},
methods: {
play() {
this.interId = setInterval(() => {
this.current++
this.check()
}, this.interval)
},
stop() {
clearInterval(this.interId)
},
check() {
if (this.current >= this.gallery.length) {
this.current = 0
}
if (this.current < 0) {
this.current = this.gallery.length - 1
}
}
},
created() {
this.play()
}
}
Vue.createApp({
components: {
swiper
},
data() {
return {
gallery: [
"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/918820682e4a490221cfd92b24c14b86.jpg?thumb=1&w=1533&h=575&f=webp&q=90",
"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/222d6c61df75f30e6782ec476d5c8273.jpg?thumb=1&w=1533&h=575&f=webp&q=90",
"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/dd741adcce9417d72ea4c1a6dfcc96e2.jpg?thumb=1&w=1533&h=575&f=webp&q=90",
"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/39bb34167f6c178d6bb768d8872c97f8.jpg?w=2452&h=920"
]
}
}
}).mount("#app")
</script>
完整代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>幻灯片</title>
<script src="js/vue.js"></script>
<style type="text/css">
.swiper {
position: relative;
}
.prev {
position: absolute;
left: 10px;
top: 50%;
transform: translateY(-50%);
color: #ccc;
font-size: 35px;
}
.next{
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
color: #ccc;
font-size: 35px;
}
.dot {
width: 100px;
height: 20px;
border-radius: 10px;
background-color: rgba(0,0,0,0.5);
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
justify-content: space-around;
align-items: center;
}
.dot-con {
width: 15px;
height: 15px;
border-radius: 50%;
background-color: #fff;
}
.on{
background-color: #F05B1A;
}
.prev,
.next{
cursor: pointer;
}
</style>
</head>
<body>
<div id="app">
<swiper :gallery="gallery" style="width:100%"></swiper>
</div>
<script>
const swiper = {
template: `
<div class="swiper" @mouseover="stop()" @mouseout="play()">\
<span class="prev" @click="current-- ;check()"><</span>
<div class="swiper-item" v-for="(item,index) in gallery" v-show="index===current">
<img :src="item" style="width:100%">
</div>
<span class="next" @click="current++ ;check()">></span>
<div class="dot">
<div class="dot-con" v-for="(item,index) in gallery" :class="index===current?'on':''"></div>
</div>
</div>`,
props: {
gallery: {
type: Array,
default: function() {
return []
}
},
interval: {
type: Number,
default: 2000
}
},
data() {
return {
current: 0,
interId: null,
}
},
methods: {
play() {
this.interId = setInterval(() => {
this.current++
this.check()
}, this.interval)
},
stop() {
clearInterval(this.interId)
},
check() {
if (this.current >= this.gallery.length) {
this.current = 0
}
if (this.current < 0) {
this.current = this.gallery.length - 1
}
}
},
created() {
this.play()
}
}
Vue.createApp({
components: {
swiper
},
data() {
return {
gallery: [
"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/918820682e4a490221cfd92b24c14b86.jpg?thumb=1&w=1533&h=575&f=webp&q=90",
"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/222d6c61df75f30e6782ec476d5c8273.jpg?thumb=1&w=1533&h=575&f=webp&q=90",
"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/dd741adcce9417d72ea4c1a6dfcc96e2.jpg?thumb=1&w=1533&h=575&f=webp&q=90",
"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/39bb34167f6c178d6bb768d8872c97f8.jpg?w=2452&h=920"
]
}
}
}).mount("#app")
</script>
</body>
</html>