這篇文章將為大家詳細講解有關(guān)怎么在HTML5中實現(xiàn)一個全屏視頻背景,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
特性
自動調(diào)整:Bideo.js可以根據(jù)當前瀏覽器窗口的大小自動調(diào)整視頻尺寸,當瀏覽器窗口調(diào)整時,它會自適應(yīng)窗口尺寸,移動端、PC端都能自動調(diào)整,使視頻作為背景并全屏展示。
覆蓋:視頻作為網(wǎng)頁背景后,我們可以任意在視頻上層放置任意HTML內(nèi)容。
視頻封面:頁面打開時,視頻可能需要幾秒鐘才能加載完,那么我們可以設(shè)置一張圖片作為視頻的封面,等加載完再播放。
HTML
在你的頁面body中加入如下HTML代碼,很顯然, <video> 標簽是用來加載視頻的,屬性 loop 是指循環(huán)播放視頻, muted 是指靜音模式,即音量為0。 #video_cover 是默認的視頻封面。 #overlay 是遮罩層,所有其他頁面內(nèi)容在遮罩層上展示。
<div id="container"> <video id="background_video" loop muted></video> <div id="video_cover"></div> <div id="overlay"></div> <div id="video_controls"> <span id="play"> <img src="play.png"> </span> <span id="pause"> <img src="pause.png"> </span> </div> <section id="main_content"> <div id="head"> <h2>HTML5輕松實現(xiàn)全屏視頻背景-Bideo.js</h2> <p class="sub_head">一個可以輕松添加頁面全屏背景視頻的Javscript庫</p> <p class="info">(稍等片刻,視頻加載需要一點點時間.)</p> </div> </section> </div>
我們還添加了 #video_controls ,這個是用來控制視頻播放與暫停的,適用于手機移動端。最后你可以在接下來的 section 中添加任意你想展示的HTML內(nèi)容了。
CSS
CSS也是比較關(guān)鍵,最需要關(guān)注的是 #container 和 #background_video 的設(shè)置。以下css代碼直接拿去無需解釋:
* { margin: 0; padding: 0; } html, body { width: 100%; height: 100%; overflow: hidden; } #container { overflow: hidden; position: absolute; top: 0; left: 0; right: 0; bottom: 0; height: 100%; } #background_video { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); object-fit: cover; height: 100%; width: 100%; } #video_cover { position: absolute; width: 100%; height: 100%; background: url('video_cover.jpeg') no-repeat; background-size: cover; background-position: center; } #overlay { position: absolute; top: 0; right: 0; left: 0; bottom: 0; background: rgba(0,0,0,0.5); }
Javascript
首先加載Bideo庫:
<script src="bideo.js"></script>
接著實例化bideo: new Bideo() ,然后直接初始化加載,設(shè)置如下選項:
(function () { var bv = new Bideo(); bv.init({ // Video元素 videoEl: document.querySelector('#background_video'), // 容器元素 container: document.querySelector('body'), // 自適應(yīng)調(diào)整 resize: true, // autoplay: false, isMobile: window.matchMedia('(max-width: 768px)').matches, playButton: document.querySelector('#play'), pauseButton: document.querySelector('#pause'), // 加載視頻源, 根據(jù)實際業(yè)務(wù)更換自己的視頻源文件 src: [ { src: 'http://ak4.picdn.net/shutterstock/videos/4170274/preview/stock-footage-beautiful-girl-lying-on-the-meadow-and-dreaming-enjoy-nature-close-up-slow-motion-footage.mp4', type: 'video/mp4' }, { src: 'night.webm', type: 'video/webm;codecs="vp8, vorbis"' } ], // 一旦視頻加載后即將視頻封面隱藏 onLoad: function () { document.querySelector('#video_cover').style.display = 'none'; } }); }());
關(guān)于怎么在HTML5中實現(xiàn)一個全屏視頻背景就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
分享文章:怎么在HTML5中實現(xiàn)一個全屏視頻背景-創(chuàng)新互聯(lián)
URL分享:http://jinyejixie.com/article28/cecpjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、建站公司、App設(shè)計、移動網(wǎng)站建設(shè)、企業(yè)建站、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容