jQ效果:jQuery和css自定义video播放控件

前端之家收集整理的这篇文章主要介绍了jQ效果:jQuery和css自定义video播放控件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面介绍一下通过jquery和css自定义video播放控件。

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

 

实际上,自定义视频控件并不困难.本文将告诉你如何用jQuery自定义视频控件,希望对你有用!

HTML5 Video 基础标签  

  1. <video id="myVideo" controls poster="video.jpg" width="600" height="400" >
  2. source src="video.mp4" type="video/mp4" />
  3. ="video.webm"="video/webM" ="video.ogv"="video/ogg" p>Your browser does not support the video tag.</>
  4. video>

幸运的是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); var video = $('#videoID')[0];
  4. return a jQuery object
  5. var video = $('#videoID');

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

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

  1. div class="control"a href="#" class="btnPlay">Play/Pauseadiv>

我们可以轻松的控制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支持视频回放.这里我们要显示视频的当前播放时间和总时间.

  1. ="progressTime">
  2. Current play time: span ="current"></span
  3. Video duration: ="duration">

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

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

  1. get HTML5 video time duration
  2. video.on('loadedMetadata',1)">() {
  3. $('.duration').text(video[0].duration);
  4. });
  5. update HTML5 video current play time
  6. video.on('timeupdate',1)">() {
  7. $('.current').text(video[0].currentTime);
  8. });

 视频进度条

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

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

下面的js就是通过视频的总时间与当前时间的计算,获得播放进度条。

  1. ].duration));
  2. });
  3. () {
  4. var currentPos = video[0].currentTime; Get currenttime
  5. var maxduration = video[0].duration; Get video duration
  6. var percentage = 100 * currentPos / maxduration; in %
  7. $('.timeBar').css('width',percentage+'%');
  8. });

下面实现播放进度条的拖拽,来播放视频

  1. var timeDrag = false; /* Drag status */
  2. $('.progressBar').mousedown((e) {
  3. timeDrag = true;
  4. updatebar(e.pageX);
  5. });
  6. $(document).mouseup((e) {
  7. if(timeDrag) {
  8. timeDrag = ;
  9. updatebar(e.pageX);
  10. }
  11. });
  12. $(document).mousemove((timeDrag) {
  13. updatebar(e.pageX);
  14. }
  15. });
  16. update Progress Bar control
  17. var updatebar = (x) {
  18. var progress = $('.progressBar');
  19. Video duraiton
  20. var position = x - progress.offset().left; Click pos
  21. var percentage = 100 * position / progress.width();
  22. Check within range
  23. if(percentage > 100) {
  24. percentage = 100;
  25. }
  26. if(percentage < 0) {
  27. percentage = 0;
  28. }
  29. Update progress bar and video currenttime
  30. $('.timeBar').css('width',1)">);
  31. video[0].currentTime = maxduration * percentage / 100;
  32. };

 

进阶-显示缓冲栏

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

  1. .progressBar
  2. position
  3. width
  4. height
  5. backgroud-color
  6. .bufferBar
  7. top
  8. left
  9. background-color>
  10. ="bufferBar">

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

  1. loop to get HTML5 video buffered data
  2. var startBuffer = var maxduration = video[0].duration;
  3. var currentBuffer = video[0].buffered.end(0);
  4. var percentage = 100 * currentBuffer / maxduration;
  5. $('.bufferBar').css('width',1)">);
  6. if(currentBuffer < maxduration) {
  7. setTimeout(startBuffer,500);
  8. }
  9. };
  10. setTimeout(startBuffer,500);

音量控制

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

  1. ="muted" >Mute/Unmute="volumeBar"="volume">

js:

  1. Mute/Unmute control clicked
  2. $('.muted').click(() {
  3. video[0].muted = !video[0].muted;
  4. ;
  5. });
  6. Volume control clicked
  7. $('.volumeBar').on('mousedown',1)">(e) {
  8. var position = e.pageX - volume.offset().left;
  9. volume.width();
  10. $('.volumeBar').css('width',1)">);
  11. video[0].volume = percentage / 100;
  12. });

 快进/快退 倒带控制

Html5 Video支持播放速度的改变.我们可以使用playbackrate属性来控制.

  1. ="ff">Fast Forward="rw">Rewind="sl">Slow Motion>

不幸的是FireFox不支持playbackrate属性.以及有些版本的chrome浏览器不支持负值(倒带).到目前为止,只有Safri浏览器完全支持.

  1. /Fast forward control
  2. $('.ff').on('click',1)">() {
  3. video[0].playbackrate = 3;
  4. Rewind control
  5. $('.rw').on('click',1)">() {
  6. video[0].playbackrate = -3Slow motion control
  7. $('.sl').on('click',1)">() {
  8. video[0].playbackrate = 0.5;
  9. });

其他

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

  1. $('.fullscreen').on('click',1)">For Webkit
  2. video[0].webkitEnterFullscreen();
  3. For Firefox
  4. video[0].mozRequestFullScreen();
  5. ;
  6. });

开灯关灯控制

  1. $('.btnLight').click(if($(this).hasClass('on')) {
  2. $(this).removeClass('on');
  3. $('body').append('<div class="overlay"></div>');
  4. $('.overlay').css({
  5. 'position':'absolute','width':100+'%':$(document).height(),'background':'#000''top':0'z-index':999
  6. });
  7. $('#myVideo').css({
  8. 'z-index':1000
  9. });
  10. }
  11. {
  12. $(this).addClass('on').remove();
  13. }
  14. ;
  15. });

 

原文地址:http://www.inwebson.com/html5/custom-html5-video-controls-with-jquery/#comment-form

 

猜你在找的jQuery相关文章