html5 自定义播放器核心代码
程序员文章站
2023-11-17 14:57:34
HTML5提供有新的video标签,可以不用编程直接播放video,只需要写几行简单的代码, 就可以自定义播放器... 13-12-20...
网页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);
好东西啊,摘了代码部分
复制代码
代码如下:<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);
好东西啊,摘了代码部分
上一篇: 人民日报:机器人做手术渐靠谱
下一篇: html5教程画矩形代码分享