jquery设置video的宽度_使用jQuery和CSS自定义HTML5 Video 控件 简单适用

Html5 Video是现在html5最流行的功能之一,得到了大多数最新版本的浏览器支持.包括IE9,也是如此.不同的浏览器提供了不同的原生态浏览器视频空间.我们制作自定义视频控件为了在所有的浏览器中有一个相同的Html5视频控件而不受默认视频控件的控制.

HTML5 Video 基础标签

1

2

3

4

5

Your browser does not support the video tag.

6

video标签最好包含mp4、webM和ogg这三种源视频文件-可以跨浏览器。如果浏览器不支持html5,你可以使用flash作为后备!

开始制作 HTML5 Video Controls

幸运的是HTML5 Video 的Api可以用JavaScript访问,并使用他们来作为控制视频的媒介.

在编码之前让我简单的介绍一下jQuery是如何获取video标签的.

在JavaScript中我们使用getElementById('videoID')来获取Video标签,作为结果,我们会获取到一个Dom对象.但是这不是等价的jQuery对象.$("videoID")会返回一个jQuery对象.不是Dom对象.这就是为什么在将其转换为Dom对象之前我们不能直接使用jQuery选择器调用/使用Html5 Video的Dom属性和功能.

1 //return a DOM object

2 var video = document.getElementById('videoID'); //or

3 var video = $('#videoID').get(0); //or

4 var video = $('#videoID')[0];5

6 //return a jQuery object

7 var video = $('#videoID');

Video Play/Pause Controls 播放/暂停 按钮

好的,这是所有的介绍.现在让我们来编码.首先,我们要创建一个简单的播放/暂停按钮.

1

我们可以轻松的控制Html5 Video的播放与暂停状态.

1 //Play/Pause control clicked

2 $('.btnPlay').on('click', function() {3 if(video[0].paused) {4 video[0].play();5 }6 else{7 video[0].pause();8 }9 return false;10 };

显示视频播放时间和持续时间

Html5 Video支持视频回放.这里我们要显示视频的当前播放时间和总时间.

Current play time:

Video duration:

为了得到视频的总时间,我们要确保视频元数据已经加载.这个时候我们要用到Html5 Video的loadedmetadata事件.

对于当前的视频播放时间.我们可以用Html5 Video timeupdate事件来保证他的更新.

1 //get HTML5 video time duration

2 video.on('loadedmetadata', function() {3 $('.duration').text(video[0].duration);4 });5

6 //update HTML5 video current play time

7 video.on('timeupdate', function() {8 $('.current').text(video[0].currentTime);9 });

视频进度条

在这里我们将会把当前播放时间和总的时间长度转换为更人性化的进度条.

1

2 .progressBar3 {4 position: relative;5 width: 100%;6 height: height:10px;7 backgroud-color: #000;8 }9 .timeBar10 {11 position: absolute;12 top: 0;13 left: 0;14 width: 0;15 height: 100%;16 background-color: #ccc;17 }18

19

20

21

1 //get HTML5 video time duration

2 video.on('loadedmetadata', function() {3 $('.duration').text(video[0].duration));4 });5

6 //update HTML5 video current play time

7 video.on('timeupdate', function() {8 var currentPos = video[0].currentTime; //Get currenttime

9 var maxduration = video[0].duration; //Get video duration

10 var percentage = 100 * currentPos / maxduration; //in %

11 $('.timeBar').css('width', percentage+'%');12 });

1 var timeDrag = false; /*Drag status*/

2 $('.progressBar').mousedown(function(e) {3 timeDrag = true;4 updatebar(e.pageX);5 });6 $(document).mouseup(function(e) {7 if(timeDrag) {8 timeDrag = false;9 updatebar(e.pageX);10 }11 });12 $(document).mousemove(function(e) {13 if(timeDrag) {14 updatebar(e.pageX);15 }16 });17

18 //update Progress Bar control

19 var updatebar = function(x) {20 var progress = $('.progressBar');21 var maxduration = video[0].duration; //Video duraiton

22 var position = x - progress.offset().left; //Click pos

23 var percentage = 100 * position /progress.width();24

25 //Check within range

26 if(percentage > 100) {27 percentage = 100;28 }29 if(percentage < 0) {30 percentage = 0;31 }32

33 //Update progress bar and video currenttime

34 $('.timeBar').css('width', percentage+'%');35 video[0].currentTime = maxduration * percentage / 100;36 };

完成!

进阶-显示缓冲栏

我们需要给视频制作一个缓冲栏让用户知道视频加载了多少.

1

2 .progressBar {3 position: relative;4 width: 100%;5 height: height:10px;6 backgroud-color: #000;7 }8 .bufferBar {9 position: absolute;10 top: 0;11 left: 0;12 width: 0;13 height: 100%;14 background-color: #ccc;15 }16

17

18

19

Html5 Video缓冲属性将返回一个对象的缓存范围.因此,我们将使用缓存数据的最后一个值.

1 //loop to get HTML5 video buffered data

2 var startBuffer = function() {3 var maxduration = video[0].duration;4 var currentBuffer = video[0].buffered.end(0);5 var percentage = 100 * currentBuffer /maxduration;6 $('.bufferBar').css('width', percentage+'%');7

8 if(currentBuffer

音量控制

现在,我们要增加声音控制.有两种不同的音量控制方法.静音按钮/音量栏

Mute/Unmute

js

1 //Mute/Unmute control clicked

2 $('.muted').click(function() {3 video[0].muted = !video[0].muted;4 return false;5 });6

7 //Volume control clicked

8 $('.volumeBar').on('mousedown', function(e) {9 var position = e.pageX -volume.offset().left;10 var percentage = 100 * position /volume.width();11 $('.volumeBar').css('width', percentage+'%');12 video[0].volume = percentage / 100;13 });

其他

除了主要的控制插件.还可以做一些额外的控制.例如全屏播放

$('.fullscreen').on('click', function() {//For Webkit

video[0].webkitEnterFullscreen();//For Firefox

video[0].mozRequestFullScreen();return false;

});

开灯关灯控制

$('.btnLight').click(function() {if($(this).hasClass('on')) {

$(this).removeClass('on');

$('body').append('

$('.overlay').css({'position':'absolute','width':100+'%','height':$(document).height(),'background':'#000','opacity':0.9,'top':0,'left':0,'z-index':999});

$('#myVideo').css({'z-index':1000});

}else{

$(this).addClass('on');

$('.overlay').remove();

}return false;

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值