当前位置: 首页 > article >正文

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

思路

主要就是参考

swiper | uni-app官网

实现轮播图。

实例

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

代码如下:

<template>
	<view>
		轮播图
	</view>
</template>

<script>
</script>

<style>
</style>

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

<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样式:

<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>

运行截图如下:

在此进行优化下:

<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>

运行截图如下:


http://www.kler.cn/a/599705.html

相关文章:

  • python 实现一个简单的window 任务管理器
  • python --face_recognition(人脸识别,检测,特征提取,绘制鼻子,眼睛,嘴巴,眉毛)/活体检测
  • 常见的表单元素
  • Java并发编程面试汇总
  • Unity客户端一些面试高频题(自用)
  • 采样率24G DA子卡
  • Atlas 800I A2 双机直连部署DeepSeek-R1-w8a8
  • 塔能科技:智慧物联节能专利成就裴然
  • TCP netstat TIME_WAIT CLOSE_WAIT
  • WebSocket 传输大量数据好不好?稳定不稳定
  • 使用 Docker 部署 mysql 应用
  • C stm32f10x LED亮
  • go命令使用
  • 【微服务】SpringCloudGateway网关
  • Android App安装列表获取
  • k8s基础知识总结node+pod(上)
  • 跨域,前端
  • 埋点数据采集方案
  • 机器学习结合伏羲模型高精度多尺度气象分析与降尺度实现
  • C++ 性能优化隐藏危机:忽视数据结构与内存细节,效率大打折扣