HTML5技术

html5 实现video标签的自定义播放进度条 - 风雨后见彩虹

字号+ 作者:风雨后见彩虹 来源:博客园 2015-09-26 17:09 我要评论( )

现在随着html5的渐热,越来越多的web开发者都开始选择使用html5写出一些比较好的web应用。 html代码: HTML5-Video-Player .videoPlayer { border width #video { margin-top #videoControls { width margin-top .show { opacity .hide { opacity #progressW

现在随着html5的渐热,越来越多的web开发者都开始选择使用html5写出一些比较好的web应用。

html代码:

HTML5-Video-Player .videoPlayer{ border width #video{ margin-top #videoControls{ width margin-top .show{ opacity .hide{ opacity #progressWrap{ background-color height cursor #playProgress{ background-color width height border-right #showProgress{ background-color: ; font-weight font-size line-heightHTML5_Video_Player width preload controls 0播放 全屏

 

js代码;

// 为了不随意的创建全局变量,我们将我们的代码放在一个自己调用自己的匿名函数中,这是一个好的编程习惯 (function(window, document){ video = document.getElementById("video"); var videoControls = document.getElementById("videoControls"); var videoContainer = document.getElementById("videoContainer"); var controls = document.getElementById("video_controls"); var playBtn = document.getElementById("playBtn"); var fullScreenBtn = document.getElementById("fullScreenBtn"); var progressWrap = document.getElementById("progressWrap"); var playProgress = document.getElementById("playProgress"); var fullScreenFlag = false; var progressFlag; videoPlayer = { init: function(){ var that = this; video.removeAttribute("controls"); bindEvent(video, "loadeddata", videoPlayer.initControls); videoPlayer.operateControls(); }, initControls: function(){ videoPlayer.showHideControls(); }, showHideControls: function(){ bindEvent(video, "mouseover", showControls); bindEvent(videoControls, "mouseover", showControls); bindEvent(video, "mouseout", hideControls); bindEvent(videoControls, "mouseout", hideControls); }, operateControls: function(){ bindEvent(playBtn, "click", play); bindEvent(video, "click", play); bindEvent(fullScreenBtn, "click", fullScreen); bindEvent(progressWrap, "mousedown", videoSeek); } } videoPlayer.init(); bindEvent(ele, eventName, func){ if(window.addEventListener){ ele.addEventListener(eventName, func); } else{ ele.attachEvent('on' + eventName, func); } } showControls(){ videoControls.style.opacity = 1; } hideControls(){ // 为了让控制面板一直出现,我把videoControls.style.opacity的值改为1 videoControls.style.opacity = 1; } play(){ if ( video.paused || video.ended ){ if ( video.ended ){ video.currentTime = 0; } video.play(); playBtn.innerHTML = "暂停"; progressFlag = setInterval(getProgress, 60); } else{ video.pause(); playBtn.innerHTML = "播放"; clearInterval(progressFlag); } } fullScreen(){ if(fullScreenFlag){ videoContainer.webkitCancelFullScreen(); } else{ videoContainer.webkitRequestFullscreen(); } } getProgress(){ var percent = video.currentTime / video.duration; playProgress.style.width = percent * (progressWrap.offsetWidth) - 2 + "px"; showProgress.innerHTML = (percent * 100).toFixed(1) + "%"; } videoSeek(e){ if(video.paused || video.ended){ play(); enhanceVideoSeek(e); } else{ enhanceVideoSeek(e); } } function enhanceVideoSeek(e){ clearInterval(progressFlag); var length = e.pageX - progressWrap.offsetLeft; var percent = length / progressWrap.offsetWidth; playProgress.style.width = percent * (progressWrap.offsetWidth) - 2 + "px"; video.currentTime = percent * video.duration; progressFlag = setInterval(getProgress, 60); } }(this, document))

 

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • HTML5 进阶系列:拖放 API 实现拖放排序 - _林鑫

    HTML5 进阶系列:拖放 API 实现拖放排序 - _林鑫

    2017-05-02 11:02

  • HTML5 进阶系列:indexedDB 数据库 - _林鑫

    HTML5 进阶系列:indexedDB 数据库 - _林鑫

    2017-04-27 14:02

  • HTML5 高级系列:web Storage - _林鑫

    HTML5 高级系列:web Storage - _林鑫

    2017-04-27 14:01

  • HTML5和CSS3 - 奔跑在起跑线佼佼者

    HTML5和CSS3 - 奔跑在起跑线佼佼者

    2017-04-20 13:00

网友点评