<template>
<swiper :options="swiperOptionVideo" ref="mySwiper" @slideChange="slideChangeFunc">
<swiper-slide v-for="(item, index) in videoList" :key="item.id" :id="'video' + index">
<video :ref="'video' + index" :class="'video' + index" :src="item.url" muted="true" autoplay="autoplay" loop="loop" />
</swiper-slide>
<div class="swiper-pagination" slot="pagination"></div>
</swiper>
</template>
<script>
export default {
data () {
return {
videoList: [],
swiperOptionVideo: {
loop: true,
autoplay: {
disableOnInteraction: false,
delay: 15000
},
speed: 800,
pagination: {
el: '.swiper-pagination',
clickable: true,
bulletClass: 'my-bullet',
bulletActiveClass: 'my-bullet-active'
}
},
isNotMutedRef: 'video0',
isClick: false,
activeRef: 'video0'
}
},
watch: {
'$store.state.isClick': {
handler: function (newVal, oldVal) {
this.func()
}
}
},
created () {
this.isClick = this.$store.state.isClick
this.initData()
},
methods: {
initData (num) {
this.$request(this.$api.getData, {
type: num,
floor: 0
}).then(res => {
res = res.data
this.videoList = res.data
if (this.isClick) {
this.$nextTick(() => {
this.$refs[this.activeRef][0].play()
this.$refs[this.activeRef][0].muted = false
this.isNotMutedRef = this.activeRef
})
}
})
},
func () {
if (!this.isClick) {
this.isClick = true
this.$store.commit('SET_IS_CLICK', this.isClick)
this.$refs[this.activeRef][0].play()
this.$refs[this.activeRef][0].muted = false
this.isNotMutedRef = this.activeRef
}
},
slideChangeFunc (e) {
let index = this.$refs.mySwiper.swiper.realIndex
this.activeRef = 'video' + index
if (this.isClick) {
if (this.isNotMutedRef) {
this.$refs[this.isNotMutedRef][0].muted = true
}
this.$refs['video' + index][0].muted = false
this.$refs['video' + index][0].play()
this.isNotMutedRef = 'video' + index
}
}
}
}
</script>