vue3实现简单轮播图

 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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值