本文實例為大家分享了Vue制作登錄主頁動態背景短視頻的具體代碼,供大家參考,具體內容如下
一、HTML代碼
<source src="../assets/video/G1s.mp4" type="video/mp4"/>
注:src的路徑根據自己的需要改變;
video標簽無法自動播放 刷新后無法自動播放;
解決方法:給video標簽添加muted屬性,可寫為muted或完整寫法:muted=“muted”
<template> <div class="homepage-hero-module"> <div class="video-container"> <div : class="filter"> <!--內容--> </div> <video : autoplay loop muted class="fillWidth" v-on:canplay="canplay"> <source src="../assets/video/G1s.mp4" type="video/mp4"/> 瀏覽器不支持 video 標簽,建議升級瀏覽器。 <source src="../assets/video/G1w.webm" type="video/webm"/> 瀏覽器不支持 video 標簽,建議升級瀏覽器。 </video> <div class="poster hidden" v-if="!vedioCanPlay"> <img : src="../assets/video/G1.jpg" alt=""> </div> </div> </div> </template>
二、css代碼
<style scoped>
.homepage-hero-module,
.video-container {
position: relative;
height: 100vh;
overflow: hidden;
}
.video-container .poster img{
z-index: 0;
position: absolute;
}
.video-container .filter {
z-index: 1;
position: absolute;
background: rgba(0, 0, 0, 0.4);
width: 100%;
}
.fillWidth {
width: 100%;
}
</style>
三、JavaScript----代碼
name: ' *** ' 自定義
export default {
name: 'Video',
data() {
return {
vedioCanPlay: false,
fixStyle: ''
}
},
methods: {
canplay() {
this.vedioCanPlay = true
}
},
mounted: function() { //屏幕自適應
window.onresize = () => {
const windowWidth = document.body.clientWidth
const windowHeight = document.body.clientHeight
const windowAspectRatio = windowHeight / windowWidth
let videoWidth
let videoHeight
if (windowAspectRatio < 0.5625) {
videoWidth = windowWidth
videoHeight = videoWidth * 0.5625
this.fixStyle = {
height: windowWidth * 0.5625 + 'px',
width: windowWidth + 'px',
'margin-bottom': (windowHeight - videoHeight) / 2 + 'px',
'margin-left': 'initial'
}
} else {
videoHeight = windowHeight
videoWidth = videoHeight / 0.5625
this.fixStyle = {
height: windowHeight + 'px',
width: windowHeight / 0.5625 + 'px',
'margin-left': (windowWidth - videoWidth) / 2 + 'px',
'margin-bottom': 'initial'
}
}
}
window.onresize()
}
}
四、效果演示
由于上傳大小限制,只能剪短的gif動畫了。

五、HTML版
視頻呢下載和html版代碼在官網最下方:https://coverr.co
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。