当前位置:   article > 正文

html5 video视频进度条的实现_h5视频进度开发

h5视频进度开发

html5 video视频进度条的实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>视频</title>
    <link rel="stylesheet" href="./css/index.css">
    <link rel="stylesheet" href="./css/font-awesome-4.7.0/css/font-awesome.css">
    
    <!-- <script src="./jquery-3.5.1.js"></script> -->
    <script>
        // jQuery实现
        // $(function(){
        //     var video = $("video")[0];
        //     $("button").click(function(){
        //         if($(this).text()==="播放"){
        //             video.play();
        //         }

        //         if($(this).text()==="暂停"){
        //              video.pause();
        //         }

        //         if($(this).text()==="前进"){
        //             video.currentTime += 5;
        //         }

        //         if($(this).text()==="后退"){
        //             video.currentTime -= 5;
        //         }
        //     })
        // })
        
        // 原生js实现
          window.onload = function(){
            // 视频
              var video = document.getElementsByTagName("video")[0];
              var progressTimer = document.querySelector('.progress_timer');
              var durationTimer = document.querySelector('.duration_timer');
              var progress = document.querySelector('.progress');
            //   console.log(video);
            let {totalT,presentT} = {totalT:0,presentT:0}
            //获取视频总时间
            video.addEventListener('canplay',function(){
                totalT = this.duration;
                var videoDuration = formatTime(totalT);
                durationTimer.innerHTML = videoDuration;
            })
            //获取视频当前播放的时间
            video.addEventListener('timeupdate',function(){
                presentT = this.currentTime;
                var videoCurrent = formatTime(presentT);
                progressTimer.innerHTML = videoCurrent;
                // 进度条
                var percent = presentT/totalT*100;
                progress.style.width = percent+'%';
            })
            function formatTime(t){
                var h = parseInt(t/3600);
                h = h<10?'0'+h:h;
                var m = parseInt(t%3600/60);
                m = m<10?'0'+m:m;
                var s = parseInt(t%60);
                s = s<10?'0'+s:s;
                return h+':'+m+':'+s;
            }
            // console.log(video.currentTime);
            // 播放
              document.getElementById("one").onclick = function(){
                      video.play();
              }
            // 暂停
              document.getElementById("two").onclick = function(){
                  video.pause();
              }
            // 快进
              document.getElementById("three").onclick = function(){
                  video.currentTime += 1/25;
                  console.log(video.currentTime);
              }
            // 后退
              document.getElementById("four").onclick = function(){
                  video.currentTime -= 1/25; 
              }

             // 音频
              var audio = document.getElementsByTagName("audio")[0];
              console.log(audio);
            //   播放
              document.getElementById("five").onclick = function(){
                audio.play();
              }
            // 暂停
              document.getElementById("six").onclick = function(){
                  audio.pause();
              }
            // 快进
              document.getElementById("seven").onclick = function(){
                  audio.currentTime += 5;
              }
            // 后退
              document.getElementById("eight").onclick = function(){
                  audio.currentTime -= 5; 
              }
          }
    </script>
</head>
<body>
    <div class="wrap">
        <div class="content">
            <div class="player">
                <video src="./mv/1.mp4"></video>
                <div class="control">
                    <div>
                        <span class="progress"></span>
                    </div>
                    <div class="timer">
                        <span class="progress_timer">00:00:00</span>/
                        <span class="duration_timer">00:00:00</span>
                    </div>
                </div>
            </div>
        </div>
        <button id="one">播放</button>
        <button id="two">暂停</button>
        <button id="three">快进</button>
        <button id="four">后退</button>
    </div>      

    
    
    <audio src="./mv/周杰伦 - 青花瓷.mp3" controls></audio>
    <br>
    <button id="five">播放</button>
    <button id="six">暂停</button>
    <button id="seven">快进</button>
    <button id="eight">后退</button>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139

css部分

*{
    margin: 0;
    padding: 0;
}
.player{
    width: 720px;
    height: 400px;
    margin: 0 auto;
    position: relative;

}
.player video{
    width: 500px;
    display: block;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    height: 100%;
}
.player .control{
    position: absolute;
    background: rgb(33, 177, 221);
    width: 600px;
    height: 40px;
    border-radius: 5px;
    left: 50%;
    bottom: 10px;
    transform: translateX(-50%);
}
.player .control div{
    display: inline-block;
    line-height: 40px;
    margin-left: 15px;
    font-size: 18px;
    color: rgb(15, 14, 14);
}
.player .control .play_pause,.player .control .expand{
    color: rgb(255,255,0);
}
.player .control div:nth-child(1){
    width: 460px;
    height: 10px;
    background-color: rgba(255,255,255,0.3);
    border-radius: 5px;
    overflow: hidden;
}
.player .control .progress{
    display: block;
    width: 0%;
    height: 10px;
    background: #fff;
}
.player .control .timer{
    font-size: 12px;
}
#one,#two,#three,#four{
    position: relative;
    left: 200px;
    margin: 20px;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/130246
推荐阅读
相关标签
  

闽ICP备14008679号