vue2項(xiàng)目之swiper.js 的使用
swiper.js 的使用
官網(wǎng) API(部署在 swiper 實(shí)例中):https://www.swiper.com.cn/api/index.html
官網(wǎng)輪播圖(查看源代碼):https://www.swiper.com.cn/demo/index.html
接下來介紹怎么在 vue2 里使用 swiper.js (vue2 使用 swiper5版本)
1、安裝、引入css
npm i swiper@5
// main.js // 引入 swiper.css import "swiper/css/swiper.css";
2、在組件中使用:引入 js 引入 html 結(jié)構(gòu)
import Swiper from 'swiper'
html 結(jié)構(gòu):
1、開始先放個(gè)圖片占個(gè)位置確定布局,再把圖片換成下面的結(jié)構(gòu)
2、注意最外層的 class="swiper-container" 必須!且后面的 swiper 實(shí)例也要改!
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="(img,index) in bannerList" :key="index">
<img :src="img.imgUrl" />
</div>
</div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-pagination"></div>
</div>
3、最后關(guān)鍵是創(chuàng)建 swiper 實(shí)例! 有兩種方式
方式一:
如果圖片已經(jīng)固定(或圖片url數(shù)組已經(jīng)確定 )那么直接在 mounted 函數(shù)中創(chuàng)建
mounted() {
// 下面是普通swiper模板
new Swiper(".swiper-container", {
loop: true,
mousewheel: true,
keyboard: true,
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
pagination: {
el: ".swiper-pagination",
},
});
}
方式二:
用到 v-for 遍歷圖片url數(shù)組(并且該數(shù)組是在本組件中通過發(fā)請求獲取的),那么就要用到 watch + $nextTick
5.11.1 watch+$nextTick
當(dāng)一個(gè)數(shù)據(jù)發(fā)生改變時(shí),此時(shí) DOM 還沒有更新,所以在監(jiān)視屬性中的 handle 函數(shù)中 寫一個(gè) $nextTick 可以實(shí)現(xiàn) 數(shù)據(jù)發(fā)生改變且 DOM 更新后執(zhí)行代碼
回到 swiper ,我們在這個(gè)時(shí)候 創(chuàng)建 swiper 實(shí)例
bannerList:圖片url數(shù)組
watch: {
bannerList: {
handler() {
this.$nextTick(function() {
new Swiper(".swiper-container", {
loop: true,
mousewheel: true,
keyboard: true,
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
pagination: {
el: ".swiper-pagination",
},
});
})
}
}
},
5.11.2 修改分頁器樣式
1、添加屬性
pagination: {
el: ".swiper-pagination",
clickable: true,
bulletClass : 'my-bullet', // 這個(gè)
bulletActiveClass: 'my-bullet-active',
},
2、在組件里面寫 css (不要加 scope)
// 分頁器樣式
.my-bullet{
position: relative;
display: inline-block;
width: 15px;
height: 15px;
border-radius: 100%;
background: black;
opacity: 0.5;
margin: 0 4px;
}
// 選中的分頁器樣式(會繼承上面那個(gè)樣式)
.my-bullet-active {
background: #ff6600;
opacity: 1;
}
5.11.3 封裝輪播圖組件
當(dāng)一個(gè)圖片需要變?yōu)檩啿D時(shí),我們把 img 標(biāo)簽 換成 Carousel 組件即可!
1、Carousel 組件需要一個(gè)參數(shù):圖片 url 數(shù)組
imgList = [
{imgUrl: '...'}
{imgUrl: '...'}
]
2、將 Carousel 組件注冊為全局組件
// 在 components 中新建 Carousel 文件夾 // main.js import Carousel from '@/components/Carousel' Vue.component(Carousel.name,Carousel)
3、Carousel/index.vue (直接照搬即可 樣式可自行修改)
<template>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="(img,index) in imgList" :key="index">
<img :src="img.imgUrl" />
</div>
</div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-pagination"></div>
</div>
</template>
<script>
import Swiper from 'swiper'
export default {
name: 'Carousel',
props: ['imgList'],
watch: {
imgList: {
immediate: true,
handler() {
this.$nextTick(function() {
new Swiper(".swiper-container", {
loop: true,
pagination: {
el: ".swiper-pagination",
clickable: true,
bulletClass : 'my-bullet',
bulletActiveClass: 'my-bullet-active',
},
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
});
})
}
}
}
}
</script>
<style lang="less">
// 分頁器樣式
.my-bullet{
position: relative;
display: inline-block;
width: 15px;
height: 15px;
border-radius: 100%;
background: black;
opacity: 0.5;
margin: 0 4px;
}
// 選中的分頁器樣式(會繼承上面那個(gè)樣式)
.my-bullet-active {
background: #ff6600;
opacity: 1;
}
</style>
4、組件中使用(傳入圖片 url 數(shù)組即可)
<Carousel :imgList="bannerList" />
5.11.4 swiper 屬性
1、 <div class="swiper-container">:為輪播圖大盒子
2、<div class="swiper-slide">:為裝圖片的盒子,可以指定大小,那么圖片直接適配。或者不指定大小,則需要指定圖片大小。
3、slidesPerView:設(shè)置 輪播圖大盒子 顯示 輪播圖 張數(shù),輪播圖 會被修改寬度適配 輪播圖大盒子
4、slidesPerGroup:每次切換 輪播圖 張數(shù)
5、給 <div class="swiper-slide"> 添加類名 swiper-no-swiping :禁止滑動
以上就是vue2項(xiàng)目之swiper.js 的使用的詳細(xì)內(nèi)容,更多關(guān)于vue2項(xiàng)目之swiper.js 的使用的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue3中頁面跳轉(zhuǎn)兩種實(shí)現(xiàn)方式
在Vue3中Vue?Router是一個(gè)常用的路由管理庫,它提供了一種簡單而強(qiáng)大的方式來實(shí)現(xiàn)路由跳轉(zhuǎn)和導(dǎo)航,這篇文章主要給大家介紹了關(guān)于vue3中頁面跳轉(zhuǎn)的兩種實(shí)現(xiàn)方式,需要的朋友可以參考下2024-09-09
使用Vue3+PDF.js實(shí)現(xiàn)PDF預(yù)覽功能
項(xiàng)目中有一個(gè)需要預(yù)覽下載pdf的需求,網(wǎng)上找了很久,決定使用 pdf.js 完成,下面這篇文章主要給大家介紹了關(guān)于使用Vue3+PDF.js實(shí)現(xiàn)PDF預(yù)覽功能的相關(guān)資料,需要的朋友可以參考下2022-12-12
vue項(xiàng)目實(shí)現(xiàn)圖形驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目實(shí)現(xiàn)圖形驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
Vue實(shí)現(xiàn)刷新當(dāng)前頁面的三種方式總結(jié)
項(xiàng)目當(dāng)中如果做新增/修改/刪除等等操作通常情況下都需要刷新數(shù)據(jù)或者刷新當(dāng)前頁面。本文為大家整理了三種不同的實(shí)現(xiàn)方法,需要的可以參考一下2023-01-01

