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ā)請(qǐng)求獲取的),那么就要用到 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 ,我們?cè)谶@個(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; } // 選中的分頁器樣式(會(huì)繼承上面那個(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 組件注冊(cè)為全局組件
// 在 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; } // 選中的分頁器樣式(會(huì)繼承上面那個(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">
:為裝圖片的盒子,可以指定大小,那么圖片直接適配?;蛘卟恢付ù笮。瑒t需要指定圖片大小。
3、slidesPerView
:設(shè)置 輪播圖大盒子 顯示 輪播圖 張數(shù),輪播圖 會(huì)被修改寬度適配 輪播圖大盒子
4、slidesPerGroup
:每次切換 輪播圖 張數(shù)
5、給 <div class="swiper-slide">
添加類名 swiper-no-swiping
:禁止滑動(dòng)
以上就是vue2項(xiàng)目之swiper.js 的使用的詳細(xì)內(nèi)容,更多關(guān)于vue2項(xiàng)目之swiper.js 的使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決vue動(dòng)態(tài)下拉菜單 有數(shù)據(jù)未反應(yīng)的問題
這篇文章主要介紹了解決vue動(dòng)態(tài)下拉菜單 有數(shù)據(jù)未反應(yīng)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08vue 獲取url參數(shù)、get參數(shù)返回?cái)?shù)組的操作
這篇文章主要介紹了vue 獲取url參數(shù)、get參數(shù)返回?cái)?shù)組的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11Vue.js結(jié)合SortableJS實(shí)現(xiàn)樹形數(shù)據(jù)拖拽
本文主要介紹了Vue.js結(jié)合SortableJS實(shí)現(xiàn)樹形數(shù)據(jù)拖拽,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05vue3 組合式API defineEmits() 與 emits 組
在Vue中,defineEmits()是Vue3組合式API中用于聲明自定義事件的,而emits選項(xiàng)則用于Vue2和Vue3的選項(xiàng)式API中,defineEmits()允許使用字符串?dāng)?shù)組或?qū)ο笮问铰暶魇录?emits選項(xiàng)也支持這兩種形式,且驗(yàn)證函數(shù)可以驗(yàn)證事件參數(shù),這兩種方法都是為了更規(guī)范地在組件間通信2024-09-09Vue $emit $refs子父組件間方法的調(diào)用實(shí)例
今天小編就為大家分享一篇Vue $emit $refs子父組件間方法的調(diào)用實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09解決ant Design中this.props.form.validateFields未執(zhí)行的問題
這篇文章主要介紹了解決ant Design中this.props.form.validateFields未執(zhí)行的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10vue3如何實(shí)現(xiàn)在style中使用響應(yīng)式變量
vue3已經(jīng)內(nèi)置了這個(gè)功能啦,可以在style中使用v-bind指令綁定script模塊中的響應(yīng)式變量,這篇文章我們來講講vue是如何實(shí)現(xiàn)在style中使用script模塊中的響應(yīng)式變量,感興趣的朋友一起看看吧2024-07-07