vue使用swiper實(shí)現(xiàn)左右滑動切換圖片
本文實(shí)例為大家分享了vue使用swiper實(shí)現(xiàn)左右滑動切換圖片的具體代碼,供大家參考,具體內(nèi)容如下
使用npm 安裝vue-awesome-swiper
npm install vue-awesome-swiper --save
在main.js中引用
import VueAwesomeSwiper from 'vue-awesome-swiper' Vue.user(VueAwesomeSwiper) import 'swiper/dist/css/swiper.css'
在組件中使用
<template>
<div>
<label class="timeline">{{ time }}</label>
<div id="star-pic-vue">
<template v-if="data">
<img
e
v-for="(item, index) in images"
:src="item.url"
:key="index"
id="contract_url"
@click="enlargePic(index)"
/>
<template v-if="isDialogShow"> </template>
<el-dialog
:visible.sync="centerDialogVisible"
width="100%"
modal
close-on-click-modal
custom-class="dialog"
>
<swiper :options="swiperOption" ref="mySwiper" style="height: 100%;">
<swiper-slide v-for="(img, index) in images" :key="index">
<div class="swiper-zoom-container">
<img :src="img.url" alt="" />
</div>
</swiper-slide>
</swiper>
</el-dialog>
</template>
</div>
</div>
</template>
<script>
import { swiper, swiperSlide } from "vue-awesome-swiper";
export default {
name: "PictureComponent",
props: ["data", "maxShow", "time"],
data() {
return {
centerDialogVisible: false,
showPic: "",
isDialogShow: false,
activeIndex: 1,
startX: 0,
swiperOption: {
width: window.innerWidth,
zoom: true,
initialSlide: 0
}
};
},
computed: {
images() {
if (this.data instanceof Array && this.data.length > 2) {
var value = this.data;
return value.splice(0, this.maxShow);
} else {
return this.data;
}
}
},
components: {
swiper,
swiperSlide
},
methods: {
// 放大圖片
enlargePic(i) {
this.activeIndex = i;
this.isDialogShow = true;
// 使用$refs,如果ref是定位在有v-if、v-for、v-show中的DOM節(jié)點(diǎn),
// 返回來的只能是undefined,因?yàn)樵趍ounted階段他們根本不存在
this.$nextTick(() => {
var swiper = this.$refs.mySwiper.swiper;
swiper.activeIndex = i;
});
this.centerDialogVisible = true;
}
}
};
</script>
<style lang="scss">
.timeline {
display: block;
margin: 10px 20px 5px;
}
#star-pic-vue .el-dialog__wrapper {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow: auto;
margin: 0;
background: #171717;
}
#star-pic-vue {
width: 100%;
height: auto;
display: flex;
flex-wrap: wrap;
justify-content: stretch;
padding: 3px 13px;
img {
width: 82px;
height: 80px;
margin: 4px 0px 0px;
padding-right: 2px;
}
.dialog {
img {
width: 100%;
height: 100%;
margin: 0;
}
}
.el-carousel__item h3 {
color: #475669;
font-size: 18px;
opacity: 0.75;
line-height: 300px;
margin: 0;
height: 100%;
width: 100%;
}
.el-dialog__header {
display: none;
}
.el-dialog__body {
padding: 0 !important;
margin: 0 !important;
height: 460px;
background: #171717;
}
.el-carousel {
height: 100%;
}
.el-carousel__container {
height: 410px;
}
.el-carousel__indicators--outside {
margin-top: 20px;
}
}
</style>
效果

$refs定位不到的主要原因是因?yàn)関-if、v-for、v-show這些語句如果依賴父組件傳來的參數(shù)的話,該參數(shù)是在mounted()階段子還沒獲取得到。
如果想要真正地在DOM加載完成后拿到數(shù)據(jù),就需要調(diào)用VUE的全局api : this.$nextTick(() => {})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
前端Vue手機(jī)號校驗(yàn)以及后端Java手機(jī)號校驗(yàn)例子
接收一個(gè)輸入的手機(jī)號,判斷輸入的手機(jī)號是否正確是一個(gè)很常見的功能,這篇文章主要給大家介紹了關(guān)于前端Vue手機(jī)號校驗(yàn)以及后端Java手機(jī)號校驗(yàn)的相關(guān)資料,需要的朋友可以參考下2023-11-11
離線搭建vue環(huán)境運(yùn)行項(xiàng)目詳細(xì)步驟
由于公司要求在內(nèi)網(wǎng)開發(fā)項(xiàng)目,而內(nèi)網(wǎng)不能連接外網(wǎng),因此只能離線安裝vue環(huán)境,下面這篇文章主要給大家介紹了關(guān)于離線搭建vue環(huán)境運(yùn)行項(xiàng)目的詳細(xì)步驟,需要的朋友可以參考下2023-11-11
vue3.0中ref與reactive的區(qū)別及使用場景分析
ref與reactive都是Vue3.0中新增的API,用于響應(yīng)式數(shù)據(jù)的處理,這篇文章主要介紹了vue3.0中ref與reactive的區(qū)別及使用,需要的朋友可以參考下2023-08-08
使用Vue實(shí)現(xiàn)一個(gè)樹組件的示例
這篇文章主要介紹了使用Vue實(shí)現(xiàn)一個(gè)樹組件的示例,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-11-11
webpack 3 + Vue2 使用dotenv配置多環(huán)境的步驟
這篇文章主要介紹了webpack 3 + Vue2 使用dotenv配置多環(huán)境,env文件在配置文件都可以用, vue頁面用的時(shí)候需要在 webpack.base.conf.js 重新配置,需要的朋友可以參考下2023-11-11

