uniapp笔记-swiper组件实现轮播图

思路

主要就是参考

swiper | uni-app官网

实现轮播图。

实例

新建一个banner.vue通用组件。

代码如下:

javascript 复制代码
<template>
	<view>
		轮播图
	</view>
</template>

<script>
</script>

<style>
</style>

随后在index.vue中导入banner相关代码:

javascript 复制代码
<template>
	<view class="index-box">
		<bannerVue></bannerVue>
	</view>
</template>

<script>
	import bannerVue from '../../components/common/banner.vue'
	export default {
		components: {bannerVue},
		data() {
			return {
				title: 'Hello'
			}
		},
		onLoad() {

		},
		methods: {

		}
	}
</script>

<style>
</style>

运行效果如下:

下面把图片放进去,在在static中新建images目录,如下图:

查询下swiper的相关文档

对swiper进行如下补充(修改banner.vue代码)并做一些css样式:

javascript 复制代码
<template>
	<view class="banner-box">
		<view class="banner-bg"></view>
		<swiper class="banner-swipper"  indicator-dots indicator-color="#007aff"
		indicator-active-color="#4cd964"
		autoplay="4000">
		<swiper-item class="swiper-item" v-for="(item, index) in bannerList" :key="index">
			<image :src="item.imageUrl"></image>
		</swiper-item>
		</swiper>
	</view>
</template>

<script>
	export default{
		props: {
			bannerList:{
				type: Array,
				default: () => [
					{
						id: 1,
						imageUrl: '/static/images/01.png',
						background: '#009B8C'
					},
					{
						id: 2,
						imageUrl: '/static/images/02.png',
						background: '#729B8C'
					}
				]
			}
		}
	}
</script>

<style lang="scss">
	.banner-box{
		padding-top: 60rpx;
		.banner-bg{
			position: absolute;
			top: 0;
			width: 100%;
			height: 470rpx;
			background-image: linear-gradient(red 50%, #FFF);
			transform: .5s;
		}
		.banner-swipper{
			width: 100%;
			height: 350rpx;
			.swiper-item{
				width: 100%;
				height: 100%;
				image{
					width: 100%;
					height: 100%;
					border-radius: 15rpx;
				}
			}
		}
	}
</style>

运行截图如下:

在此进行优化下:

javascript 复制代码
<template>
	<view class="banner-box">
		<view class="banner-bg" :style="{'background-image': `linear-gradient(${bannerBackground || `#32DC2`} 50%, #FFF)`}"></view>
		<swiper class="banner-swipper"  indicator-dots indicator-color="#007aff"
		indicator-active-color="#4cd964"
		autoplay="4000" 
		circular
		:current="current" @change="swiperChange">
		<swiper-item class="swiper-item" v-for="(item, index) in bannerList" :key="index">
			<image :src="item.imageUrl"></image>
		</swiper-item>
		</swiper>
	</view>
</template>

<script>
	export default{
		props: {
			bannerList:{
				type: Array,
				default: () => [
					{
						id: 1,
						imageUrl: '/static/images/01.png',
						background: '#009B8C'
					},
					{
						id: 2,
						imageUrl: '/static/images/02.png',
						background: '#729B8C'
					}
				]
			}
		},
		data(){
			return {
				current: 0, //当前滑块的index
				bannerBackground: '#009B8C'	//背景色
			}
		},
		methods:{
			swiperChange(e){
				// console.log(e.detail.current)
				this.current = e.detail.current
				this.bannerBackground = this.bannerList[this.current].background
			}
		}
	}
</script>

<style lang="scss">
	.banner-box{
		padding-top: 60rpx;
	
		.banner-bg{
			position: absolute;
			top: 0;
			width: 100%;
			height: 470rpx;
			background-image: linear-gradient(red 50%, #FFF);
			transform: .5s;
		}
		.banner-swipper{
			width: 100%;
			height: 350rpx;
			.swiper-item{
				width: 100%;
				height: 100%;
				padding: 0 30rpx;
				box-sizing: border-box;
				image{
					width: 100%;
					height: 100%;
					border-radius: 15rpx;
				}
			}
		}
	}
</style>

运行截图如下:

相关推荐
渣哥4 分钟前
代理选错,性能和功能全翻车!Spring AOP 的默认技术别再搞混
javascript·后端·面试
遇见火星8 分钟前
Docker入门:快速部署你的第一个Web应用
前端·docker·容器
浓墨染彩霞24 分钟前
Java----set
java·经验分享·笔记
WeilinerL25 分钟前
泛前端代码覆盖率探索之路
前端·javascript·测试
浮游本尊29 分钟前
React 18.x 学习计划 - 第五天:React状态管理
前端·学习·react.js
-睡到自然醒~34 分钟前
[go 面试] 前端请求到后端API的中间件流程解析
前端·中间件·面试
洛卡卡了42 分钟前
Sentry 都不想接,这锅还让我背?这xx工作我不要了!
前端·架构
咖啡の猫1 小时前
Vue 实例生命周期
前端·vue.js·okhttp
JNU freshman1 小时前
vue 之 import 的语法
前端·javascript·vue.js
剑亦未配妥1 小时前
Vue 2 响应式系统常见问题与解决方案(包含_demo以下划线开头命名的变量导致响应式丢失问题)
前端·javascript·vue.js