1.banner 組件
components/Banner.vue
<!-- 輪播圖 組件 -->
<template>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="str in listImg" :style="{ backgroundImage: 'url(' + str.img + ')' }"></div>
</div>
<div class="swiper-pagination swiper-pagination-bullets"></div>
</div>
</template>
<script>
// npm install swiper --save
import Swiper from 'swiper';
import 'swiper/dist/css/swiper.min.css';
export default {
props: ['listImg'],
name: 'banner',
mounted() {
let mySwiper = new Swiper('.swiper-container', {
pagination: { // 按鈕
el: '.swiper-pagination',
clickable :true, // 分頁導航是否可點擊
},
loop: true, // 環路(無縫滾動)
speed: 600, // 切換速度
autoplay: { // 自動切換
delay: 3000, // 自動切換的時間間隔
stopOnLastSlide: false, // 如果設置為true,當切換到最后一個slide時停止自動切換(loop模式下無效)
disableOnInteraction: false, // 用戶操作swiper之后,是否禁止autoplay.默認為true:停止
}
});
}
}
</script>
<style lang="less" scoped>
.swiper-container {
width: 100%;
height: 200px;
.swiper-wrapper {
width: 100%;
height: 100%;
}
.swiper-slide {
background-position: center;
background-size: cover;
width: 100%;
height: 100%;
img {
width: 100%;
height: 100%;
}
}
}
</style>
2.swiper 組件
components/Swiper.vue
<template>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="item in swiper"><img :src="item.img" alt=""></div>
</div>
<div class="swiper-pagination swiper-pagination-bullets"></div>
</div>
</template>
<script>
// npm install swiper --save
import Swiper from 'swiper';
import 'swiper/dist/css/swiper.min.css';
export default {
name: 'swiper',
data() {
return {
mySwiper: null
}
},
props: ['swiper'], //swiper的就是test這個數據傳遞來的
methods: {
_initSwiper() {
this.mySwiper = new Swiper('.swiper-container', {
pagination: { // 按鈕
el: '.swiper-pagination',
clickable :true, // 分頁導航是否可點擊
},
loop: true, // 環路(無縫滾動)
speed: 600, // 切換速度
autoplay: { // 自動切換
delay: 3000, // 自動切換的時間間隔
stopOnLastSlide: false, // 如果設置為true,當切換到最后一個slide時停止自動切換(loop模式下無效)
disableOnInteraction: false, // 用戶操作swiper之后,是否禁止autoplay.默認為true:停止
}
})
},
_updateSwiper() {
this.$nextTick(() => {
this.mySwiper.update(true); //swiper update的方法
})
},
swiperUpdate() {
if (this.mySwiper) { //節點存在
this._updateSwiper(); //更新
} else {
this._initSwiper(); //創建
}
}
},
watch: {
//通過props傳來的數據 和 組件一加載節點就創建成功 二者不是同步,實時監聽的swiper(傳遞的值)的變化
swiper() {
this.swiperUpdate();
}
},
mounted() {
this.swiperUpdate(); //頁面一加載拉去數據創建節點
}
}
</script>
<style lang="less" scoped>
.swiper-container {
width: 100%;
height: 200px;
margin-top: 10px;
.swiper-wrapper {
width: 100%;
height: 100%;
.swiper-slide {
background-size: cover;
width: 100%;
height: 200px;
img {
width: 100%;
height: 100%;
}
}
}
}
</style>
3.頁面調用
<!-- 書影音 -->
<template>
<div>
<!-- 標題欄 -->
<mt-header title="書影音"></mt-header>
<!-- 輪播圖 組件一 -->
<banner :listImg="bannerList"></banner>
<!-- 輪播圖 組件二 -->
<swiper :swiper="bannerList"></swiper>
</div>
</template>
<script>
import Banner from '../components/Banner.vue'
import Swiper from '../components/Swiper.vue'
export default {
name: 'AudioBook',
components: {
Banner,
Swiper
},
data(){
return {
bannerList: [
{"type":"1","img":"http://www.youdingsoft.com/fileUploadsmall/20180119172411843341.jpg;","url":""},
{"type":"1","img":"http://www.youdingsoft.com/fileUploadsmall/20180119172434968049.jpg;","url":""},
{"type":"1","img":"http://www.youdingsoft.com/fileUploadsmall/20180119172503906167.jpg;","url":""},
{"type":"1","img":"http://www.youdingsoft.com/fileUploadsmall/20180119172518390352.jpg;","url":""},
{"type":"1","img":"http://www.youdingsoft.com/fileUploadsmall/20180119172540250495.jpg;","url":""},
{"type":"1","img":"http://www.youdingsoft.com/fileUploadsmall/20180119172552359735.jpg;","url":""}
]
}
}
}
</script>
<style lang="less" scoped>
//
</style>
4.效果圖

