vue3中使用swiper及遇到的問(wèn)題解析
一、安裝swiper
使用npm install swiper安裝swpier插件
npm install swiper -s // @9.2.0 // 或者安裝指定版本 npm install swiper@8.4.7 -s
二、使用swiper
直接按照官網(wǎng)的引用方法,項(xiàng)目會(huì)報(bào)錯(cuò)
解決方法:
引入的組件使用以下路徑
import { Swiper, SwiperSlide } from "swiper/vue/swiper-vue"; import "swiper/swiper.min.css";
有時(shí)還需要使用一些其他的組件API,如:
<template> ? <swiper ? ? class="swpier" ? ? :modules="modules" ? ? :slides-per-view="3" ? ? :space-between="50" ? ? loop="true" ? ? direction="horizontal" ? ? navigation ? ? :autoplay="{ ? ? ? delay: 2000, ? ? ? disableOnInteraction: false, ? ? ? pauseOnMouseEnter: true, ? ? }" ? ? :pagination="{ clickable: true }" ? ? :scrollbar="{ draggable: true }" ? ? @swiper="onSwiper" ? ? @slideChange="onSlideChange" ? > ? ? <swiper-slide>Slide 1</swiper-slide> ? ? <swiper-slide>Slide 2</swiper-slide> ? ? <swiper-slide>Slide 3</swiper-slide> ? ? <swiper-slide>Slide 4</swiper-slide> ? ? <swiper-slide>Slide 5</swiper-slide> ? </swiper> </template> <script> // Navigation: 方向箭頭:向左,向右 /* Pagination: 輪播小圓點(diǎn) clickable: 如果為true,則單擊分頁(yè)按鈕將跳轉(zhuǎn)到對(duì)應(yīng)的slide。僅適用于項(xiàng)目符號(hào)分頁(yè)類(lèi)型 */ /* Scrollbar: 滾動(dòng)條 draggable: 設(shè)置為true可使?jié)L動(dòng)條可拖動(dòng),從而控制滑塊位置 */ /* AutoPlay: 自動(dòng)輪播 delay: 轉(zhuǎn)換之間的延遲(毫秒)。如果未指定此參數(shù),將禁用自動(dòng)播放 disableOnInteraction:設(shè)置為false,用戶(hù)交互(滑動(dòng))后自動(dòng)播放不會(huì)被禁用,每次交互后都會(huì)重新啟動(dòng) pauseOnMouseEnter:?jiǎn)⒂煤?,指針(鼠?biāo))在Swiper容器上輸入時(shí)將暫停自動(dòng)播放。 */
import { Navigation, Pagination, Scrollbar, A11y, Autoplay } from "swiper"; import { Swiper, SwiperSlide } from "swiper/vue/swiper-vue"; import "swiper/swiper.min.css"; import "swiper/modules/navigation/navigation.min.css"; import "swiper/modules/pagination/pagination.min.css"; import "swiper/modules/scrollbar/scrollbar.min.css"; import "swiper/modules/autoplay/autoplay.min.css"; export default { ? components: { ? ? Swiper, ? ? SwiperSlide, ? }, ? setup() { ? ? const onSwiper = (swiper) => { ? ? ? console.log(swiper); ? ? }; ? ? const onSlideChange = () => { ? ? ? console.log("slide change"); ? ? }; ? ? return { ? ? ? onSwiper, ? ? ? onSlideChange, ? ? ? modules: [Navigation, Pagination, Scrollbar, A11y, Autoplay], ? ? }; ? }, }; </script> <style> .swpier { ? height: 200px; } .swiper-slide { ? width: 100px; ? line-height: 40px; ? font-size: 30px; ? text-align: center; ? border: 1px solid pink; } </style>
其中:
loop: 是否循環(huán)播放:true/false
direction:輪播方向"horizontal"/“vertical”,默認(rèn)"horizontal"
slides-per-view:控制一次顯示幾張輪播圖
space-between: 每張輪播圖之間的距離,該屬性不可以和margin 屬性同時(shí)使用;
三、echarts+swiper
項(xiàng)目背景
需要在swiper的每個(gè)輪播項(xiàng)中展示并包含不同的echarts,且開(kāi)啟loop:true循環(huán)
“echarts”: “^5.4.0”
“swiper”: “^9.2.0”
問(wèn)題描述:
開(kāi)啟loop:true后,第一個(gè)和最后一個(gè)echarts無(wú)法正常渲染
原因:
loop:true后swiper會(huì)在前后復(fù)制同樣的slide保證循環(huán)效果,初始化時(shí)使用的ID就不是唯一的了,導(dǎo)致echarts初始化無(wú)效了
解決方法:
// 通過(guò)class獲取dom,并在循環(huán)時(shí)初始化,為了保證echarts初始化時(shí)dom已經(jīng)更新渲染,加一個(gè)setTimeout函數(shù) ?setTimeout(() => { ? const myEchart = document.getElementsByClassName(classname); ? let chart = null; ? Array.prototype.forEach.call(myEchart, function (element, i, arr) { ? ? element.setAttribute("_echarts_instance_", ""); ? ? chart = echarts.init(element); ? ? console.log(arr); ? ? chart.setOption(optionArr[i]); ? }); })
或者使用for (let i = 0; i < myEchart.length; i++) {}循環(huán)遍歷dom
注意:這里不是能用for (let i in myEchart) {},否則會(huì)報(bào)錯(cuò)如下:
還有點(diǎn)擊失效的問(wèn)題可參考這里
到此這篇關(guān)于vue3中使用swiper及遇到的問(wèn)題的文章就介紹到這了,更多相關(guān)vue3使用swiper內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
優(yōu)選七個(gè)用于vue開(kāi)發(fā)的JS庫(kù)
這篇文章主要介紹了JavaScript優(yōu)選七個(gè)用于vue開(kāi)發(fā)庫(kù),借助開(kāi)源庫(kù)加速Vue項(xiàng)目的開(kāi)發(fā)進(jìn)度是現(xiàn)代前端開(kāi)發(fā)比較常見(jiàn)的方式,平常收集一些JavaScript庫(kù)介紹,在遇到需要的時(shí)候可以信手拈來(lái)2023-02-02vue3使用高德地圖,自定義點(diǎn)標(biāo)記、默認(rèn)點(diǎn)聚合樣式、點(diǎn)擊點(diǎn)標(biāo)記獲取信息
這篇文章主要介紹了vue3使用高德地圖,自定義點(diǎn)標(biāo)記、默認(rèn)點(diǎn)聚合樣式、點(diǎn)擊點(diǎn)標(biāo)記獲取信息的相關(guān)知識(shí),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-01-01vue實(shí)現(xiàn)頁(yè)面內(nèi)容禁止選中功能,僅輸入框和文本域可選
今天小編就為大家分享一篇vue實(shí)現(xiàn)頁(yè)面內(nèi)容禁止選中功能,僅輸入框和文本域可選,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11ant-design表單處理和常用方法及自定義驗(yàn)證操作
這篇文章主要介紹了ant-design表單處理和常用方法及自定義驗(yàn)證操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10Vue echarts模擬后端數(shù)據(jù)流程詳解
在平常的項(xiàng)目中,echarts圖表我們也是使用的非常多的,通常我們從后端拿到數(shù)據(jù),需要在圖表上動(dòng)態(tài)的進(jìn)行呈現(xiàn),接下來(lái)我們就模擬從后端獲取數(shù)據(jù)然后進(jìn)行呈現(xiàn)的方法2022-09-09vue+element開(kāi)發(fā)一個(gè)谷歌插件的全過(guò)程
這篇文章主要給大家介紹了關(guān)于vue+element開(kāi)發(fā)一個(gè)谷歌插件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05