使用jQuery和CSS自定義HTML5 Video 控件 簡單適用


Html5 Video是現在html5最流行的功能之一,得到了大多數最新版本的瀏覽器支持.包括IE9,也是如此.不同的瀏覽器提供了不同的原生態瀏覽器視頻空間.我們制作自定義視頻控件為了在所有的瀏覽器中有一個相同的Html5視頻控件而不受默認視頻控件的控制.

 HTML5 Video 基礎標簽

1  <video id="myVideo" controls poster="video.jpg" width="600" height="400" >
2     <source src="video.mp4" type="video/mp4" />
3     <source src="video.webm" type="video/webM" />
4     <source src="video.ogv" type="video/ogg" />
5     <p>Your browser does not support the video tag.</p>
6  </video>

video標簽最好包含mp4、webM和ogg這三種源視頻文件-可以跨瀏覽器。如果瀏覽器不支持html5,你可以使用flash作為后備!  

 開始制作 HTML5 Video Controls

  幸運的是HTML5 Video 的Api可以用JavaScript訪問,並使用他們來作為控制視頻的媒介.

  在編碼之前讓我簡單的介紹一下jQuery是如何獲取video標簽的.

  在JavaScript中我們使用getElementById('videoID')來獲取Video標簽,作為結果,我們會獲取到一個Dom對象.但是這不是等價的jQuery對象.$("videoID")會返回一個jQuery對象.不是Dom對象.這就是為什么在將其轉換為Dom對象之前我們不能直接使用jQuery選擇器調用/使用Html5 Video的Dom屬性和功能. 

1 //return a DOM object
2 var video = document.getElementById('videoID'); //or
3 var video = $('#videoID').get(0); //or
4 var video = $('#videoID')[0];
5  
6 //return a jQuery object
7 var video = $('#videoID');

Video Play/Pause Controls 播放/暫停 按鈕

  好的,這是所有的介紹.現在讓我們來編碼.首先,我們要創建一個簡單的播放/暫停按鈕. 

1 <div class="control">
2     <a href="#" class="btnPlay">Play/Pause</a>
3  </div>

我們可以輕松的控制Html5 Video的播放與暫停狀態.

 1 //Play/Pause control clicked
 2 $('.btnPlay').on('click', function() {
 3    if(video[0].paused) {
 4       video[0].play();
 5    }
 6    else {
 7       video[0].pause();
 8    }
 9    return false;
10 };

顯示視頻播放時間和持續時間

   Html5 Video支持視頻回放.這里我們要顯示視頻的當前播放時間和總時間.

<div class="progressTime">
   Current play time: <span class="current"></span>
   Video duration: <span class="duration"></span>
</div>

為了得到視頻的總時間,我們要確保視頻元數據已經加載.這個時候我們要用到Html5 Video的loadedmetadata事件.

  對於當前的視頻播放時間.我們可以用Html5 Video timeupdate事件來保證他的更新.

1 //get HTML5 video time duration
2 video.on('loadedmetadata', function() {
3    $('.duration').text(video[0].duration);
4 });
5  
6 //update HTML5 video current play time
7 video.on('timeupdate', function() {
8    $('.current').text(video[0].currentTime);
9 });

視頻進度條

  在這里我們將會把當前播放時間和總的時間長度轉換為更人性化的進度條.

 1 <style>
 2 .progressBar
 3 {
 4    position: relative;
 5    width: 100%;
 6    height: height:10px;
 7    backgroud-color: #000;
 8 }
 9 .timeBar
10 {
11    position: absolute;
12    top: 0;
13    left: 0;
14    width: 0;
15    height: 100%;
16    background-color: #ccc;
17 }
18 </style>
19 <div class="progressBar">
20    <div class="timeBar"></div>
21 </div>
 1 //get HTML5 video time duration
 2 video.on('loadedmetadata', function() {
 3    $('.duration').text(video[0].duration));
 4 });
 5  
 6 //update HTML5 video current play time
 7 video.on('timeupdate', function() {
 8    var currentPos = video[0].currentTime; //Get currenttime
 9    var maxduration = video[0].duration; //Get video duration
10    var percentage = 100 * currentPos / maxduration; //in %
11    $('.timeBar').css('width', percentage+'%');
12 });
 1 var timeDrag = false;   /* Drag status */
 2 $('.progressBar').mousedown(function(e) {
 3    timeDrag = true;
 4    updatebar(e.pageX);
 5 });
 6 $(document).mouseup(function(e) {
 7    if(timeDrag) {
 8       timeDrag = false;
 9       updatebar(e.pageX);
10    }
11 });
12 $(document).mousemove(function(e) {
13    if(timeDrag) {
14       updatebar(e.pageX);
15    }
16 });
17  
18 //update Progress Bar control
19 var updatebar = function(x) {
20    var progress = $('.progressBar');
21    var maxduration = video[0].duration; //Video duraiton
22    var position = x - progress.offset().left; //Click pos
23    var percentage = 100 * position / progress.width();
24  
25    //Check within range
26    if(percentage > 100) {
27       percentage = 100;
28    }
29    if(percentage < 0) {
30       percentage = 0;
31    }
32  
33    //Update progress bar and video currenttime
34    $('.timeBar').css('width', percentage+'%');
35    video[0].currentTime = maxduration * percentage / 100;
36 };

完成!

 進階-顯示緩沖欄

  我們需要給視頻制作一個緩沖欄讓用戶知道視頻加載了多少.

 1 <style>
 2 .progressBar {
 3    position: relative;
 4    width: 100%;
 5    height: height:10px;
 6    backgroud-color: #000;
 7 }
 8 .bufferBar {
 9    position: absolute;
10    top: 0;
11    left: 0;
12    width: 0;
13    height: 100%;
14    background-color: #ccc;
15 }
16 </style>
17 <div class="progressBar">
18    <div class="bufferBar"></div>
19 </div>

Html5 Video緩沖屬性將返回一個對象的緩存范圍.因此,我們將使用緩存數據的最后一個值.

 1 //loop to get HTML5 video buffered data
 2 var startBuffer = function() {
 3    var maxduration = video[0].duration;
 4    var currentBuffer = video[0].buffered.end(0);
 5    var percentage = 100 * currentBuffer / maxduration;
 6    $('.bufferBar').css('width', percentage+'%');
 7  
 8    if(currentBuffer < maxduration) {
 9       setTimeout(startBuffer, 500);
10    }
11 };
12 setTimeout(startBuffer, 500);

音量控制

  現在,我們要增加聲音控制.有兩種不同的音量控制方法.靜音按鈕/音量欄

<a href="#" class="muted" >Mute/Unmute</a>
<div class="volumeBar">
   <div class="volume"></div>
</div>

js

 1 //Mute/Unmute control clicked
 2 $('.muted').click(function() {
 3    video[0].muted = !video[0].muted;
 4    return false;
 5 });
 6  
 7 //Volume control clicked
 8 $('.volumeBar').on('mousedown', function(e) {
 9    var position = e.pageX - volume.offset().left;
10    var percentage = 100 * position / volume.width();
11    $('.volumeBar').css('width', percentage+'%');
12    video[0].volume = percentage / 100;
13 });

其他

  除了主要的控制插件.還可以做一些額外的控制.例如全屏播放

$('.fullscreen').on('click', function() {
   //For Webkit
   video[0].webkitEnterFullscreen();
 
   //For Firefox
   video[0].mozRequestFullScreen();
 
   return false;
});

開燈關燈控制

$('.btnLight').click(function() {
   if($(this).hasClass('on')) {
      $(this).removeClass('on');
      $('body').append('<div class="overlay"></div>');
      $('.overlay').css({
         'position':'absolute',
         'width':100+'%',
         'height':$(document).height(),
         'background':'#000',
         'opacity':0.9,
         'top':0,
         'left':0,
         'z-index':999
      });
      $('#myVideo').css({
         'z-index':1000
      });
   }
   else {
      $(this).addClass('on');
      $('.overlay').remove();
   }
   return false;
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM