vue2項目之swiper.js 的使用
swiper.js 的使用
官網(wǎng) API(部署在 swiper 實例中):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、開始先放個圖片占個位置確定布局,再把圖片換成下面的結(jié)構(gòu)
2、注意最外層的 class="swiper-container"
必須!且后面的 swiper 實例也要改!
<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、最后關鍵是創(chuàng)建 swiper 實例! 有兩種方式
方式一:
如果圖片已經(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
當一個數(shù)據(jù)發(fā)生改變時,此時 DOM 還沒有更新,所以在監(jiān)視屬性中的 handle 函數(shù)中 寫一個 $nextTick 可以實現(xiàn) 數(shù)據(jù)發(fā)生改變且 DOM 更新后執(zhí)行代碼
回到 swiper ,我們在這個時候 創(chuàng)建 swiper 實例
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', // 這個 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; } // 選中的分頁器樣式(會繼承上面那個樣式) .my-bullet-active { background: #ff6600; opacity: 1; }
5.11.3 封裝輪播圖組件
當一個圖片需要變?yōu)檩啿D時,我們把 img 標簽 換成 Carousel 組件即可!
1、Carousel 組件需要一個參數(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; } // 選中的分頁器樣式(會繼承上面那個樣式) .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ù),輪播圖 會被修改寬度適配 輪播圖大盒子
4、slidesPerGroup
:每次切換 輪播圖 張數(shù)
5、給 <div class="swiper-slide">
添加類名 swiper-no-swiping
:禁止滑動
以上就是vue2項目之swiper.js 的使用的詳細內(nèi)容,更多關于vue2項目之swiper.js 的使用的資料請關注腳本之家其它相關文章!
相關文章
解決vue動態(tài)下拉菜單 有數(shù)據(jù)未反應的問題
這篇文章主要介紹了解決vue動態(tài)下拉菜單 有數(shù)據(jù)未反應的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08vue 獲取url參數(shù)、get參數(shù)返回數(shù)組的操作
這篇文章主要介紹了vue 獲取url參數(shù)、get參數(shù)返回數(shù)組的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11Vue.js結(jié)合SortableJS實現(xiàn)樹形數(shù)據(jù)拖拽
本文主要介紹了Vue.js結(jié)合SortableJS實現(xiàn)樹形數(shù)據(jù)拖拽,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05vue3 組合式API defineEmits() 與 emits 組
在Vue中,defineEmits()是Vue3組合式API中用于聲明自定義事件的,而emits選項則用于Vue2和Vue3的選項式API中,defineEmits()允許使用字符串數(shù)組或?qū)ο笮问铰暶魇录?emits選項也支持這兩種形式,且驗證函數(shù)可以驗證事件參數(shù),這兩種方法都是為了更規(guī)范地在組件間通信2024-09-09Vue $emit $refs子父組件間方法的調(diào)用實例
今天小編就為大家分享一篇Vue $emit $refs子父組件間方法的調(diào)用實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09解決ant Design中this.props.form.validateFields未執(zhí)行的問題
這篇文章主要介紹了解決ant Design中this.props.form.validateFields未執(zhí)行的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10