vue卡片化輪播圖組件
1.安裝swiper
npm install swiper
2.在組件中引入swiper
import 'swiper/dist/css/swiper.min.css'
import Swiper from "swiper" //版本是用的:"swiper": "^4.5.0"
3.頁面的代碼
<template>
<div>
<mt-header fixed title="處方示例">
<a slot="left" @click="$router.go(-1)">
<mt-button icon="back"></mt-button>
</a>
</mt-header>
<div class="examples">
<div class="bannerTxt">
{{bannerTxt}}
</div>
<div class="bannerBox">
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="(item,index) in banner" :key="index">
<img :src="item.picture_path" alt="">
</div>
</div>
</div>
</div>
<div class="bannerIndex">
<span>{{bannerIndex}}</span>/4
</div>
<div class="tips">
<img src="./imgs/icon_tz.png" alt="" class="tipsImg">
<p>請儘量保證上傳的處方文字清晰可見,並保證紅框內的信息完整不缺失</p>
</div>
</div>
</div>
</template>
<script>
import Swiper from 'swiper';
import 'swiper/dist/css/swiper.min.css';
export default {
data() {
return {
bannerIndex: 0,
bannerTxt: '手寫的處方',
banner: [
{
picture_txt: '手寫的處方',
picture_path: 'https://kmyhl.oss-cn-shenzhen.aliyuncs.com/e-medicine/NEXw6KmWH5.png',
},
{
picture_txt: '機打的處方',
picture_path: 'https://kmyhl.oss-cn-shenzhen.aliyuncs.com/e-medicine/ZdXbcS77SK.png',
},
{
picture_txt: '機打的處方',
picture_path: 'https://kmyhl.oss-cn-shenzhen.aliyuncs.com/e-medicine/BKfxNfcHpH.png',
},
{
picture_txt: '機打的處方',
picture_path: 'https://kmyhl.oss-cn-shenzhen.aliyuncs.com/e-medicine/d3TNrsh75a.png'
}
]
};
},
mounted() {
let that = this;
/* eslint-disable */
let mySwiper = new Swiper('.swiper-container', {
direction: 'horizontal', //滑動方向,可設置水平(horizontal)或垂直(vertical)。
loop: true, // 設置爲true 則開啓loop模式
autoplay: 3000, // 自動切換時間
slidesPerView: 'auto', // 設置slider容器能夠同時顯示的slides數量(carousel模式)。類型:number or auto
centeredSlides: true, // 設定爲true時,active slide會居中,而不是默認狀態下的居左。
spaceBetween: 10, // 在slide之間設置距離(單位px)。
loopAdditionaSlider: 0, // loop模式下會在slides前後複製若干個slide,,前後複製的個數不會大於原總個數。
on: {
slideChangeTransitionEnd: function(){
if(this.activeIndex === 8 || this.activeIndex === 4 ) {
that.bannerIndex = 1;
that.bannerTxt = '手寫的處方';
} else {
that.bannerIndex = this.activeIndex-3;
that.bannerTxt = '機打的處方';
}
},
},
});
},
};
</script>
<style lang="less" scoped>
.examples{
position: absolute;
top: 44px;
left: 0px;
right: 0px;
bottom: 0px;
overflow: auto;
background-color: #f5f0f0;
font-size: 14px;
.tips{
position: absolute;
bottom: 0px;
width: 100%;
height: 46px;
background-color: #b5654d;
text-align: center;
img{
width: 12px;
height: 14px;
position: absolute;
top: 7.5px;
left: 12vw;
color: #fff;
}
p{
width: 240px;
text-align: left;
color: #fff;
font-size: 12px;
position: absolute;
left: 18vw;
top: 7px;
line-height: 16px;
}
}
}
.bannerTxt{
height: 60px;
line-height: 60px;
text-align: center;
color: #666666;
font-size: 15px;
}
.bannerIndex{
line-height: 40px;
text-align: center;
color: #666666;
font-size: 15px;
text-align: center;
span{
color: #b5654d;
}
}
.bannerBox{
position: relative;
height: 380px;
}
.swiper-container {
width: 100%;
height: 100%;
}
.swiper-container .swiper-wrapper .swiper-slide {
width: 80% !important;
overflow: hidden;
display: flex;
align-items: center;
}
.swiper-container .swiper-wrapper .swiper-slide img {
width: 100%;
height: 380px;
border-radius: 5px;
}
.swiper-container .swiper-wrapper .swiper-slide-prev,.swiper-container .swiper-wrapper .swiper-slide-next{
height: 340px !important;
margin-top: 20px;
}
.swiper-container .swiper-wrapper .swiper-slide-prev img,.swiper-container .swiper-wrapper .swiper-slide-next img{
width: 100%;
height: 100%;
}
</style>
效果圖