uni-app中轮播图swiper修改指示器样式为数字胶囊式

本文介绍如何在uni-app中实现九宫格与轮播图混合使用,并自定义数字胶囊样式的指示器。通过修改swiper组件的指示器样式,结合自定义样式和逻辑处理,最终实现了领导要求的效果。

九宫格grid和轮播图混合应用

思路:轮播图嵌套在九宫格外面,详情参考官方文档。https://uviewui.com/components/grid.html
领导要求将轮播图的指示器改为:数字胶囊类型,在uview组件的u-swiper组件中有这一属性 :mode=“number” 。但是如果里面还要嵌套九宫格grid就不能应用u-swiper组件。此时就需要用uni-app中的swiper
进行到这步,我们需要把swiper中的指示器 :indicator-dots 改为false。令他不显示指示器默认样式
还有一点获取当前轮播展示的下标,并传值给指示器,完成数字胶囊样式指示器的轮播

  1. 官网

在这里插入图片描述
2. 根据官网的做出来之后,指示器样式是dots类型,这是根据官网做出来的样式↓

在这里插入图片描述
在这里插入图片描述
3. 不符合要求,我百度了好多才解决好这个指示器样式,首先在swiper下边定义一个指示器,样式和位置在style中更改。
在这里插入图片描述

/* 指示器样式 */
	.u-indicator-item-number {
		width: 40px;
		padding: 6rpx 16rpx;
		line-height: 1;
		background-color: rgba(0, 0, 0, 0.3);
		border-radius: 100rpx;
		font-size: 26rpx;
		color: rgba(255, 255, 255, 0.8);
		position: relative;
		left: 45%;
	}
  1. 子组件中template部分代码

在这里插入图片描述

<view class="swiper" >
		<!--外部轮播 -->
		<swiper @change="topSwiperTab" :current="uCurrent" :indicator-dots="false"  >
			<swiper-item v-for="(item,index) in newBankList " :key="index"  >
				<u-grid :col="4" >
						<u-grid-item v-for="(item2,index2) in item" :key="index2" >
							<u-image width="30px" height="30px" :src="item2.src"></u-image>
							<view class="grid-text">{{item2.text}}</view>
						</u-grid-item>
				</u-grid>
			</swiper-item>
		</swiper>	
		<!-- 更改指示器样式
		 指示器:数字胶囊 -->
		<block class="number"  >	
			<view class="u-indicator-item-number">{{ uCurrent + 1 }}/{{ newBankList.length }}</view>	
		</block>
	</view>
  1. 子组件中script部分代码
    在这里插入图片描述
    在这里插入图片描述
	props:{
			
			newBankList:Array,
			mode:String
		},
		data(){
			return{
				uCurrent:0
			}
		},
	methods:{
			topSwiperTab(e){
				this.uCurrent =Number(e.target.current)
				// number(e.target.current)
				console.log(this.uCurrent);
			}
		}

6.查看完成后的样式
在这里插入图片描述

### uni-app 中实现轮播图的方 在 `uni-app` 开发框架中,可以通过内置的 `<swiper>` 组件轻松实现轮播图功能。以下是基于提供的引用内容以及实际开发经验整理的一个完整的轮播图实现方案。 #### 1. 使用 `<swiper>` 和 `<swiper-item>` 构建基本结构 通过设置属性如 `indicator-dots`, `autoplay`, `interval`, `duration` 等来控制轮播行为[^1]。以下是一个基础示例: ```html <template> <view class="container"> <!-- 轮播图 --> <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true" > <swiper-item v-for="(item, index) in swiperList" :key="index"> <navigator class="swiper-item" :url="'/pages/detail/detail?id=' + item.id"> <image :src="item.imageSrc" mode="scaleToFill"></image> </navigator> </swiper-item> </swiper> </view> </template> <script> export default { data() { return { swiperList: [ { id: 1, imageSrc: 'https://example.com/image1.jpg' }, { id: 2, imageSrc: 'https://example.com/image2.jpg' }, { id: 3, imageSrc: 'https://example.com/image3.jpg' } ] }; } }; </script> <style scoped> .swiper-item { width: 100%; height: 300rpx; } image { width: 100%; height: 100%; } </style> ``` 此代码片段展示了如何创建一个自动播放、带有圆点指示器轮播图,并支持点击跳转至详情页的功能[^1]。 --- #### 2. 动态加载数据并绑定到视图 如果需要动态获取图片列表,则可以在生命周期钩子函数 `onLoad` 或其他方法中调用接口请求数据[^3]。例如: ```javascript data() { return { swiperList: [] // 存储从服务器获取的数据 }; }, methods: { fetchSwiperData() { uni.request({ url: 'https://api.example.com/swiper-data', method: 'GET', success: (res) => { if (res.statusCode === 200 && Array.isArray(res.data)) { this.swiperList = res.data.map(item => ({ id: item.id, imageSrc: item.imageUrl })); } } }); } }, onLoad() { this.fetchSwiperData(); // 页面加载时发起网络请求 } ``` 以上逻辑实现了从远程 API 加载轮播图数据,并将其赋值给 Vue 的响应变量 `swiperList`,从而更新界面显示[^3]。 --- #### 3. 自定义指示点样式 默认情况下,`<swiper>` 提供了简单的圆形指示点;但如果希望自定义更复杂的指示效果(比如数字胶囊),则需关闭原生指示点并通过额外的 HTML/CSS 来完成定制化设计[^4]。下面是一段演示代码: ```html <swiper :indicator-dots="false" :autoplay="true" :interval="3000" :duration="1000" @change="handleSwiperChange" > <swiper-item v-for="(item, index) in topSwiper" :key="index"> <view class="custom-swiper-item"> <image :src="item.src" mode="aspectFill"></image> </view> </swiper-item> </swiper> <!-- 自定义指示点 --> <view class="custom-indicator">{{ currentIdx + 1 }} / {{ topSwiper.length }}</view> ``` 配合脚本部分处理切换事件: ```javascript data() { return { currentIdx: 0, topSwiper: [ { src: '/static/images/banner1.png' }, { src: '/static/images/banner2.png' }, { src: '/static/images/banner3.png' } ] }; }, methods: { handleSwiperChange(e) { const currentIndex = e.detail.current || 0; this.currentIdx = currentIndex; } } ``` 这里利用了 `@change` 事件监听当前活动项的变化,并实时同步到状态管理中的索引值 `currentIdx` 上[^4]。 --- #### 总结 综上所述,在 `uni-app` 平台下构建轮播图主要依赖于其核心组件 `<swiper>` 及其实现机制。无论是静态配置还是动态拉取资源链接都能很好地满足项目需求。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值