H5的video非常简单,方便,有时我们可能需要自己来设置样式来自定义的video,自定义的话我们需要对功能进行一些处理,这里常用的功能几乎是都用到了,第一次练习代码很累赘,之后会慢慢改进。

常用的一些 video API

"视频播放":video.play();

"视频暂停播放":video.pause();

"视频地址":video.currentSrc;

"视频总时长":video.duration;

"视频播放速率":video.playbackRate;

"是否暂停":video.paused;

"是否结束":video.ended;

"是否静音":video.muted;  

"当前播放时间": video.currentTime;

"当前缓冲量":video.buffered.end(0);

"当前音量":video.volume

api使用方法

video.onloadedmetadata=function(){
console.log("视频地址"+video.currentSrc);
console.log("视频总时长"+video.duration);
console.log("视频播放速率"+video.playbackRate);
console.log("是否暂停"+video.paused);
console.log("是否结束"+video.ended);
console.log("是否静音"+video.muted);
}
//当前时间
video.ontimeupdate=function(){
console.log("当前播放时间"+video.currentTime);
console.log("当前缓冲量"+video.buffered.end(0));
}
//当前音量
video.onvolumechange=function(){
console.log("当前音量"+video.volume);
}
//播放
btnPlay.onclick=function (){
video.play();
}
//停止
btnStop.onclick=function (){
video.pause();
}

demo全部代码(代码里面的图片请替换成本地图片,不让无法看到此demo的完整效果,播放按钮在进度条左边)

<!DOCTYPE html>
<html>     <head>
        <meta charset="UTF-8">         <title>自定义播放器音量拖动</title>
        <style>
            .video-warp {
                width: 800px;
                margin: auto;
                text-align: center;
            }
            
            video {
                width: 100%;
            }
            
            .controls {
                width: 100%;
                background: #ccc;
                height: 20px;
                line-height: 20px;
                text-align: left;
                box-sizing: border-box;
                padding-left: 5px;
                position: relative;
            }
            
            .controls i {
                display: inline-block;
                width: 20px;
                height: 20px;
                background: url(img/play.png) no-repeat;
                background-size: 100%;
            }
            
            .controls i:nth-of-type(2) {
                display: none;
                background: url(img/pause.png) no-repeat;
                background-size: 100%;
            }
            
            .controls i:nth-of-type(3) {
                background: url(img/sound.png) no-repeat;
                background-size: 100%;
            }
            
            .controls i:nth-of-type(4) {
                display: none;
                background: url(img/mute.png) no-repeat;
                background-size: 100%;
            }
            
            .progress {
                display: inline-block;
                width: 62%;
                height: 10px;
                line-height: 10px;
                background: #e5e9ef;
                border-radius: 5px;
                vertical-align: super;
                overflow: hidden;
            }
            
            .video-timer {
                display: inline-block;
                vertical-align: top;
            }
            
            .timrBar {
                display: inline-block;
                height: 11px;
                background: #8adced;
            }
            
            #playSpeed {
                color: #fb0606;
                cursor: pointer;
                float: right;
                margin-right: -32px;             }
            /*yinliang*/
            
            .video-sound {
                position: absolute;
                width: 100px;
                height: 10px;
                background: #e5e9ef;
                bottom: 5px;
                right:48px;
                border-radius: 5px;
                overflow: hidden;
            }
            
            .soundBar {
                height: 100%;
                background: #00a1d6;
            }
            /*全屏*/
            #screen{
                float: right;
                margin-right: 25px;
                margin-top: 5px;
                width: 10px;
                height: 10px;
                background: url(img/full-screen.png);
                background-size:contain ;
            }
        </style>
    </head>     <body>
        <div class="video-warp" id="video-warp">
            <video id="video" poster="img/preview.jpg"                 
                <source src="http://www.w3school.com.cn/i/movie.ogg" type="video/ogg"></source>               
                当前浏览器不支持 video直接播放,点击这里下载视频:
                <a href="myvideo.webm">下载视频</a>
            </video>
            <div class="controls" id="controls">
                <i id="play"></i>
                <i id="pause" class="hide"></i>
                <!--进度条-->
                <div class="progress">
                    <div class="timrBar"></div>
                </div>
                <!--时长-->
                <div class="video-timer">
                    <span id="currentTime">00:00</span><em>/</em>
                    <span id="duration">00:00</span>
                </div>
                <i id="sound" class="sound"></i>
                <i id="muteSound" class="sound"></i>
                <div class="video-sound">
                    <div class="soundBar"></div>
                </div>
                <i id="screen"></i>
                <span id="playSpeed">*3</span>
            </div>
        </div>
        
        <script>
            var v = {
                video: document.getElementById("video"),//容器框
                play: document.getElementById("play"),//播放按钮
                pause: document.getElementById("pause"),//暂停按钮
                duration: document.getElementById("duration"),//总时长
                currentTime: document.getElementById("currentTime"),//当前播放时间
                progress: document.getElementsByClassName("progress")[0],//进度条容器
                timrBar: document.getElementsByClassName("timrBar")[0], //进度条                 sound: document.getElementsByClassName("video-sound")[0], //音量容器    
                soundBar: document.getElementsByClassName("soundBar")[0],//音量
                playSpeed: document.getElementById("playSpeed"),//播放速率
                warp: document.getElementById("video-warp"), //视频区域距离左边距离
                soundPercent:0 ,//音量百分比
                fullScreen:document.getElementById("screen")/*全屏按钮*/
            };             v.video.onloadedmetadata = function() {                
                //播放
                v.play.onclick = function() {
                    if(v.video.paused || v.video.ended) {
                        v.video.play();
                        this.style.display = "none";
                        v.pause.style.display = "inline-block";
                    }
                }
                //暂停
                v.pause.onclick = function() {
                    if(!v.video.paused || !v.video.ended) {
                        v.video.pause();
                        v.pause.style.display = "none";
                        v.play.style.display = "inline-block";
                    }
                }
                //获取时长
                v.duration.innerHTML = timer(v.video.duration);
                v.currentTime.innerHTML = timer(v.video.currentTime);
                //进度条跟随
                v.video.ontimeupdate = function() {
                    var currentTime = v.video.currentTime;
                    var duration = v.video.duration;
                    var percent = currentTime / duration * 100;
                    v.timrBar.style.width = percent + "%";
                    v.currentTime.innerHTML = timer(v.video.currentTime);
                }
                //进度条获取坐标调用 拖拽实现方法
                var enableProgressDrag = function(e) {
                    var progressDrag = false;
                    v.progress.onmousedown = function(e) {
                        progressDrag = true;
                        updateprogress(e.pageX - v.warp.offsetLeft);
                    }
                    document.onmouseup = function(e) {
                        if(progressDrag) {
                            progressDrag = false;
                            updateprogress(e.pageX - v.warp.offsetLeft);
                        }                     }
                    v.progress.onmousemove = function(e) {
                        if(progressDrag) {
                            updateprogress(e.pageX - v.warp.offsetLeft);
                        }
                    }
                };                 enableProgressDrag();
                //播放速率
                v.playSpeed.onclick = function() {
                    v.video.playbackRate = 3;
                }                 //音量获取坐标调用 拖拽实现方法
                var enableSoundDrag = function(e) {
                    var soundDrag = false;
                    v.sound.onmousedown = function(e) {
                        soundDrag = true;
                        updatesound(e.pageX - v.warp.offsetLeft);
                    }
                    v.sound.onmouseup = function(e) {
                        if(soundDrag) {
                            soundDrag = false;
                            updatesound(e.pageX - v.warp.offsetLeft);
                        }                     }
                    v.sound.onmousemove = function(e) {
                        if(soundDrag) {
                            updatesound(e.pageX - v.warp.offsetLeft);
                        }
                    }
                };
                enableSoundDrag();
                updatesound(null, 35); //初始化音量
                /*全屏*/
                var isScreen=false;            
                v.fullScreen.addEventListener("click",function(){
                    if(isScreen==false){
                        
                        requestFullscreen(v.video);
                        isScreen=true;
                    }else{
                        exitFullscreen(v.video);
                        isScreen=false;
                    }
                })
            }
            //进度条可拖拽实现
            function updateprogress(x) {
                var percent = 100 * (x - v.progress.offsetLeft) / v.progress.offsetWidth;
                if(percent > 100) {
                    percent = 100;
                }
                if(percent < 0) {
                    percent = 0;
                }
                v.timrBar.style.width = percent + "%";
                v.video.currentTime = v.video.duration * percent / 100;             }
            //音量可拖拽实现
            function updatesound(x, n) {
                sounding();
                if(n) {
                    soundPercent = n;
                } else {
                    soundPercent = 100 * (x - v.sound.offsetLeft) / v.sound.offsetWidth;
                }
                if(soundPercent > 100) {
                    persoundPercentcent = 100;
                }
                if(soundPercent < 0) {
                    soundPercent = 0;
                }
                v.soundBar.style.width = soundPercent + "%";
                v.video.volume = soundPercent / 100;
            }
            //时间格式化            
            function timer(seconds) {
                var minute = Math.floor(seconds / 60);
                if(minute < 10) {
                    minute = "0" + minute;
                }                 var second = Math.floor(seconds % 60);
                if(second < 10) {
                    second = "0" + second;
                }
                return minute + ":" + second;
            }
            //是否静音
            var sound = document.querySelector("#sound");
            var muteSound = document.querySelector("#muteSound");
            function sounding() {                
                if(v.video.volume == 0 ) {
                    sound.style.display = "none";
                    muteSound.style.display = "inline-block";
                } else {
                    sound.style.display = "inline-block";
                    muteSound.style.display = "none";
                }
            }
            //切换静音
            sound.onclick=function(){
                showHide(sound,muteSound);
                v.video.volume =0;
                v.soundBar.style.width=0;
            }
            //去除静音 音量回到之前选定区域
            muteSound.onclick=function(){
                showHide(muteSound,sound);
                v.soundBar.style.width = soundPercent + "%";
                v.video.volume = soundPercent / 100;
            }
            //显示与隐藏
            function showHide(a,b){
                a.style.display = "none";
                b.style.display = "inline-block";
            }             /*全屏*/
            // 全屏
            // ele:全屏的对象                        
            function requestFullscreen(ele) {
                // 全屏兼容代码
                if (ele.requestFullscreen) {
                    ele.requestFullscreen();
                } else if (ele.webkitRequestFullscreen) {
                    ele.webkitRequestFullscreen();
                } else if (ele.mozRequestFullScreen) {
                    ele.mozRequestFullScreen();
                } else if (ele.msRequestFullscreen) {
                    ele.msRequestFullscreen();
                }
            }
            
            // 取消全屏
            function exitFullscreen() {
                if (document.exitFullscreen) {
                    document.exitFullscreen();
                } else if (document.webkitExitFullscreen) {
                    document.webkitExitFullscreen();
                } else if (document.msExitFullscreen) {
                    document.msExitFullscreen();
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                }
            }
        </script>
    </body> </html>

效果:

H5自定义video功能与样式处理的更多相关文章

  1. 富文本编辑器UEditor自定义工具栏(三、自定义工具栏功能按钮图标及工具栏样式简单修改)

    导读 富文本编辑器UEditor提供丰富了定制配置项,如果想设置个性化的工具栏按钮图标有无办法呢?答案是肯定的!前两篇博文简要介绍了通过将原工具栏隐藏,在自定义的外部按钮上,调用UEditor各命令实 ...

  2. jquery和css自定义video播放控件

    下面介绍一下通过jquery和css自定义video播放控件. Html5 Video是现在html5最流行的功能之一,得到了大多数最新版本的浏览器支持.包括IE9,也是如此.不同的浏览器提供了不同的 ...

  3. 自定义浏览器滚动条的样式,打造属于你的滚动条风格——兼容IE和webkit(ff不支持)

    前段时间,到网上找素材时,看到了一个很个性的滚动条式,打开Chrome的调试工具看了一下,发现不是用JavaScript来模拟实现的,觉得 有必要折腾一下.于是在各大浏览器中对比了一下,发现只用Chr ...

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

    下面介绍一下通过jquery和css自定义video播放控件. Html5 Video是现在html5最流行的功能之一,得到了大多数最新版本的浏览器支持.包括IE9,也是如此.不同的浏览器提供了不同的 ...

  5. WPF编程,使用WindowChrome实现自定义窗口功能的一种方法。

    原文:WPF编程,使用WindowChrome实现自定义窗口功能的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/arti ...

  6. c#微信开发,使用JS-SDK自定义分享功能,分享朋友圈,分享给朋友等

    如果一个后端开发掌握了微信开发,甚至有微信开发上线项目,这在换工作的时候 还是有竞争力的. 微信开发的资料很多,但是用asp.net c#进行微信开发好像比较少,或者资料不够完整. 使用JS-SDK自 ...

  7. h5 的video视频控件

    h5 的video视频控件 由于html5的流行,其中的video视频的使用很流行,使得可恨的IE9也能与时俱进了. video所支持的格式有mp4.ogg和wav三种. 例: HTML5 Video ...

  8. arcgis api for js共享干货系列之二自定义Navigation控件样式风格

    arcgis api for js默认的Navigation控件样式风格如下图: 这样的风格不能说不好,各有各的爱好,审美观,这里也不是重点,这里的重点是如何自定义一套自己喜欢的样式风格呢:自己自定义 ...

  9. jquery美化select,自定义下拉框样式

    select默认的样式比较丑,有些应用需要美化select,在网上找到一个很好的美化样式效果,本人很喜欢,在这里分享一下. <!DOCTYPE html PUBLIC "-//W3C/ ...

随机推荐

  1. 前端html+css标签简介(可能就我自己看的懂-。-)

    标签集合 # html 文字标签:修改样式 -<font></font> -属性:size:大小,范围1-7,大于7时默认7 color:颜色,英文单词或者十六进制(editp ...

  2. python-1:正则表达式(基础知识点)

    1.简单匹配: \d  →匹配一个数字 \w →匹配一个数字或字母 \s  →匹配一个空格(包括tab等空白符) .    →匹配任意字符 *   →匹配任意个字符(包括0个) +  →匹配至少一个字 ...

  3. Iterable<T>接口

    https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html public interface Iterable<T> ...

  4. redis 学习(7) -- 有序集合

    redis 学习(7) -- 有序集合 zset 结构 有序集合:有序.不能包含重复元素 每个节点包含:score和value两个属性,根据score进行排序 如图: zset 重要 API 含义 命 ...

  5. Echarts ajax异步

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. Task的取消

    原文:.NET 4 并行(多核)编程系列之三 从Task的取消 .NET 4 并行(多核)编程系列之三 从Task的取消 前言:因为Task是.NET 4并行编程最为核心的一个类,也我们在是在并行编程 ...

  7. jquery简单实现表格隔行变色

    小知识点:odd的过滤选择器大的使用 html代码: <table> <tr> <td>用户名</td> <td>年龄</td> ...

  8. openlayers之点,线,面(以城市,河流,省份为例,分别对应点线面)

    kpst._this这里指向初始化的map // 设置标注样式函数 function createStyle(name) { // 河流style var riverStyle = new Style ...

  9. cmd窗口颜色设置

    color  02    第一个数字是背景颜色,第二个是文字颜色.

  10. Linux vim替换命令

    #替换每一行的第一个a:%s/a/b/#全部替换:%s/a/b/g#替换/时需要转义"\/"