我们采用 Bootstrap 网站的图标字体,作为播放器的按钮图标。
index.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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 <!DOCTYPE html > <html > <head lang ="en" > <meta charset ="UTF-8" > <title > </title > <link rel ="stylesheet" href ="css/font-awesome.min.css" /> <style > *{ margin: 0; padding: 0; } figcaption{ text-align: center; line-height: 150px; font-family: "Microsoft Yahei"; font-size :24px ; } .palyer { width: 720px; height: 360px; margin :10px auto ; border : 1px solid #000 ; background: url(images/loading.gif) center no-repeat #000; background-size :auto 100%; position: relative; border-radius: 20px; } .palyer video { height :100 %; display: block; margin :0 auto ; } .controls { width: 700px; height :40px ; background-color : rgba (255, 255, 0, 0.3 ); position: absolute; bottom :10px ; left :10px ; border-radius: 10px; } .switch { position: absolute; width: 20px; height: 20px; left :10px ; top :10px ; text-align: center; line-height: 20px; color :yellow ; } .progress { width: 432px; height: 10px; position: absolute; background-color : rgba (255,255,255,0.4 ); left :40px ; top :15px ; border-radius: 4px; overflow: hidden; } .curr-progress { width: 50%; height: 10px; background-color : #fff ; } .time { width: 120px; height: 20px; text-align: center; line-height: 20px; color :#fff ; position: absolute; left :510px ; top :10px ; font-size :12px ; } .extend { position: absolute; width: 20px; height: 20px; right :20px ; top :10px ; text-align: center; line-height: 20px; color :yellow ; } </style > </head > <body > <figure > <figcaption > 视频案例</figcaption > <div class ="palyer" > <video src ="video/fun.mp4" > </video > <div class ="controls" > <a href ="#" class ="switch icon-play" > </a > <div class ="progress" > <div class ="curr-progress" > </div > </div > <div class ="time" > <span class ="curr-time" > 00:00:00</span > /<span class ="total-time" > 00:00:00</span > </div > <a href ="#" class ="extend icon-resize-full" > </a > </div > </div > </figure > <script > /* * 1、点击按钮 实现播放暂停并且切换图标 * 2、算出视频的总时显示出出来 * 3、当视频播放的时候,进度条同步,当前时间同步 * 4、点击实现全屏 */ var video=document .querySelector('video' ); var playBtn=document .querySelector('.switch' ); var currProgress=document .querySelector('.curr-progress' ); var currTime=document .querySelector('.curr-time' ); var totalTime=document .querySelector('.total-time' ); var extend=document .querySelector('.extend' ); var tTime=0 ; playBtn.onclick=function () { if(video.paused){ video.play(); this .classList.remove('icon-play' ); this .classList.add('icon-pause' ); }else { video.pause(); this .classList.remove('icon-pause' ); this .classList.add('icon-play' );} } video.oncanplay=function () { tTime=video.duration; console .log(tTime); 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; console .log(h); console .log(m); console .log(s); totalTime.innerHTML=h+":" +m+":" +s; } video.ontimeupdate=function () { var 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; currTime.innerHTML=h+":" +m+":" +s; var value=cTime/tTime; currProgress.style.width=value*100 +"%" ; } extend.onclick=function () { video.webkitRequestFullScreen(); } </script > </body > </html >
工程文件: