安装video.js
npm install video.js
在main.js中引用
import Video from 'video.js'
import 'video.js/dist/video-js.css'
Vue.prototype.$video = Video
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})
页面使用
<template>
<div class="district-page">
<div class="district-text">
<div v-for="item in videoList" :key="item.id" style="margin-top:15px">
<div class="test_two_box">
<video :id="'wVideo'+item.Id" class="video-js" style="width:100%;height:100%">
<source :src="item.Url" type="video/mp4" />
</video>
</div>
</div>
</div>
</div>
</template>
<script>
import { CorpStyleList } from '@/apis/apis'
export default {
data() {
return {
videoList: [],
videoHtmlList: '',
}
},
created() {
this.GetVideo()
},
methods: {
GetVideo() {
CorpStyleList({TopNum:0}).then(res => {
this.videoList = res;
if(this.videoList.length > 0 ){
this.$nextTick(() => {
this.initVideo();
})
}
})
},
initVideo(){
let _this = this;
this.videoList.forEach((element,i) => {
let myPlayer = this.$video(`wVideo${element.Id}`, {
controls: true,
autoplay: false,
preload: "auto",
width: "300px",
height: "150px",
poster: element.CoverUrl
});
myPlayer.on("play", function(){
_this.videoList.forEach((element,i) => {
let elsePlayer = _this.$video(`wVideo${element.Id}`);
if(elsePlayer.id_ !== myPlayer.id_){
elsePlayer.pause();
}
});
});
});
},
},
destroyed() {
this.videoList.forEach((element,i) => {
let myPlayer = this.$video(`wVideo${element.Id}`);
myPlayer.dispose()
});
}
};
</script>
<style lang="less" scoped>
.district-page {
text-align: left;
.district-text {
.test_two_box {
width: 300px;
height: 150px;
margin: 0 auto;
.video-js {
width: 100%;
height: 100%;
background-color: #f8f8f8;
/deep/.vjs-big-play-button {
width: 60px;
height: 60px;
border-radius: 50%;
line-height: 60px;
left: 50%;
top: 50%;
transform: translate(-30px,-30px);
border: 1px solid #fff;
}
/deep/.vjs-control-bar {
width: 100%;
height: 30px;
line-height: 30px;
.vjs-play-control,
.vjs-volume-panel,
.vjs-fullscreen-control {
width: 30px;
.vjs-button {
width: 100%;
}
}
.vjs-time-control {
padding: 0;
line-height: 30px;
}
.vjs-picture-in-picture-control {
display: none!important;
}
.vjs-progress-control {
.vjs-play-progress:before {
top: -13px;
right: -6px;
}
}
.vjs-volume-control {
display: none!important;
}
}
}
}
}
}
</style>
<style lang="less">
.vjs-paused .vjs-big-play-button,
.vjs-paused.vjs-has-started .vjs-big-play-button {
display: block;
}
</style>