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

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>

运行截图如下:

相关文章:

  • Unicode代码点与编码
  • 好消息!软航文档控件(NTKO WebOffice)在Chrome 133版本上提示扩展已停用的解决方案
  • 【虚拟环境配置】地信从0开始的环境配置小白教程
  • python康复日记-request库的使用,爬虫自动化测试
  • 【2025】基于springboot+vue的校园创新创业竞赛平台设计与实现(源码、万字文档、图文修改、调试答疑)
  • 使用Mastra.ai构建AI智能体:一次动手实践
  • 年龄预测识别模型训练python代码
  • PostgreSQL 事务处理
  • AI大模型全攻略:原理 · 部署 · Prompt · 场景应用
  • AI本地部署之dify
  • 【LNMP】网站架构分布式部署
  • 智能体(Agent)入门,智能体构建方法、工具链与应用策略;大语言模型的本地化部署、优化
  • 【AI学习从零至壹】PyTorch模型训练相关要素
  • 亚马逊云科技全面托管DeepSeek-R1模型现已上线
  • 第二节第一部分:String字符串
  • 建库字符集选择`utf8mb4` + `utf8mb4_unicode_ci` 组合
  • 第三讲 | C/C++内存管理完全手册
  • 3. 轴指令(omron 机器自动化控制器)——>MC_CamIn
  • 在VMware17中安装使用Ubuntu虚拟机
  • 数据结构笔记
  • 最高法改判一起植物新品种侵权案:判赔逾5300万元破纪录
  • 青海西宁市公安局原党委委员、副局长王小华被“双开”
  • “70后”女博士张姿卸任国家国防科技工业局副局长
  • 宁夏中卫深化公立医院机构编制改革:市人民医院机构规格升为正处级
  • 人民日报:外资车企携新车、前沿技术亮相上海车展,坚定信心深耕中国市场
  • 特朗普称或将“大幅降低”对华关税,外交部:打,奉陪到底;谈,大门敞开