原生JS实现视频播放功能

准备工作:

  • 一个视频文件
  • 一份字体图标文件
  • 缓冲加载的GIF图标
  • 效果如图
    原生JS实现视频播放功能_第1张图片

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>制作视频播放title>
    
    <link rel="stylesheet" href="css/font-awesome.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        figcaption {
            text-align: center;
            font-family: "Microsoft YaHei";
            font-size: 24px;
            font-weight: 700;
            line-height: 150px;
        }
        .player {
            width:640px;
            height:360px;
            margin: 10px auto;
            background-color: pink;
            background:url("login_on.gif")top center no-repeat;
            background-size: auto 100%;
            position: relative;
            border-radius: 10px;
            overflow: hidden;
        }
        .player video {
            height:100%;
            display: block;
            margin: 0px auto;
            display: block;
        }
        .controls {
            width: 620px;
            height:40px;
            background-color: rgba(0,0,0,0.5);
            position: absolute;
            left: 10px;
            bottom: 10px;
            border-radius: 10px;
        }
        .switch {
            width: 20px;
            height: 20px;
            position: absolute;
            top:10px;
            left: 10px;
            display: block;
            text-align: center;
            line-height: 20px;
            color: yellow;
        }
        .progerss {
            width: 400px;
            height:10px;
            position: absolute;
            background-color: rgba(255,255,255,0.4);
            left: 40px;
            top:15px;
            border-radius: 4px;
            overflow: hidden;
        }
        .cur-progress {
            width:0%;
            height:100%;
            background: yellow;
        }
        .time {
            width: 120px;
            height: 20px;
            text-align: center;
            line-height: 20px;
            position: absolute;
            left: 450px;
            top:10px;
            font-size: 12px;
            color: #fff;
        }
        .exted {
            width: 20px;
            height: 20px;
            position: absolute;
            top:10px;
            right: 10px;
            text-align: center;
            line-height: 20px;
            color: yellow;
        }
    style>
head>
<body>
    
    <figure>
        <figcaption>制作视频播放figcaption>
        <div class="player">
            
            
            
            <video src="1.mp4">
                <source src="1.mp4"/>
                <source src="1.ogg"/>
                <source src="1.webm"/>
                抱歉!您的浏览器不支持视频标签
            video>
            <div class="controls">
                <a href="#" class="switch icon-play" title="播放">a>
                <div class="progerss">
                    <div class="cur-progress">div>
                div>
                <div class="time">
                    <span class="curr-time">00:00:00span>/
                    <span class="total-time">00:00:00span>
                div>
                <a href="#" class="exted icon-fullscreen" title="全屏">a>
            div>
        div>
    figure>
    <script>
        //实现步骤:
        /*
        * 1: 点击按钮播放 实现播放暂停,更改图标
        * 2: 算出视频的总时间出来
        * 3: 当视频播放的时候,精度条同步
        * 4: 设置视频播放器全屏显示
        * */
        //获取视频
        var video = document.querySelector("video")
        //播放/暂停按钮
        var playBtn = document.querySelector(".switch");
        //获取滚动条
        var crrProgress = document.querySelector(".cur-progress");
        //获取开始时间
        var crrTime = document.querySelector(".curr-time");
        //获取总时间
        var totalTime = document.querySelector(".total-time");
        //全屏
        var exted = document.querySelector(".exted");
        var tTime;
        var cTime;
        //点击播放时候
        playBtn.onclick = function () {
            //如果视频播放就暂停,如果是暂停就播放
            if(video.paused){
                video.play();
                this.classList.remove("icon-play");
                this.classList.add("icon-pause");
                playBtn.title = "暂停";
            }else{
                video.pause();
                this.classList.remove("icon-pause");
                this.classList.add("icon-play");
                playBtn.title = "播放";
            }
        }
        //算出视频的时间显示出来,视频能播放的时候
        //oncanplay : 当时加载完成后的时间,
        video.oncanplay = function () {
            //显示总时长
            tTime = video.duration;
            //将总秒数,转换为 时分秒格式 00:00:00
            var h = Math.floor(tTime / 3600);
            var m = Math.floor(tTime % 3600 / 60);
            var s = Math.floor(tTime % 60);
            h = h >= 10 ? h :"0" + h;
            m = m >= 10 ? m :"0" + m;
            s = s >= 10 ? s :"0" + s;
            totalTime.innerHTML = h + ":" + m + ":" + s;
        }
        //当视频播放的时候,进度条同步,当前时间同步,
        // ontimeupdate : 当时间当前时间更新的时候触发
        video.ontimeupdate = function () {
            cTime = video.currentTime;
            var h = Math.floor(cTime / 3600);
            var m = Math.floor(cTime % 3600 / 60);
            var s = Math.floor(cTime % 60);
            h = h >= 10 ? h :"0" + h;
            m = m >= 10 ? m :"0" + m;
            s = s >= 10 ? s :"0" + s;
            crrTime.innerHTML = h + ":" + m + ":" + s;

            //改变精度条的宽度  :当前时间/总时间
            var value = cTime / tTime;
            crrProgress.style.width = value * 100 + "%";
        }
        //实现全屏的效果
        exted.onclick = function () {
            //全屏的H5写法
            video.webkitRequestFullScreen();
        }
    script>
body>
html>

你可能感兴趣的:(javascript)