網頁Html
復制代碼代碼如下:
<body style="background-color:#8EEE5EE;">
<section id="skin">
<video id="myMovIE" width="640" height="360">
<source src="videos/Introduction.mp4">
</video>
<nav>
<div id="buttons">
<button type="button" id="playButton">Play</button>
</div>
<div id="defaultBar">
<div id="progressBar"></div>
</div>
<div style="clear:both"></div>
</nav>
</section>
</body>
CSS樣式
復制代碼代碼如下:
body{
text-align:center;
}
header,section,footer,aside,nav,article,hgroup{
display:block;
}
#skin{
width:700px;
margin:10px auto;
padding:5px;
background:red;
border:4px solid black;
border-radius:20px;
}
nav{
margin:5px 0px;
}
#buttons{
float:left;
width:70px;
height:22px;
}
#defaultBar{
position:relative;
float:left;
width:600px;
height:14px;
padding:4px;
border:1px solid black;
background:yellow;
}
/*progressBar在defaultBar內部*/
#progressBar{
position:absolute;
width:0px; /*使用Javascript控制變化*/
height:14px; /*和defaultBar高度相同*/
background:blue;
}
Javascript代碼
復制代碼代碼如下:
function doFisrt()
{
barSize=600; //注意不要使用px單位,且不要用var,是全局變量
myMovie=document.getElementById('myMovIE');
playButton=document.getElementById('playButton');
bar=document.getElementById('defaultBar');
progressBar=document.getElementById('progressBar');
playButton.addEventListener('click',playOrPause,false); //第三個參數總是false, Register the event handler for the bubbling phase.
bar.addEventListener('click',clickedBar,false);
}
//控制movIE播放和停止
function playOrPause(){
if(!myMovie.paused && !myMovIE.ended){
myMovIE.pause();
playButton.innerHtml='Play';
window.clearInterval(updatedBar);
}else{
myMovIE.play();
playButton.innerHtml='pause';
updatedBar=setInterval(update,500);
}
}
//控制進度條的動態顯示
function update(){
if(!myMovIE.ended){
var size=parseInt(myMovie.currentTime*barSize/myMovIE.duration);
progressBar.style.width=size+'px';
}else{
progressBar.style.width='0px';
playButton.innerHtml='Play';
window.clearInterval(updatedBar);
}
}
//鼠標點擊進度條控制方法
function clickedBar(e){
if(!myMovie.paused && !myMovIE.ended){
var mouseX=e.pageX-bar.offsetLeft;
var newtime=mouseX*myMovIE.duration/barSize; //new starting time
myMovIE.currentTime=newtime;
progressBar.style.width=mouseX+'px';
window.clearInterval(updatedBar);
}
}
window.addEventListener('load',doFisrt,false);
好東西啊,摘了代碼部分