HTML5+CSS3+JQuery打造自定义视频播放器

简介
HTML5的<video>标签已经被目前大多数主流浏览器所支持,包括还未正式发布的IE9也声明将支持<video>标签,利用浏览器原生特性嵌入视频有很多好处,所以很多开发者想尽快用上,但是真正使用前还有些问题要考虑,尤其是 Opera/Firefox 和IE/Safari浏览器所支持的视频编码不同的问题,Google几个月前发布的开源视频编码VP8有望能解决这一问题,另外Google还发布了开放网络媒体项目WebM,旨在帮助开发者为开放网络制作出世界级媒体格式,Opera, Firefox, Chrome和IE9都将支持VP8,而且Flash Player也将可以播放VP8,这就意味着我们很快就可以只制作一个版本的视频然后在所有主流浏览器上播放了。另外一个主要的问题就是如何构建自定义的HTML5<video>播放器,这是目前Flash Player的优势所在,利用Flash的IDE所提供的接口可以很方便的构建一个个性化的视频播放器,那HTML5的<video>标签要怎样才能实现呢?这个问题就是本文所要解决的!我们将开发一个HTML5<video>视频播放器的jQuery插件,并且可以很方便的进行自定义,将分为以下几个部分:
1.视频控制工具条
2.视频控制按钮
3.打包成jQuery插件
4.外观和体验
5.自定义皮肤


视频控制工具条
做为一个专业的web开发人员,我们创建一个视频播放器时一定希望它的外观在各个浏览器中看起来一致(consistent),但是通过下面的图可以看到目前各个浏览器提供的视频控制工具条外观各不相同:

那就没办法了,我们得自己从头来创建这个控制工具条,利用HTML和CSS再加上一些图片实现起来并不算很难,另外通过HTML5多媒体元素提供的API我们可以很方便将创建的任何按钮与播放/暂停等事件进行绑定。

视频控制按钮
基本的视频控制工具条要包含一个播放/暂停按钮,一个进度条,一个计时器和一个音量控制按钮,我们将这些按钮放在<video>元素下面,并用一个div作为父容器:

<div class="ghinda-video-controls">  
        <a class="ghinda-video-play" title="Play/Pause"></a>  
        <div class="ghinda-video-seek"></div>  
        <div class="ghinda-video-timer">00:00</div>  
        <div class="ghinda-volume-box">  
                <div class="ghinda-volume-slider"></div>  
                <a class="ghinda-volume-button" title="Mute/Unmute"></a>  
        </div>  
</div>  

 

复制代码

注意,我们使用元素的class属性来代替ID属性是为了方便在一个页面上使用多个播放器。

打包成jQuery插件
创建好控制按钮后我们需要配合多媒体元素的API来实现视频控制的目的,正如前面提到的一样我们将我们的播放器打包成jQuery插件,这样可以很好的实现复用,代码如下:

$.fn.gVideo = function(options) {  
        // build main options before element iteration  
        var defaults = {  
                theme: 'simpledark',  
                childtheme: ''  
        };  
        var options = $.extend(defaults, options);  
        // iterate and reformat each matched element  
        return this.each(function() {  
                var $gVideo = $(this);  
                  
                //create html structure  
                //main wrapper  
                var $video_wrap = $('<div></div>').addClass('ghinda-video-player').addClass(options.theme).addClass(options.childtheme);  
                //controls wraper  
                var $video_controls = $('<div class="ghinda-video-controls"><a class="ghinda-video-play" title="Play/Pause"></a><div class="ghinda-video-seek"></div><div class="ghinda-video-timer">00:00</div><div class="ghinda-volume-box"><div class="ghinda-volume-slider"></div><a class="ghinda-volume-button" title="Mute/Unmute"></a></div></div>');                                                  
                $gVideo.wrap($video_wrap);  
                $gVideo.after($video_controls);  

 

这里先假设您了解jQuery并知道如何创建一个jQuery插件,因为这个不在本文的讨论范围之内,在上面这段脚本中我们使用jQuery动态创建视频控制工具条的元素,接下来为了绑定事件我们需要获取对应的元素:

//get newly created elements  
var $video_container = $gVideo.parent('.ghinda-video-player');  
var $video_controls = $('.ghinda-video-controls', $video_container);  
var $ghinda_play_btn = $('.ghinda-video-play', $video_container);  
var $ghinda_video_seek = $('.ghinda-video-seek', $video_container);  
var $ghinda_video_timer = $('.ghinda-video-timer', $video_container);  
var $ghinda_volume = $('.ghinda-volume-slider', $video_container);  
var $ghinda_volume_btn = $('.ghinda-volume-button', $video_container);  
  
$video_controls.hide(); // keep the controls hidden  

 

这里我们通过className方式获取,先让工具条隐藏直到所有资源加载完成,现在来实现播放/暂停按钮:

var gPlay = function() {  
        if($gVideo.attr('paused') == false) {  
                $gVideo[0].pause();                                          
        } else {                                          
                $gVideo[0].play();                                  
        }  
};  
  
$ghinda_play_btn.click(gPlay);  
$gVideo.click(gPlay);  
  
$gVideo.bind('play', function() {  
        $ghinda_play_btn.addClass('ghinda-paused-button');  
});  
  
$gVideo.bind('pause', function() {  
        $ghinda_play_btn.removeClass('ghinda-paused-button');  
});  
  
$gVideo.bind('ended', function() {  
        $ghinda_play_btn.removeClass('ghinda-paused-button');  
});  

 

大多数浏览器在右键点击视频时会提供一个独立的菜单,它也提供了视频控制功能,如果用户通过这个右键菜单控制视频那就会跟我们的自定义控件冲突,所以为了避免这一点我们需要绑定视频播放器自身的“播放”,“暂停”和“结束”事件,在事件处理函数中处理播放/暂停按钮,控制按钮的样式。

为了创建进度条的拖动块,我们使用了jQuery UI的Slider组件:

var createSeek = function() {  
        if($gVideo.attr('readyState')) {  
                var video_duration = $gVideo.attr('duration');  
                $ghinda_video_seek.slider({  
                        value: 0,  
                        step: 0.01,  
                        orientation: "horizontal",  
                        range: "min",  
                        max: video_duration,  
                        animate: true,                                          
                        slide: function(){                                                          
                                seeksliding = true;  
                        },  
                        stop:function(e,ui){  
                                seeksliding = false;                                                  
                                $gVideo.attr("currentTime",ui.value);  
                        }  
                });  
                $video_controls.show();                                          
        } else {  
                setTimeout(createSeek, 150);  
        }  
};  
  
createSeek();  

 

正如你所看到的,这里我们写了一个递归函数,通过循环比较video的readyState属性来判断视频是否已经准备好,否则我们就不能获得视频的时长也无法创建滑动块,当视频准备好后我们初始化滑动块并显示控制工具条,下一步我们通过绑定video元素的timeupdate事件实现计时器功能:

var gTimeFormat=function(seconds){  
        var m=Math.floor(seconds/60)<10?"0"+Math.floor(seconds/60):Math.floor(seconds/60);  
        var s=Math.floor(seconds-(m*60))<10?"0"+Math.floor(seconds-(m*60)):Math.floor(seconds-(m*60));  
        return m+":"+s;  
};  
  
var seekUpdate = function() {  
        var currenttime = $gVideo.attr('currentTime');  
        if(!seeksliding) $ghinda_video_seek.slider('value', currenttime);  
        $ghinda_video_timer.text(gTimeFormat(currenttime));                                                          
};  
  
$gVideo.bind('timeupdate', seekUpdate);  

 

这里我们用seekUpdate函数获取video的currentTime属性值然后调用gTimeFormat函数进行格式化后得到当前播放的时间点。

至于音量控制控件我们还是利用jQuery UI的Slider组件然后利用自定义函数实现静音和取消静音的功能:

$ghinda_volume.slider({  
        value: 1,  
        orientation: "vertical",  
        range: "min",  
        max: 1,  
        step: 0.05,  
        animate: true,  
        slide:function(e,ui){  
                $gVideo.attr('muted',false);  
                video_volume = ui.value;  
                $gVideo.attr('volume',ui.value);  
        }  
});  
  
var muteVolume = function() {  
        if($gVideo.attr('muted')==true) {  
                $gVideo.attr('muted', false);  
                $ghinda_volume.slider('value', video_volume);  
                  
                $ghinda_volume_btn.removeClass('ghinda-volume-mute');                                          
        } else {  
                $gVideo.attr('muted', true);  
                $ghinda_volume.slider('value', '0');  
                  
                $ghinda_volume_btn.addClass('ghinda-volume-mute');  
        };  
};  
  
$ghinda_volume_btn.click(muteVolume);  

 

最后当我们自己的自定义视频控制工具条构造完成后需要移除<video>标签的controls属性,这样浏览器默认的工具条就被去掉了。

好了,我们的插件功能已经全部完成了,调用方法:

$('video').gVideo();  
 
这会将我们的插件应用到页面上每一个video元素上。


外观和体验
好的,现在到了比较有意思的部分,也就是播放器的外观和体验了。当插件功能已经完成后利用一点CSS就可以很容易地自定义样式了,我们将全部使用CSS3来实现。
首先,我们给播放器主容器加一些样式:

.ghinda-video-player {  
        float: left;  
        padding: 10px;  
        border: 5px solid #61625d;  
                  
        -moz-border-radius: 5px; /* FF1+ */  
        -ms-border-radius: 5px; /* IE future proofing */  
        -webkit-border-radius: 5px; /* Saf3+, Chrome */  
        border-radius: 5px; /* Opera 10.5, IE 9 */  
          
        background: #000000;  
        background-image: -moz-linear-gradient(top, #313131, #000000); /* FF3.6 */  
        background-image: -webkit-gradient(linear,left top,left bottombottom,color-stop(0, #313131),color-stop(1, #000000)); /* Saf4+, Chrome */  
          
        box-shadow: inset 0 15px 35px #535353;  
}  

 

下一步,我们设置视频控制工具条左边浮动使它们水平对齐,利用CSS3的opacity和transitions我们给播放/暂停和静音/取消静音按钮添加了非常不错的悬浮效果:
.ghinda-video-play {  
        display: block;  
        width: 22px;  
        height: 22px;  
        margin-right: 15px;  
        background: url(../images/play-icon.png) no-repeat;          
          
        opacity: 0.7;  
          
        -moz-transition: all 0.2s ease-in-out; /* Firefox */  
        -ms-transition: all 0.2s ease-in-out; /* IE future proofing */  
        -o-transition: all 0.2s ease-in-out;  /* Opera */  
        -webkit-transition: all 0.2s ease-in-out; /* Safari and Chrome */  
        transition: all 0.2s ease-in-out;   
}  
  
.ghinda-paused-button {  
        background: url(../images/pause-icon.png) no-repeat;  
}  
  
.ghinda-video-play:hover {          
        opacity: 1;  
}  
 
如果您仔细看了前面那段根据视频播放状态(Playing/Paused)添加和移除播放/暂停按钮样式的JavaScript代码,就会明白为什么.ghinda-paused-button为什么要重写.ghinda-video-play的背景属性了。

现在轮到滑动块了,我们进度条和音量控制的滑动块的实现都是利用了jQuery UI的Slider组件,这个组件它本身自带了样式,定义在jQuery UI对应的css文件中,但是为了使滑动块和播放器其他控件外观保持一致我们全部重写了它的样式:

.ghinda-video-seek .ui-slider-handle {  
        width: 15px;  
        height: 15px;  
        border: 1px solid #333;  
        top: -4px;  
  
        -moz-border-radius:10px;  
        -ms-border-radius:10px;  
        -webkit-border-radius:10px;  
        border-radius:10px;          
          
        background: #e6e6e6;  
        background-image: -moz-linear-gradient(top, #e6e6e6, #d5d5d5);  
        background-image: -webkit-gradient(linear,left top,left bottombottom,color-stop(0, #e6e6e6),color-stop(1, #d5d5d5));  
          
        box-shadow: inset 0 -3px 3px #d5d5d5;          
}  
  
.ghinda-video-seek .ui-slider-handle.ui-state-hover {  
        background: #fff;  
}  
  
.ghinda-video-seek .ui-slider-range {  
        -moz-border-radius:15px;  
        -ms-border-radius:15px;  
        -webkit-border-radius:15px;  
        border-radius:15px;  
          
        background: #4cbae8;  
        background-image: -moz-linear-gradient(top, #4cbae8, #39a2ce);  
        background-image: -webkit-gradient(linear,left top,left bottombottom,color-stop(0, #4cbae8),color-stop(1, #39a2ce));  
          
        box-shadow: inset 0 -3px 3px #39a2ce;  
}  

 

这时候音量控制的滑动块一直显示在音量按钮旁边,我们需要将它改成默认隐藏,当鼠标悬浮在音量按钮上再动态显示出来,使用transitions来实现这个效果会是个不错的的选择:
.ghinda-volume-box {          
        height: 30px;  
          
        -moz-transition: all 0.1s ease-in-out; /* Firefox */  
        -ms-transition: all 0.1s ease-in-out; /* IE future proofing */  
        -o-transition: all 0.2s ease-in-out;  /* Opera */  
        -webkit-transition: all 0.1s ease-in-out; /* Safari and Chrome */  
        transition: all 0.1s ease-in-out;   
}  
  
.ghinda-volume-box:hover {          
        height: 135px;  
        padding-top: 5px;  
}  
  
.ghinda-volume-slider {          
        visibility: hidden;  
        opacity: 0;  
          
        -moz-transition: all 0.1s ease-in-out; /* Firefox */  
        -ms-transition: all 0.1s ease-in-out;  /* IE future proofing */  
        -o-transition: all 0.1s ease-in-out;  /* Opera */  
        -webkit-transition: all 0.1s ease-in-out; /* Safari and Chrome */  
        transition: all 0.1s ease-in-out;   
}  
  
.ghinda-volume-box:hover .ghinda-volume-slider {  
        position: relative;  
        visibility: visible;  
        opacity: 1;  
}  
 
利用一些基础的CSS属性以及CSS3提供的新属性我们打造了一个全新的播放器外观,它看起来是这个样子:


自定义皮肤
可能您已经注意到,我们在编写插件的时候已经定义了一些默认选项,它们是theme和childtheme,可以在调用插件的时候根据需要方便的应用自定义皮肤。
这里解释下theme就是所有控件的一整套样式定义,childtheme就是在theme基础上重写某些样式,我们在调用插件的时候可以同时指定这两个选项或者其中的一个:

$('video').gVideo({  
           childtheme:'smalldark'  
});  

 

我们写了一个示例的皮肤smalldark,它只重写了部分的样式,显示效果是这样的:


总结
利用HTML5 video,JavaScript和CSS3打造自定义的视频播放器真的非常容易,t实现工具条功能用JavaScrip,外观和体验交给CSS3,我们得到了一个功能强大并且易于定制的解决方案!
enjoy!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的注册验证界面的示例代码: HTML代码: ``` <!DOCTYPE html> <html> <head> <title>注册验证界面</title> <meta charset="UTF-8"> <link href="style.css" rel="stylesheet"> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="script.js"></script> </head> <body> <h1>注册验证界面</h1> <form id="register-form" method="post" action=""> <label for="username">用户名:</label> <input type="text" id="username" name="username" required> <div id="username-error" class="error-message"></div> <label for="email">邮箱:</label> <input type="email" id="email" name="email" required> <div id="email-error" class="error-message"></div> <label for="password">密码:</label> <input type="password" id="password" name="password" required> <div id="password-error" class="error-message"></div> <label for="confirm-password">确认密码:</label> <input type="password" id="confirm-password" name="confirm-password" required> <div id="confirm-password-error" class="error-message"></div> <input type="submit" value="注册"> </form> </body> </html> ``` CSS代码: ``` body { font-family: Arial, sans-serif; background-color: #f0f0f0; } h1 { text-align: center; margin-top: 50px; } form { width: 400px; margin: 0 auto; padding: 20px; background-color: #fff; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); } label { display: block; margin-bottom: 5px; } input[type="text"], input[type="email"], input[type="password"] { display: block; width: 100%; padding: 10px; margin-bottom: 10px; border: none; border-radius: 3px; box-shadow: 0 0 5px rgba(0, 0, 0, 0.1); } input[type="submit"] { display: block; width: 100%; padding: 10px; margin-top: 20px; background-color: #4CAF50; color: #fff; border: none; border-radius: 3px; cursor: pointer; } .error-message { color: #f00; font-size: 12px; margin-bottom: 10px; } ``` jQuery代码: ``` $(document).ready(function() { // 验证用户名 $("#username").blur(function() { var username = $(this).val(); if (username.trim() == "") { $("#username-error").text("用户名不能为空"); } else if (username.length < 6 || username.length > 20) { $("#username-error").text("用户名长度应为6-20个字符"); } else { $("#username-error").text(""); } }); // 验证邮箱 $("#email").blur(function() { var email = $(this).val(); if (email.trim() == "") { $("#email-error").text("邮箱不能为空"); } else if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email)) { $("#email-error").text("邮箱格式不正确"); } else { $("#email-error").text(""); } }); // 验证密码 $("#password").blur(function() { var password = $(this).val(); if (password.trim() == "") { $("#password-error").text("密码不能为空"); } else if (password.length < 6 || password.length > 20) { $("#password-error").text("密码长度应为6-20个字符"); } else { $("#password-error").text(""); } }); // 验证确认密码 $("#confirm-password").blur(function() { var confirmPassword = $(this).val(); var password = $("#password").val(); if (confirmPassword.trim() == "") { $("#confirm-password-error").text("确认密码不能为空"); } else if (confirmPassword != password) { $("#confirm-password-error").text("两次密码输入不一致"); } else { $("#confirm-password-error").text(""); } }); // 提交表单时验证 $("#register-form").submit(function() { var username = $("#username").val(); var email = $("#email").val(); var password = $("#password").val(); var confirmPassword = $("#confirm-password").val(); if (username.trim() == "") { $("#username-error").text("用户名不能为空"); return false; } else if (username.length < 6 || username.length > 20) { $("#username-error").text("用户名长度应为6-20个字符"); return false; } else if (email.trim() == "") { $("#email-error").text("邮箱不能为空"); return false; } else if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email)) { $("#email-error").text("邮箱格式不正确"); return false; } else if (password.trim() == "") { $("#password-error").text("密码不能为空"); return false; } else if (password.length < 6 || password.length > 20) { $("#password-error").text("密码长度应为6-20个字符"); return false; } else if (confirmPassword.trim() == "") { $("#confirm-password-error").text("确认密码不能为空"); return false; } else if (confirmPassword != password) { $("#confirm-password-error").text("两次密码输入不一致"); return false; } else { return true; } }); }); ``` 在这个示例中,我们使用了HTML、CSS和jQuery来创建一个简单的注册验证界面。在表单中,我们为每个输入字段添加了必要的验证规则,例如,用户名不能为空且长度应为6-20个字符,电子邮件地址必须符合标准格式,密码不能为空且长度应为6-20个字符,确认密码必须与密码相匹配。 我们使用jQuery的事件处理程序来验证每个输入字段,并在需要时显示错误消息。在提交表单时,我们还验证了每个输入字段,并在表单未通过验证时阻止提交。 这只是一个简单的示例,您可以根据需要添加更多的验证规则和样式来创建自定义的注册验证界面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值