HTML5 <video> - 使用 DOM 進行自定義控制示例

    HTML5的video雖然可用controls來展示控件,並進行控制播放暫停等,但是不同的瀏覽器顯示的
效果可能不一樣,所以很多時候我們需要使用Dom來進行自定義的一些操作和控制。下面是一個小例子。
當然效果不是很美觀,若想好看的可以自己設置css樣式等。
<div id="video_div" style="text-align:center;">
  <button onclick="playPause()">播放/暫停</button> 
  <button onclick="toBig()">大</button>
  <button onclick="toNormal()">中</button>
  <button onclick="toSmall()">小</button>
  <br /> 
  <video id="myVideo" width="500" height="250" style="margin-top:15px;">
    <source src="demo.mp4" type="video/mp4" />
    <source src="demo.ogg" type="video/ogg" />
   您的瀏覽器不支持此HTML5 視頻標籤。
  </video>
</div>

<script type="text/javascript">
var myVideo=document.getElementById("myVideo");

function playPause()
{ 
if (myVideo.paused) 
  myVideo.play(); 
else 
  myVideo.pause(); 
} 

function toBig()
{ 
myVideo.width=560; 
} 

function toNormal()
{ 
myVideo.width=420; 
}

function toSmall()
{ 
myVideo.width=320; 
}  
</script> 

需要注意的是在所有屬性中,只有 videoWidth 和 videoHeight 屬性是立即可用的。
在視頻的元數據已加載後,其他屬性纔可用。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章