vue調(diào)用swiper插件步驟教程(最易理解且詳細(xì))
我們最開(kāi)始接觸的是在操作dom時(shí)候的時(shí)候引入swiper,那么這次我就用之前的文檔來(lái)教你在vue中如何調(diào)用swiper.
我們之前看的是swiper教程
那么我根據(jù)上面的教程一步一步來(lái)教你使用
1.首先創(chuàng)建好swiper組件.寫(xiě)好template 里面的標(biāo)簽內(nèi)容(當(dāng)然這里寫(xiě)了個(gè)插槽為了以后方便動(dòng)態(tài)插入輪播的內(nèi)容),你直接復(fù)制教程里的代碼就好了
<template> <div class="swiper"> <div class="swiper-wrapper"> <slot></slot> </div> <!-- 如果需要分頁(yè)器 --> <div class="swiper-pagination"></div> <!-- 如果需要導(dǎo)航按鈕 --> <div class="swiper-button-prev"></div> <div class="swiper-button-next"></div> <!-- 如果需要滾動(dòng)條 --> <!-- <div class="swiper-scrollbar"></div> --> </div> </template> <script>
2.以vue的形式引入文件(就想之前的把swiper庫(kù)導(dǎo)入)
先下載swiper庫(kù), cnpm i --save swiper
這里npm ,cnpm都是可以的.下載好庫(kù)后,就可以在我們的script中導(dǎo)入了
import Swiper from 'swiper/bundle'; import 'swiper/swiper-bundle.css'
3.在掛載的時(shí)候初始化swiper,掛載這是vue的生命周期,這里不知道的話需要查一查
<script> //引入swiper文件 js css // import Swiper from 'swiper/bundle'; import 'swiper/swiper-bundle.css' export default{ mounted(){ //掛在后的去初始化swiper new Swiper ('.swiper', { direction: 'horizontal', // 垂直切換選項(xiàng) loop: true, // 循環(huán)模式選項(xiàng) // 如果需要分頁(yè)器 pagination: { el: '.swiper-pagination', }, // 如果需要前進(jìn)后退按鈕 navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, autoplay: { delay: 1000, stopOnLastSlide: false, disableOnInteraction: true, }, // 如果需要滾動(dòng)條 // scrollbar: { // el: '.swiper-scrollbar', // }, }) } } </script>?
4.我們的swiper組件就配置好了,那么我們只要引入swiper組件進(jìn)入我們的根組件下了.輪播圖就能運(yùn)轉(zhuǎn)了. 我這里為了美觀些把<div class="swiper-slide"> </div>又當(dāng)成組件封裝了起來(lái).所以這個(gè)項(xiàng)目是一根兩件.但是你template要是直接復(fù)制教程里的話恭喜你,你的輪播就可以運(yùn)轉(zhuǎn)了.后面是為了模仿真實(shí)情況,動(dòng)態(tài)插入輪播圖的話就繼續(xù)看下去.
根主件
<template> <div> <film-swiper v-if="lists.length"> <film-swiper-slider v-for="item in lists" :key="item" :mysrc="item"> </film-swiper-slider> </film-swiper> <router-view></router-view> </div> </template> <script> //webwa 要引入css呀 import filmSwiper from '../components/films/FilmSwiper.vue' import flimeSwiperSlider from '../components/films/FilmSwiperSlider.vue' export default { components:{ filmSwiper:filmSwiper, filmSwiperSlider:flimeSwiperSlider }, data(){ return{ lists:['https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwx1.sinaimg.cn%2Fmw690%2F4e39f084ly1h7os7gw9q6j20bn0fjq3t.jpg&refer=http%3A%2F%2Fwx1.sinaimg.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1670337717&t=c3b4e15be46c2705dc527f0b70f36d3d','https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202109%2F11%2F20210911134617_4f6a7.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1670337717&t=ca4dcfb56726444c635d201ad0f88fae','https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202104%2F04%2F20210404122758_2c825.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1670337717&t=d7f902f79caac3d938a2a927c6951205'] } } } </script> <style scoped> *{ padding: 0; margin: 0; } </style>
第二個(gè)組件
<template> <div class="swiper-slide"> <img :src="mysrc"> </div> </template> <script> export default { props:{ mysrc:String, }, } </script> <style scoped> img{ width: 100%; height: 100%; background-size: contain; background-repeat: no-repeat; } </style>
總結(jié)
到此這篇關(guān)于vue調(diào)用swiper插件的文章就介紹到這了,更多相關(guān)vue調(diào)用swiper插件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 路由頁(yè)面之間實(shí)現(xiàn)用手指進(jìn)行滑動(dòng)的方法
下面小編就為大家分享一篇vue 路由頁(yè)面之間實(shí)現(xiàn)用手指進(jìn)行滑動(dòng)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨跟隨小編過(guò)來(lái)看看吧2018-02-02Vue項(xiàng)目本地沒(méi)有問(wèn)題但部署到服務(wù)器上提示錯(cuò)誤(問(wèn)題解決方案)
一個(gè) VUE 的項(xiàng)目在本地部署沒(méi)有問(wèn)題,但是部署到服務(wù)器上的時(shí)候提示訪問(wèn)資源的錯(cuò)誤,遇到這樣的問(wèn)題如何解決呢?下面小編給大家?guī)?lái)了Vue項(xiàng)目本地沒(méi)有問(wèn)題但部署到服務(wù)器上提示錯(cuò)誤的解決方法,感興趣的朋友一起看看吧2023-05-05全面解析vue router 基本使用(動(dòng)態(tài)路由,嵌套路由)
路由,其實(shí)就是指向的意思,當(dāng)我點(diǎn)擊頁(yè)面上的home按鈕時(shí),頁(yè)面中就要顯示home的內(nèi)容,如果點(diǎn)擊頁(yè)面上的about 按鈕,頁(yè)面中就要顯示about 的內(nèi)容。這篇文章主要介紹了vue router 基本使用 ,需要的朋友可以參考下2018-09-09Vue使用Element實(shí)現(xiàn)增刪改查+打包的步驟
這篇文章主要介紹了Vue使用Element實(shí)現(xiàn)增刪改查+打包的步驟,幫助大家更好的理解和學(xué)習(xí)vue框架,感興趣的朋友可以了解下2020-11-11基于vue2.0實(shí)現(xiàn)的級(jí)聯(lián)選擇器
這篇文章主要介紹了基于vue2.0實(shí)現(xiàn)的級(jí)聯(lián)選擇器,基于Vue的級(jí)聯(lián)選擇器,可以單項(xiàng),二級(jí), 三級(jí)級(jí)聯(lián),多級(jí)級(jí)聯(lián),有興趣可以了解一下2017-06-06Vue數(shù)據(jù)監(jiān)聽(tīng)方法watch的使用
這篇文章主要介紹了Vue數(shù)據(jù)監(jiān)聽(tīng)方法watch的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03SpringBoot結(jié)合Vue3實(shí)現(xiàn)簡(jiǎn)單的前后端交互
本文主要介紹了SpringBoot結(jié)合Vue3實(shí)現(xiàn)簡(jiǎn)單的前后端交互,結(jié)合實(shí)際案例,說(shuō)明了如何實(shí)現(xiàn)前后端數(shù)據(jù)的交互,具有一定的?參考價(jià)值,感興趣的可以了解一下2023-08-08