自定义视频播放器
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>视频播放</title>
<!--字体图标库-->
<link rel="stylesheet" href="css/font-awesome.css"/>
<link rel="stylesheet" href="css/player.css"/>
</head>
<body>
<figure>
<figcaption>视频播放器</figcaption>
<div class="player">
<video src="./video/fun.mp4"></video>
<div class="controls">
<!--播放/暂停-->
<a href="javascript:;" class="switch fa fa-play"></a>
<!--播放进度-->
<div class="progress">
<div class="line"></div>
<div class="bar"></div>
</div>
<!--当前播放时间/播放总时长-->
<div class="timer">
<span class="current">00:00:00</span> / <span class="total">00:00:00</span>
</div>
<!--全屏/取消全屏-->
<a href="javascript:;" class="expand fa fa-arrows-alt"></a>
</div>
</div>
</figure>
<script src="./js/jquery.min.js"></script>
<script>
$(function () {
var $video = $('video');
var video = $video.get(0);
var $switch = $('.switch');
var $total = $('.total');
var $line = $('.line');
var $current = $('.current');
var $expand = $('.expand');
var $bar = $('.bar');
var formatTime = function (time) {
var h = Math.floor(time/3600);
var m = Math.floor(time%3600/60);
var s = Math.floor(time%60);
return (h<10?'0'+h:h)+':'+(m<10?'0'+m:m)+':'+(s<10?'0'+s:s)
}
video.oncanplay = function () {
$video.show();
var timeStr = formatTime(video.duration);
$total.html(timeStr);
}
$switch.on('click',function () {
if($switch.hasClass('fa-play')){
video.play();
$switch.removeClass('fa-play').addClass('fa-pause');
}else{
video.pause();
$switch.removeClass('fa-pause').addClass('fa-play');
}
});
video.ontimeupdate = function () {
var p = video.currentTime/video.duration*100+'%';
$line.css('width',p);
$current.html(formatTime(video.currentTime));
}
$expand.on('click',function () {
if($(this).hasClass('fa-arrows-alt')){
video.webkitRequestFullScreen();
$(this).removeClass('fa-arrows-alt').addClass('fa-compress');
}else{
document.webkitCancelFullScreen();
$(this).addClass('fa-arrows-alt').removeClass('fa-compress');
}
})
video.onended = function () {
video.currentTime = 0;
$switch.removeClass('fa-pause').addClass('fa-play');
}
$bar.on('click',function (e) {
var p = e.offsetX/$bar.width();
var goTime = p * video.duration;
video.currentTime = goTime;
})
});
</script>
</body>
</html>