Loading... ##### 一、在 components 中新建 swiperCom 文件夹并新建 swiperCom.vue文件 ###### 1、写入 html 代码 ```html <template> <swiper class="swiperBox" indicator-dots autoplay :interval="swiperTime" circular previous-margin="65rpx" next-margin="65rpx" @change="swiperChange"> <swiper-item v-for="(item,index) in swiperData" :key="item.id" @click="swiperClick(item)"> <view :class="index == acitveInd ? 'swiperActive' : ''" class="swiper-item">{{item.title}}</view> </swiper-item> </swiper> </template> ``` ###### 2、写入 css 代码 ```scss .swiperBox { width: 750rpx; height: 300rpx; margin: 20rpx 0; box-sizing: border-box; swiper-item { display: flex; align-items: center; justify-content: center; .swiper-item { width: 100%; margin-top: 0; text-align: center; line-height: 280rpx; background: rgba(0, 0, 0, .3); border-radius: 20rpx; overflow: hidden; } .swiperActive { height: 300rpx !important; margin: 0rpx 10rpx; } } } ``` ###### 3、写入 js 代码 ```js export default { props: { swiperData: { type: null, require: true }, swiperTime: { type: Number, require: true }, }, name:"swiperCom", data() { return { acitveInd: 0, }; }, methods: { // 切换改变 swiperChange(event){ this.acitveInd = event.detail.current; // 触发父级事件 this.$emit('swiperChange',this.acitveInd); }, // 点击 swiperClick(item){ // 触发父级事件 this.$emit('swiperClick',item); }, }, } ``` ##### 二、父组件调用 ###### 1、html 代码 ```html <swiper-com :swiperData="swiperData" :swiperTime="swiperTime" @swiperChange="swiperChange" @swiperClick="swiperClick"></swiper-com> ``` ###### 2、js 代码 ```js <script> import swiperCom from '@/components/swiperCom/swiperCom.vue'; export default { components: { swiperCom }, data() { return { swiperData: [{ id: 1, title: '标题1', }, { id: 2, title: '标题2', }, { id: 3, title: '标题3', }, ], swiperTime: 2000, }; }, onLoad() { }, onShow() { }, methods: { // 轮播时执行 swiperChange(val){ console.log(val,'val'); }, // 点击某个执行 swiperClick(val){ console.log(val,'clickVal'); }, } } </script> ``` ##### 三、修改指示点样式需要在 App.vue 中写如下代码 ```scss /* uni-app中的条件判断语句 */ /* #ifdef H5 */ uni-swiper .uni-swiper-dot { width: 10rpx; height: 10rpx; } uni-swiper .uni-swiper-dot-active { width: 30rpx; border-radius: 10rpx; } /* #endif */ /* #ifdef MP-WEIXIN */ wx-swiper .wx-swiper-dot { width: 10rpx; height: 10rpx; } wx-swiper .wx-swiper-dot-active { width: 30rpx; height: 10rpx; border-radius: 10rpx; } /* #endif */ ``` ##### 四、效果展示 ![效果图][1] [1]: https://www.renlmingj.cn/usr/uploads/2024/06/1671254326.png 最后修改:2024 年 06 月 13 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏