写在前面 好久没跟新博客了。最近一直忙着恶补JavaScript的知识,没办法,小白就要多练多看。这两天写了个简单的视频播放器,实现了一些简单的功能。今天就把这个简单的播放器分享出来,仅供参考。希望能对大家有点帮助。。。尽管这个写的比较low。。
效果功能 效果预览 效果如下图:
实现功能 播放器是用原生js写的,实现了一些基本的功能:
播放/暂停、停止、空格键暂停/播放;
音量加减、支持拖动调节音量以及上下方向键调整音量;
支持静音、全屏播放;
支持发送弹幕,弹幕颜色随机、出现位置随机;
支持播放列表点击切换、支持自动循环列表播放。
目前还存在的bug 目前发现,在播放过程中,通过键盘上下方向键调节音量后,鼠标划入视频区域,控制条无法显示。。暂未解决。
源码分享 说明:源代码中引入了font-awesome的字体图标库。
HTML部分代码 页面结构布局较简单,下面是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 <!DOCTYPE html > <html > <head > <meta charset ="utf-8" > <title > 视频播放器</title > <link rel ="stylesheet" type ="text/css" href ="css/main.css" > <link href ="css/font-awesome.min.css" rel ="stylesheet" type ="text/css" > </head > <body > <div class ="box clearFix" > <div class ="vbox fl" > <video id ="myvideo" preload ="auto" > </video > <div class ="ctrlbox clearFix" > <div class ="left fl" > <i class ="fa fa-play" id ="playbtn" title ="点击播放" > </i > <i class ="fa fa-stop" id ="stopbtn" title ="停止" > </i > <span id ="nowtime" > 00:00</span > <span id ="sep" > /</span > <span id ="fulltime" > </span > <span class ="progress" title ="点击跳到这儿" > <span id ="pgbar" > </span > </span > </div > <div class ="right fr" > <i class ="fa fa-volume-up" id ="volume" title ="点击静音" > </i > <i class ="fa fa-expand" id ="expand" title ="全屏" > </i > </div > <div class ="volctrl" > <span id ="volnum" > 100</span > <div id ="volbarcon" > <span id ="volbar" > </span > </div > </div > </div > <div class ="mask" > <i id ="mbtn" class ="fa fa-play-circle" title ="点击播放" > </i > </div > </div > <div class ="playlist fl" > <h3 > 播放列表</h3 > <ul id ="list" > </ul > </div > <div class ="msgbox" > <label > 昵称:</label > <input type ="text" id ="usrname" > <label > 弹幕:</label > <input type ="text" id ="message" > <input type ="button" id ="send" value ="发送" /> </div > </div > <script type ="text/javascript" src ="js/main.js" > </script > </body > </html >
CSS代码 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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 @charset "UTF-8" ;body ,ul ,li { margin : 0 ; padding : 0 ; } .fl { float : left; } .fr { float : right; } .clearFix :after { content : '' ; display : block; clear : both; } .box { width : 1000px ; height : 450px ; margin : 0 auto; } .vbox { position : relative; width : 800px ; height : 450px ; overflow : hidden; } .vbox :hover .ctrlbox { display : block; } .vbox span { position : absolute; white-space : nowrap; font-size : 18px ; } .vbox .ctrlbox span { color : #fff ; position : relative; } .mask { width : 90px ; height : 90px ; position : absolute; top : 180px ; left : 355px ; text-align : center; } #mbtn { position : relative; z-index : 900 ; display : block; height : 90px ; width : 80px ; padding : 0 5px ; margin : 0 ; font-size : 90px ; color : #000 ; line-height : 90px ; } #fulltime { color : #afafaf ; } #nowtime ,#fulltime { display : inline-block; width : 44px ; font-size : 16px ; line-height : 24px ; top : -3px ; } #myvideo { width : 800px ; height : 450px ; transition : all 1s ease; } .ctrlbox { width : 770px ; height : 50px ; position : absolute; left : 0 ; bottom : 0 ; padding : 0 10px 0 20px ; background-color : #141414 ; font-size : 24px ; line-height : 50px ; color : #fff ; display : none; } i { font-style : normal; display : inline-block; width : 30px ; height : 30px ; margin-right : 5px ; text-align : left; cursor : pointer; } #playbtn { display : inline-block; width : 20px ; height : 24px ; text-align : center; } #sep { position : relative; top : -2px ; } .progress { z-index : 50 ; position : relative; display : inline-block; width : 480px ; height : 8px ; margin-bottom : 4px ; margin-left : 6px ; border-radius : 8px ; overflow : hidden; background-color : #fff ; cursor : pointer; } #pgbar { z-index : 10 ; position : absolute; left : 0 ; top : 0 ; display : inline-block; height : 8px ; border-radius : 8px ; background-color : #9f9f9f ; } #volume { line-height : 50px ; } .volctrl { width : 40px ; height : 110px ; padding : 5px 0 ; background-color : #141414 ; text-align : center; position : absolute; right : 58px ; bottom : 45px ; display : none; } #volnum { position : absolute; display : block; width : 100% ; height : 20px ; font-size : 14px ; line-height : 20px ; } #volbarcon { display : block; margin-left : 16px ; margin-top : 20px ; width : 8px ; height : 90px ; border-radius : 8px ; background-color : #fff ; position : absolute; z-index : 100 ; overflow : hidden; cursor : pointer; } #volbar { position : absolute; left : 0 ; bottom : 0 ; z-index : 20 ; display : block; width : 8px ; height : 100% ; border-radius : 8px ; background-color : #9f9f9f ; } .playlist { height : 450px ; width : 200px ; position : relative; background-color : #1f1f1f ; } h3 { margin : 10px 0 0 ; padding : 0 ; font-size : 20px ; color : #fff ; padding-left : 10px ; } #list { width : 100% ; margin-top : 20px ; } #list li { list-style : none; width : 185px ; height : 40px ; margin : 0 5px 5px ; padding-left : 5px ; font-size : 14px ; line-height : 40px ; overflow : hidden; white-space : nowrap; text-overflow : ellipsis; color : #fff ; background-color : #2F2F2F ; cursor : pointer; } #list li :hover { background-color : #4f4f4f ; } .msgbox { clear : both; width : 980px ; height : 50px ; padding : 0 10px ; margin : 10px auto 0 ; } input { width : 200px ; font-size : 16px ; padding : 8px ; } #usrname { width : 120px ; margin-right : 20px ; } label { font-size : 16px ; line-height : 30px ; } #send { width : 80px ; padding : 5px ; border-radius : 3px ; margin-top : 10px ; margin-left : 20px ; font-size : 20px ; color : #fff ; line-height : 26px ; border : 0 ; outline : none; cursor : pointer; background-color : #1C96D6 ; }
js代码 以下是js代码部分,注释较为详细,应该很容易看明白。
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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 var names = ["Taylor Swift - Begin Again.mp4" ,"Taylor Swift - Safe & Sound.mp4" ,"Taylor Swift - Red.mp4" ,"Taylor Swift - I Don’t Wanna Live Forever.mp4" ,"我怀念的 - 林俊杰.mp4" ];var list = document .getElementById ("list" );var myvideo = document .getElementById ("myvideo" );var mask = document .getElementsByClassName ("mask" )[0 ];var mbtn = document .getElementById ("mbtn" );var ctrlbox = document .getElementsByClassName ("ctrlbox" )[0 ];var play = document .getElementById ("playbtn" );var stop = document .getElementById ("stopbtn" );var mute = document .getElementById ("volume" );var expand = document .getElementById ("expand" );var nowtime = document .getElementById ("nowtime" );var fulltime = document .getElementById ("fulltime" );var pgbar = document .getElementById ("pgbar" );var index; for (var i = 0 ; i < names.length ; i++){ var li = document .createElement ("li" ); li.innerText = names[i]; li.title = li.innerText ; list.appendChild (li); } var lis = list .getElementsByTagName ("li" );myvideo.src = "video/" + lis[0 ].innerText ; lis[0 ].style .backgroundColor = "#5f5f5f" ; index = 0 ; myvideo.oncanplay =function ( ){ fulltime.innerText = secTomin (myvideo.duration ); } function secTomin (seconds ){ var s = Math .round (seconds % 60 ); if (s < 10 ){ s = "0" + s; } var m = Math .floor (seconds/60 ); if (m < 10 ){ m = "0" + m; } var t = m + ":" + s; return t; } myvideo.addEventListener ('timeupdate' , function ( ) { var currentPos = this .currentTime ; var percentage = 100 * currentPos / myvideo.duration ; pgbar.style .width = percentage + '%' ; nowtime.innerText = secTomin (currentPos); if (myvideo.ended ){ setTimeout (loopPlay, 1000 ); } }); function loopPlay ( ){ if (index == lis.length -1 ){ index = 0 } else { index += 1 ; } for (var j = 0 ; j < lis.length ; j++){ lis[j].style .backgroundColor = "#2f2f2f" ; } lis[index].style .backgroundColor = "#5f5f5f" ; myvideo.src = "video/" +lis[index].innerText ; play.onclick (); } var progress = document .getElementsByClassName ("progress" )[0 ];progress.onmousedown = function (e ){ e = e || window .event ; updatebar (e.pageX ); } function updatebar (x ){ var positions = x - progress.offsetLeft - vbox.offsetLeft ; var percent = 100 * positions / (progress.offsetWidth ); if (percent > 100 ) { percent = 100 ; } if (percent < 0 ) { percent = 0 ; } pgbar.style .width = percent + '%' ; myvideo.currentTime = myvideo.duration * percent / 100 ; }; var volnum = document .getElementById ("volnum" );var volbarcon = document .getElementById ("volbarcon" );var volbar = document .getElementById ("volbar" );volbarcon.onclick = function (e ){ e = e || window .event ; volControl (e.pageY ); } var volctrl = document .getElementsByClassName ("volctrl" )[0 ];mute.onmouseover = function ( ){ volctrl.style .display = "block" ; } mute.onmouseout = function ( ){ volctrl.style .display = "none" ; } volctrl.onmousemove = function ( ) { volctrl.style .display = "block" ; } volctrl.onmouseout = function ( ) { volctrl.style .display = "none" ; } myvideo.addEventListener ('volumechange' , function ( ) { if (myvideo.muted ){ volnum.innerText = "0" ; } else { volnum.innerText = parseInt (myvideo.volume *100 ); } }); function volControl (y ){ var curpos = vbox.offsetHeight -ctrlbox.offsetHeight - y; var per = 100 * curpos / (volbarcon.offsetHeight ); volbar.style .height = per + '%' ; myvideo.volume = per / 100 ; if (myvideo.volume == 0 ){ volume.className = "fa fa-volume-off" ; mute.style .color = "#ccc" ; } else if (myvideo.volume >0 && myvideo.volume < 0.5 ){ volume.className = "fa fa-volume-down" ; mute.style .color = "#fff" ; } else { volume.className = "fa fa-volume-up" ; mute.style .color = "#fff" ; } } play.onclick = function ( ){ if (!myvideo.paused ){ myvideo.pause (); play.className = "fa fa-play" ; play.title = "点击播放" ; mask.style .display = "block" ; } else { myvideo.play (); play.className = "fa fa-pause" ; play.title = "点击暂停" ; mask.style .display = "none" ; } } document .onkeydown =function (event ){ var e = event || window .event || arguments .callee .caller .arguments [0 ]; if (e && e.keyCode ==32 ){ play.onclick (); } if (e && e.keyCode ==13 ){ if (usrname.value !=="" && message.value !=="" ){ send.onclick (); } else { if (usrname.value =="" ){ alert ("请填写昵称后再发送!" ); } else { alert ("请填写弹幕内容后再发送!" ); } } } if (e && e.keyCode ==38 ){ showctrl (); if (myvideo.volume !== 1 ){ myvideo.volume += 0.01 ; volbar.style .height = myvideo.volume * 100 + "%" ; } else { myvideo.volume = 1 ; } setTimeout (hidectrl,2000 ); } if (e && e.keyCode ==40 ){ showctrl (); if (myvideo.volume !== 0 ){ myvideo.volume -= 0.01 ; volbar.style .height = myvideo.volume * 100 + "%" ; } else { myvideo.volume = 0 ; } setTimeout (hidectrl,2000 ); } if (e && e.keyCode ==39 ){ ctrlbox.style .display = "block" ; if (myvideo.duration - myvideo.currentTime > 15 ){ myvideo.currentTime += 10 ; pgbar.style .width = myvideo.currentTime /myvideo.duration + "%" ; } setTimeout (function ( ){ ctrlbox.style .display = "none" ; }, 2000 ); } if (e && e.keyCode ==37 ){ ctrlbox.style .display = "block" ; if (myvideo.currentTime > 15 ){ myvideo.currentTime -= 10 ; pgbar.style .width = myvideo.currentTime /myvideo.duration + "%" ; } setTimeout (function ( ){ ctrlbox.style .display = "none" ; }, 2000 ); } }; function showctrl ( ){ ctrlbox.style .display = "block" ; volctrl.style .display = "block" ; } function hidectrl ( ){ ctrlbox.style .display = "none" ; volctrl.style .display = "none" ; } for (var i = 0 ; i < lis.length ; i++){ lis[i].onclick = function ( ){ myvideo.src = "video/" + this .innerText ; play.onclick (); for (var j = 0 ; j < lis.length ; j++){ lis[j].style .backgroundColor = "#2f2f2f" ; } this .style .backgroundColor = "#5f5f5f" ; index = this .index ; } } myvideo.onclick = function ( ){ play.onclick (); } myvideo.ondblclick = function ( ){ expand.onclick (); } stop.onclick = function ( ){ myvideo.currentTime = "0" ; myvideo.pause (); } mute.onclick = function ( ){ if (!myvideo.muted ){ myvideo.muted = true ; this .className = "fa fa-volume-off" ; this .style .color = "#ccc" ; this .title = "取消静音" ; volbar.style .height = "0" ; } else { myvideo.muted = false ; this .className = "fa fa-volume-up" ; this .style .color = "#fff" ; this .title = "点击静音" ; volbar.style .height = myvideo.volume * 100 + "%" ; volnum.innerText = myvideo.volume * 100 ; } } expand.onclick =function ( ){ requestFullScreen (myvideo); }; function requestFullScreen (element ) { var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen ; if (requestMethod) { requestMethod.call (element); } else if (typeof window .ActiveXObject !== "undefined" ) { var wscript = new ActiveXObject ("WScript.Shell" ); if (wscript !== null ) { wscript.SendKeys ("{F11}" ); } } } mbtn.onclick = function ( ){ play.onclick (); } var vbox = document .getElementsByClassName ("vbox" )[0 ];var user = document .getElementById ("usrname" );var message = document .getElementById ("message" );var send = document .getElementById ("send" );var str = "" ;var timer;send.onclick = function ( ){ str = user.value + " : " + message.value ; var span1 = document .createElement ("span" ); span1.innerText = str; vbox.appendChild (span1); span1.style .top = Math .floor (Math .random ()*401 ) + "px" ; span1.style .left = "800px" ; span1.style .color = "#" +Math .floor (Math .random ()*0xffffff ).toString (16 ); timer = setInterval (function ( ){ span1.style .left = (parseInt (span1.style .left )-1 ) + "px" ; if (span1.style .left == "-400px" ){ vbox.removeChild (span1); } var spans = vbox.getElementsByTagName ("span" ); if (spans.length == 0 ){ clearInterval (timer); } }, 18 ); str = "" ; user.value = "" ; message.value = "" ; user.blur (); message.blur (); }