swiper在vue中的簡單使用方法
本次使用的是 swiper5,swiper 不同版本在使用 的過程會有一些差別

說明:本次示例中 skuImageList 是圖片列表,由父組件從服務器獲取然后傳給這個輪播子組件使用
服務器返回的數(shù)據(jù)結構如下,一個數(shù)組包含了幾個對象,對象里有圖片

html 結構,根據(jù)自己的需要進行刪減,本次只保留了前進后退按鈕
<template>
<div style="width: 350px;">
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="(item,index) in skuImageList" :key="item.id">
<img :src="item.imgUrl" alt="" />
</div>
</div>
<!-- 如果需要導航按鈕 -->
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
</div>
</template>js部分
說明:本次使用swiper5,因為是從服務器獲取的數(shù)據(jù),所以放在了 updata 生命周期中。
本次設置了輪播顯示的圖片數(shù)量,以及輪播圖片的高度。
<script>
import Swiper from "swiper";
import "swiper/css/swiper.min.css";
export default {
name: "Smallswiper",
props: ["skuImageList"],
updated() {
new Swiper(".swiper-container", {
loop: true, // 循環(huán)模式選項
// 如果需要前進后退按鈕
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
slidesPerView: 3, // 顯示幾個
height:100, // swiperlide 高度
});
},slidesPerView: 3, // 顯示幾個 height:100, // swiperlide 高度。更多其他的自定義輪播設置可以參照官方 API

也可在 watch 監(jiān)聽數(shù)組 skuImageList 是否發(fā)生變化利用 thsi.$nextTick() 觸發(fā)頁面更新
watch: {
skuImageList() {
//保證數(shù)據(jù)發(fā)生修改,頁面結構再次渲染完畢。在初始化Swiper實例
this.$nextTick(() => {
//初始化Swiper類的實例
var mySwiper = new Swiper(".swiper-container", {
//設置輪播圖防線
direction: "horizontal",
// loop:true,
// 如果需要前進后退按鈕
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
//展示區(qū)域同時展示三張圖片
slidesPerView: 2,
});
});
},樣式:--swiper-navigation-size: 20px;/* 設置按鈕大小 */
<style lang="scss" scoped>
img {
width: 50px;
height: 75px;
}
.swiper-container {
width: 350px;
--swiper-navigation-size: 20px;/* 設置按鈕大小 */
}
.swiper-slide{
left: 30px;
}
</style>更多的其他設置可以參照 api 中文api - Swiper中文網(wǎng)

最后效果:

總結
到此這篇關于swiper在vue中的簡單使用的文章就介紹到這了,更多相關swiper在vue的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在Vuex使用dispatch和commit來調(diào)用mutations的區(qū)別詳解
今天小編就為大家分享一篇在Vuex使用dispatch和commit來調(diào)用mutations的區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
ArcGis?API?for?js在vue.js中的使用示例詳解
這篇文章主要為大家介紹了ArcGis?API?for?js在vue.js中的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
vue element-ui實現(xiàn)動態(tài)面包屑導航
這篇文章主要為大家詳細介紹了vue element-ui實現(xiàn)動態(tài)面包屑導航,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12
Vue開發(fā)配置tsconfig.json文件的實現(xiàn)
tsconfig.json文件中指定了用來編譯這個項目的根文件和編譯選項,本文就來介紹一下Vue開發(fā)配置tsconfig.json文件的實現(xiàn),感興趣的可以了解一下2023-08-08

