vue封装一个swiper组件
首先在页面内引入swiper脚本,包括css、js
<!-- swiper --> <link href="https://cdn.staticfile.org/Swiper/4.5.1/css/swiper.min.css" rel="stylesheet">
<!-- swiper --> <script src="https://cdn.staticfile.org/Swiper/4.5.1/js/swiper.min.js"></script>
另外需要在eslint中配置,将swiper对象配置成全局对象
在components文件中写一个swiper组件
<template> <div> <div :class="swpName +' swiperBox '+ className"> <div class="swiper-wrapper"> <div class="swiper-slide" v-for="(item,key) in list" :key="'swiperSlide'+key"> <a v-if="item.link != undefined" :href="item.link" class="bannerItem"> <img :src="item.src" alt=""> </a> <div v-else class="bannerItem"><img :src="item.src" alt=""></div> </div> </div> <div v-if="isPrevNext == true" :class="swpName + 'prev swiper-button-prev swiper-button-'+ prevNextColor" slot="button-prev"></div> <div v-if="isPrevNext == true" :class="swpName + 'next swiper-button-next swiper-button-'+ prevNextColor" slot="button-next"></div> <div v-if="isPagination == true" :class="'swiper-pagination'"></div> </div> </div> </template> <script> export default { props: { //轮播图列表数据 list: { type: Object, default: function(){ return [] } }, //样式名称 className: { type: String, default: '' }, prevNextColor: { type: String, default: '' }, //是否显示圆点 isPagination: { type: Boolean, default: false }, //是否显示左右箭头 isPrevNext: { type: Boolean, default: false }, //自动播放延迟多少毫秒 delay: { type: String, default: '3000' }, //是否循环切换 loop: { type: Boolean, default: true }, //是否自动播放 autoPlay: { type: Boolean, default: true }, // 一屏显示几个 slidesPerView:{ type:Number, default:1 }, //每个轮播间的间隔距离 spaceBetween:{ type:Number, default:0 }, //导航点样式 paginationType:{ type:String, default:'' } }, data(){ return{ swpName: this.roundString(), MySwiper:null,//swiper实例 } }, mounted(){ var self=this; self.MySwiper = new Swiper ('.'+ self.swpName,{ init: true, observer:true, observeParents:true, slidesPerView: self.slidesPerView, spaceBetween: self.spaceBetween, keyboard: { enabled: true, }, loop: self.loop, autoplay:self.autoPlay? {delay: self.delay,disableOnInteraction: false}:false, pagination: { el: '.swiper-pagination', clickable: true, type:self.paginationType?self.paginationType:'bullets' }, navigation: { nextEl: '.'+self.swpName + 'prev', prevEl: '.'+self.swpName + 'next' }, }); }, methods: { roundString() { let str = ""; let chars = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; chars.forEach(() => { str += chars[Math.ceil(Math.random()*25)] }); return str } }, } </script> <style lang="scss" scoped> @import url("../assets/css/swiperBullet.css"); </style>
注意到上面组件内需要引入一个swiperBullet.css样式,之所以这样引入,是可以自定义导航点
swiperBullet.css放在assets文件夹下
.swiperBox{ width:100%; position: relative; overflow: hidden; } .swiperBox .bannerItem img{ height:auto; width:100%; } .swiperBox .bannerItem{ width:100%; text-align: center; } .swiperBox .bannerItem img{ height:auto; width:100%; } .swiper-pagination{ position: absolute; } /* 轮播导航点配置 */ .swiper-pagination .swiper-pagination-bullet { width: 0.1rem; height: 0.1rem; text-align: center; line-height: 20px; font-size: 12px; color:#000; opacity: 1; background: rgba(0,0,0,0.2); margin-right:20px; outline: none; } .swiper-pagination .swiper-pagination-bullet:last-child{ margin-right:0; } .swiper-pagination .swiper-pagination-bullet-active { color:#fff; background: #005aab; }
在页面中使用:
import SwiperCmp from "@/components/swiperComponent";
components:{SwiperCmp},
<div class="banner_box"> <SwiperCmp :list="bannerList" :isPagination="true" :className="'swiperClass'"/> </div>
自定义导航点样式:
.banner_box{ /deep/.swiperClass{ height:3rem; .swiper-pagination{ bottom:1rem; //这里可以自定义导航点的样式 } } }
。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通