当前位置: 首页 > news >正文

HTML5实战与剖析之媒体元素(6、视频实例)

  HTML5中的视频标签和及其模仿视频播放器的效果在一些手机端应用比較多。由于手机端基本上废除了flash的独断。让HTML5当家做主人,所以对视频支持的比較好。

所以今天专门为大家奉上HTML5视频标签模拟视频播放器的小样例,让大家更好的理解HTML5和有效的应用在项目中。


  HTML代码


<!-- src中放上本地的ogv的音频 -->
<video id="v1" src="Intermission-Walk-in.ogv"></video>
<div id="div1">
	<input type="button" value="播放" />
	<input type="button" value="00:00:00" />
	<input type="button" value="00:00:00" />
	<input type="button" value="静音" />
	<input type="button" value="全屏" />
</div>
<div id="div2">
	<div id="div3"></div>
</div>

<div id="div4">
	<div id="div5"></div>
</div>


  CSS代码


#div2{ width:300px; height:30px; background:#666666; position:relative;}
#div3{ width:30px; height:30px; background:red; position:absolute; left:0; top:0;}
#div4{ width:300px; height:20px; background:#666666; position:relative; top:10px;}
#div5{ width:20px; height:20px; background:yellow; position:absolute; right:0; top:0;}


  JavaScript代码


window.onload = function(){
	var oV = document.getElementById('v1');
	var oDiv = document.getElementById('div1');
	var aInput = oDiv.getElementsByTagName('input');
	
	var oDiv2 = document.getElementById('div2');
	var oDiv3 = document.getElementById('div3');
	var oDiv4 = document.getElementById('div4');
	var oDiv5 = document.getElementById('div5');
	
	var timer = null;
	
	//播放暂停
	aInput[0].onclick = function(){
	
		if(oV.paused){
			this.value = '暂停';
			oV.play();
			toShow();
			timer = setInterval(toShow,1000);
		}
		else{
			this.value = '播放';
			oV.pause();
			clearInterval(timer);
		}
		
	};
	
	aInput[2].value = timeChange(oV.duration);
	
	function timeChange(iAll){
	
		iAll = Math.floor(iAll);
	
		var hours = toZero(parseInt(iAll/3600));
		var mintus = toZero(parseInt(iAll%3600/60));
		var sends = toZero(parseInt(iAll%60));
		
		return hours + ":" + mintus + ":" + sends;
	
	}
	
	function toZero(num){
		if(num<10){
			return '0' + num;
		}
		else{
			return '' + num;
		}
	}
	
	function toShow(){
		aInput[1].value = timeChange(oV.currentTime);
		
		var scale = oV.currentTime/oV.duration;
		
		oDiv3.style.left = scale * (oDiv2.offsetWidth - oDiv3.offsetWidth) + 'px';
		
	}
	
	//静音
	aInput[3].onclick = function(){
		if(oV.muted){
			this.value = '静音';
			oV.muted = false;
			oDiv5.style.left = oV.volume*(oDiv4.offsetWidth - oDiv5.offsetWidth) + 'px';
		}
		else{
			this.value = '消除静音';
			oV.muted = true;
			oDiv5.style.left = 0;
		}
	};
	
	var disX = 0;
	
	//进度调整
	oDiv3.onmousedown = function(ev){
		var ev = ev || window.event;
		disX = ev.clientX - oDiv3.offsetLeft;
		
		document.onmousemove = function(ev){
			var ev = ev || window.event;
			
			var L = ev.clientX - disX;
			
			if(L<0){
				L = 0;
			}
			else if(L>oDiv2.offsetWidth - oDiv3.offsetWidth){
				L = oDiv2.offsetWidth - oDiv3.offsetWidth;
			}
			
			oDiv3.style.left = L + 'px';
			
			var scale = L/(oDiv2.offsetWidth - oDiv3.offsetWidth);
			
			oV.currentTime = scale * oV.duration;
			
			toShow();
			
		};
		document.onmouseup = function(){

			aInput[0].value = '暂停';
			oV.play();
			toShow();
			timer = setInterval(toShow,1000);

			document.onmousemove = null;
			document.onmouseup = null;
		};
		return false;
	};
	
	var disX2 = 0;
	
	//声音
	oDiv5.onmousedown = function(ev){
		var ev = ev || window.event;
		disX2 = ev.clientX - oDiv5.offsetLeft;
		
		document.onmousemove = function(ev){
			var ev = ev || window.event;
			
			var L = ev.clientX - disX2;
			
			if(L<0){
				L = 0;
			}
			else if(L>oDiv4.offsetWidth - oDiv5.offsetWidth){
				L = oDiv4.offsetWidth - oDiv5.offsetWidth;
			}
			
			oDiv5.style.left = L + 'px';
			
			var scale = L/(oDiv4.offsetWidth - oDiv5.offsetWidth);	
			
			oV.volume = scale;
			
		};
		document.onmouseup = function(){
			document.onmousemove = null;
			document.onmouseup = null;
		};
		return false;
	};
	
	//全屏
	aInput[4].onclick = function(){
	
		oV.webkitRequestFullScreen();
	
	};
	
};


  HTML5实战与剖析之媒体元素(5、视频实例)就为大家介绍到这里,模拟视频播放器的小样例差点儿把经常使用的小功能。

这个小样例只能在支持HTML5的视频标签的浏览器中播放。很多其它有关HTML5的相关知识尽在梦龙小站的HTML5实战与剖析部分,感谢大家的支持。





相关文章:

  • Excel实用操作
  • glance镜像元数据
  • CleanMyMac 3新增功能有哪些
  • 包含min函数的栈
  • Canva(设计图片)
  • Thinkphp 简单表单提交验证
  • 为了媳妇的一张号,我与百度医生杠上了
  • zend studio 的注册码-php的编辑器
  • Android 权限大全中英对照
  • Nightmare --- 炸弹时间复位
  • HDU 5655 CA Loves Stick 水题
  • 跟着实例学习ZooKeeper的用法: Barrier
  • Partition4:增加分区
  • Oracle创建表空间
  • succ
  • 【刷算法】从上往下打印二叉树
  • 2017年终总结、随想
  • 3.7、@ResponseBody 和 @RestController
  • Git的一些常用操作
  • Hexo+码云+git快速搭建免费的静态Blog
  • input实现文字超出省略号功能
  • Javascript编码规范
  • Java程序员幽默爆笑锦集
  • Java读取Properties文件的六种方法
  • Java方法详解
  • js中的正则表达式入门
  • PhantomJS 安装
  • Python 基础起步 (十) 什么叫函数?
  • SegmentFault 2015 Top Rank
  • SOFAMosn配置模型
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • Terraform入门 - 3. 变更基础设施
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 数据仓库的几种建模方法
  • 延迟脚本的方式
  • 正则表达式-基础知识Review
  • ​MPV,汽车产品里一个特殊品类的进化过程
  • !!【OpenCV学习】计算两幅图像的重叠区域
  • ![CDATA[ ]] 是什么东东
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • (32位汇编 五)mov/add/sub/and/or/xor/not
  • (6)STL算法之转换
  • (js)循环条件满足时终止循环
  • (办公)springboot配置aop处理请求.
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (附源码)springboot宠物医疗服务网站 毕业设计688413
  • (五)大数据实战——使用模板虚拟机实现hadoop集群虚拟机克隆及网络相关配置
  • (一)基于IDEA的JAVA基础1
  • (转)关于pipe()的详细解析
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • .bat批处理(三):变量声明、设置、拼接、截取
  • .NET CORE 2.0发布后没有 VIEWS视图页面文件
  • .NET 事件模型教程(二)
  • .NET 中的轻量级线程安全
  • .NET/C# 的字符串暂存池