设为首页收藏本站
查看: 2978|回复: 0

[前端开发] 013 HTML5举例:简单的视频播放器

[复制链接]
  • TA的每日心情
    开心
    2024-1-13 00:31
  • 签到天数: 535 天

    [LV.9]以坛为家II

    发表于 2021-9-15 20:11:18 | 显示全部楼层 |阅读模式
    我们采用 Bootstrap 网站的图标字体,作为播放器的按钮图标。

    index.html的代码如下:

    1. <!DOCTYPE html>
    2. <html>
    3. <head lang="en">
    4.     <meta charset="UTF-8">
    5.     <title></title>
    6.     <!-- 引入字体图标的文件-->
    7.     <link rel="stylesheet" href="css/font-awesome.min.css"/>
    8.     <style>
    9.         *{
    10.             margin: 0;
    11.             padding: 0;
    12.         }
    13.         /*多媒体标题*/
    14.         figcaption{
    15.             text-align: center;
    16.             line-height: 150px;
    17.             font-family: "Microsoft Yahei";
    18.             font-size:24px;
    19.         }

    20.         /* 播放器*/
    21.         .palyer{
    22.             width: 720px;
    23.             height: 360px;
    24.             margin:10px auto;
    25.             border: 1px solid #000;
    26.             background: url(images/loading.gif) center no-repeat #000;
    27.             background-size:auto 100%;
    28.             position: relative;
    29.             border-radius: 20px;

    30.         }

    31.         .palyer video{
    32.             height:100%;
    33.             display: block;
    34.             margin:0 auto;
    35.             /*display: none;*/
    36.         }

    37.         /* 控制条*/

    38.         .controls{
    39.             width: 700px;
    40.             height:40px;
    41.             background-color: rgba(255, 255, 0, 0.3);
    42.             position: absolute;
    43.             bottom:10px;
    44.             left:10px;
    45.             border-radius: 10px;
    46.         }
    47.         /*开关*/
    48.         .switch{
    49.             position: absolute;
    50.             width: 20px;
    51.             height: 20px;
    52.             left:10px;
    53.             top:10px;

    54.             text-align: center;
    55.             line-height: 20px;
    56.             color:yellow;

    57.         }
    58.         /*进度条*/
    59.         .progress{
    60.             width: 432px;
    61.             height: 10px;
    62.             position: absolute;
    63.             background-color: rgba(255,255,255,0.4);
    64.             left:40px;
    65.             top:15px;
    66.             border-radius: 4px;
    67.             overflow: hidden;
    68.         }
    69.         /* 当前进度*/
    70.         .curr-progress{
    71.             width: 50%;
    72.             height: 10px;
    73.             background-color: #fff;
    74.         }
    75.         /* 时间模块*/
    76.         .time{
    77.             width: 120px;
    78.             height: 20px;
    79.             text-align: center;
    80.             line-height: 20px;
    81.             color:#fff;
    82.             position: absolute;
    83.             left:510px;
    84.             top:10px;
    85.             font-size:12px;

    86.         }
    87.         /*全屏*/
    88.         .extend{
    89.             position: absolute;
    90.             width: 20px;
    91.             height: 20px;

    92.             right:20px;
    93.             top:10px;

    94.             text-align: center;
    95.             line-height: 20px;
    96.             color:yellow;
    97.         }

    98.     </style>
    99. </head>
    100. <body>
    101.     <!-- 多媒体-->
    102.     <figure>
    103.         <!--  多媒体标题-->
    104.         <figcaption>视频案例</figcaption>
    105.         <div class="palyer">
    106.             <video src="video/fun.mp4"></video>
    107.             <!-- 控制条-->
    108.             <div class="controls">
    109.                 <!-- 播放暂停-->
    110.                 <a href="#" class="switch  icon-play"></a>
    111.                 <div class="progress">
    112.                     <!-- 当前进度-->
    113.                     <div class="curr-progress"></div>
    114.                 </div>
    115.                 <!-- 时间-->
    116.                 <div class="time">
    117.                     <span class="curr-time">00:00:00</span>/<span class="total-time">00:00:00</span>
    118.                 </div>
    119.                 <!-- 全屏-->
    120.                 <a href="#" class="extend  icon-resize-full"></a>
    121.             </div>

    122.         </div>
    123.     </figure>

    124.     <script>
    125.         // 思路:
    126.         /*
    127.         * 1、点击按钮 实现播放暂停并且切换图标
    128.         * 2、算出视频的总时显示出出来
    129.         * 3、当视频播放的时候,进度条同步,当前时间同步
    130.         * 4、点击实现全屏
    131.         */

    132. //        获取需要的标签
    133.             var  video=document.querySelector('video');
    134. //          播放按钮
    135.             var  playBtn=document.querySelector('.switch');
    136. //          当前进度条
    137.             var  currProgress=document.querySelector('.curr-progress');
    138. //          当前时间
    139.             var  currTime=document.querySelector('.curr-time');
    140. //          总时间
    141.             var  totalTime=document.querySelector('.total-time');
    142. //          全屏
    143.             var extend=document.querySelector('.extend');

    144.             var tTime=0;

    145. //         1、点击按钮 实现播放暂停并且切换图标

    146.            playBtn.onclick=function(){
    147. //               如果视频播放 就暂停,如果暂停 就播放
    148.                if(video.paused){
    149. //                   播放
    150.                    video.play();
    151.                    //切换图标
    152.                    this.classList.remove('icon-play');
    153.                    this.classList.add('icon-pause');
    154.                }else{
    155. //                   暂停
    156.                     video.pause();
    157. //                   切换图标
    158.                    this.classList.remove('icon-pause');
    159.                    this.classList.add('icon-play');}
    160.            }

    161. //        2、算出视频的总时显示出出来
    162. //        当时加载完成后的事件,视频能播放的时候
    163.         video.oncanplay=function(){
    164. //             获取视频总时长
    165.             tTime=video.duration;
    166.             console.log(tTime);

    167. //          将总秒数 转换成 时分秒的格式:00:00:00
    168. //            小时
    169.             var h=Math.floor(tTime/3600);
    170. //            分钟
    171.             var m=Math.floor(tTime%3600/60);
    172. //            秒
    173.             var s=Math.floor(tTime%60);

    174. //            console.log(h);
    175. //            console.log(m);
    176. //            console.log(s);

    177. //            把数据格式转成 00:00:00
    178.             h=h>=10?h:"0"+h;
    179.             m=m>=10?m:"0"+m;
    180.             s=s>=10?s:"0"+s;


    181.             console.log(h);
    182.             console.log(m);
    183.             console.log(s);
    184. //            显示出来
    185.             totalTime.innerHTML=h+":"+m+":"+s;
    186.         }
    187. //   * 3、当视频播放的时候,进度条同步,当前时间同步
    188. //         当时当前时间更新的时候触发
    189.         video.ontimeupdate=function(){
    190. //            获取视频当前播放的时间
    191. //           console.log(video.currentTime);
    192. //            当前播放时间
    193.             var cTime=video.currentTime;
    194. //           把格式转成00:00:00

    195.             var h=Math.floor(cTime/3600);
    196. //            分钟
    197.             var m=Math.floor(cTime%3600/60);
    198. //            秒
    199.             var s=Math.floor(cTime%60);

    200. //            把数据格式转成 00:00:00
    201.             h=h>=10?h:"0"+h;
    202.             m=m>=10?m:"0"+m;
    203.             s=s>=10?s:"0"+s;

    204. //            显示出当前时间
    205.             currTime.innerHTML=h+":"+m+":"+s;

    206. //            改变进度条的宽度: 当前时间/总时间
    207.             var value=cTime/tTime;

    208.             currProgress.style.width=value*100+"%";

    209.         }

    210. //        全屏
    211.         extend.onclick=function(){
    212. //            全屏的h5代码
    213.             video.webkitRequestFullScreen();
    214.         }

    215.     </script>
    216. </body>
    217. </html>
    复制代码
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    红盟社区--红客联盟 

    Processed in 0.059385 second(s), 21 queries.

    站点统计| 举报| Archiver| 手机版| 黑屋 |   

    备案号:冀ICP备20006029号-1 Powered by HUC © 2001-2021 Comsenz Inc.

    手机扫我进入移动触屏客户端

    关注我们可获取更多热点资讯

    Honor accompaniments. theme macfee

    快速回复 返回顶部 返回列表