vue中使用swiper輪播圖的正確姿勢(shì)(親測(cè)有效)
前言
網(wǎng)上搜了一大堆在vue中如何使用swiper,結(jié)果搜出來一堆垃圾,也不知道從哪里復(fù)制的,吐槽完畢。假設(shè)你是個(gè)新手,我從新建項(xiàng)目開始跟你講,以下是步驟。
1.新建vue項(xiàng)目
vue create 項(xiàng)目名
然后選最下面那一個(gè)(鍵盤上下鍵操作)然后回車
選擇Bable,Router,Vuex,Css-Processords四個(gè),其他的不要選中(空格鍵是選中和取消選中)
剩下的步驟按這張圖來進(jìn)行選擇,然后項(xiàng)目就創(chuàng)建成功了
2.裝swiper的包
先在命令行cd到項(xiàng)目中
cd 項(xiàng)目名
npm i swiper vue-awesome-swiper
npm i swiper@5
在package.json中查看裝包是否完成
3.使用swiper
1.在components文件夾中新建swiperCom.vue,把下面代碼復(fù)制進(jìn)去,注釋里面有swiper的使用方法。
注意:請(qǐng)確保../assets/img/ 路徑下有swiper1.jpg等照片
<template> <div id="swipercom"> <div class="swiper-container" id="swiperIndex"> <div class="swiper-wrapper"> <div class="swiper-slide" v-for="(item,i) in imgs" :key="i"> <img :src="item.pic" alt=""> </div> </div> //換頁器 <div class="swiper-pagination"> </div> //前進(jìn)后退 <div class="swiper-button-next"></div> <div class="swiper-button-prev"></div> //滾動(dòng)條 <div class="swiper-scrollbar"></div> </div> </div> </template> <script> import 'swiper/css/swiper.css' //引入swiper樣式 import Swiper from 'swiper'; //引入swiper export default { name: "Swiper", data(){ return{ imgs:[ {pic:require('../assets/img/swiper1.jpg')}, {pic:require('../assets/img/swiper2.jpg')}, {pic:require('../assets/img/swiper3.png')} ] } }, mounted() { var mySwiper=new Swiper('#swiperIndex',{ //配置分頁器內(nèi)容 loop: true, // 循環(huán)模式選項(xiàng) pagination:{ el:".swiper-pagination",//換頁器與哪個(gè)標(biāo)簽關(guān)聯(lián) clickable:true//分頁器是否可以點(diǎn)擊 }, // 如果需要前進(jìn)后退按鈕 navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, //如果需要滾動(dòng)條 scrollbar: { el: '.swiper-scrollbar', }, }) } } </script> <style lang="less"> #swipercom{ width: 7.5rem; #swiperIndex.swiper-container{ width: 7.1rem; height: 2.6rem; border-radius: 0.1rem; .swiper-slide img{ width: 100%; } .swiper-pagination-bullet-active{ background-color: orangered; } } } </style>
2.然后在項(xiàng)目中找到HomeView.vue(默認(rèn)為主頁面),把下面代碼復(fù)制,替換掉里面內(nèi)容,里面引入了swiperCom子組件,這也是我們需要用到swiper的子組件
<template> <div class="home"> <!-- 輪播圖--> <swiperCom></swiperCom> </div> </template> <script> import SwiperCom from "@/components/swiperCom"; export default { name: 'HomeView', components: { SwiperCom } } </script> <style scoped> </style>
大功告成,效果如下
除此之外要是想要有更多的輪播圖樣式可以去swiper官網(wǎng)進(jìn)行查閱
https://www.swiper.com.cn/usage/index.html
總結(jié)
到此這篇關(guān)于vue中使用swiper輪播圖的正確姿勢(shì)的文章就介紹到這了,更多相關(guān)vue使用swiper輪播圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用v-if v-show頁面閃爍,div閃現(xiàn)的解決方法
在頁面層次結(jié)構(gòu),數(shù)據(jù)較多的時(shí)候,用v-if或者v-show就會(huì)出現(xiàn)div閃現(xiàn),或者部分閃爍的結(jié)果。怎么處理這樣的問題呢,下面小編給大家?guī)砹藇ue使用v-if v-show頁面閃爍,div閃現(xiàn)的解決方法,一起看看吧2018-10-10Vue-resource實(shí)現(xiàn)ajax請(qǐng)求和跨域請(qǐng)求示例
本篇文章主要介紹了Vue-resource實(shí)現(xiàn)ajax請(qǐng)求和跨域請(qǐng)求示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02vue ElementUI的from表單實(shí)現(xiàn)登錄效果的示例
本文主要介紹了vue ElementUI的from表單實(shí)現(xiàn)登錄效果的示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09