js实现音频控制进度条功能
程序员文章站
2022-06-09 12:45:19
效果图:
代码如下:
效果图:
代码如下:
<!doctype html> <html> <head> <meta name="author" content="dony"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="pragma" content="no-cache"> <meta name="renderer" content="webkit"> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <title>音频控制进度条</title> <style type="text/css"> body,div{padding: 0;margin: 0;} .m-main{width:560px; height: 100%; margin: 3% auto; background-color:#30a5ff;} .m-main video{display: none; } .m-main .player { width: 100%; height: 150px; position: relative; bottom: 0px; } .m-main .player>a{display: inline-block; width: 20%; margin: 0 auto; padding: 5%; color: #fff; text-align: center;} .m-main .play-box { width: 100%; margin: 0 auto; } .m-main .play-box .left { width: 100%; float: left; } .m-main .play-box .left p.timeline { width: 70%;height: 10px; background-color: rgba(216, 216, 216, 0.5); border-radius: 5px; margin:30px auto 0; position: relative; z-index: 2; } .m-main .play-box .left p.timeline span {position: relative; width: 0px; height: 10px; background-color: #fff; border-radius: 5px; display: block; -webkit-transition: width ease-out 0.3s;-o-transition: width ease-out 0.3s;transition: width ease-out 0.3s;} .m-main .play-box .left p.timeline span:after{content: ""; position: absolute; top: -4px; right:-0.6rem;width: 1.2rem; height:1.2rem; border-radius: 50%;background-color: #fff;} .m-main .play-box .left div.info { height: 26px; line-height: 26px; font-size: 14px; color: #000; position: relative; top: -18px; margin:0 10px; z-index: 1;color: #fff;} .m-main .play-box .left div.info .size { float: left; display: block;} .m-main .play-box .left div.info .timeshow { float: right; display: block;} </style> </head> <body> <div class="m-main"> <div class="player"> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" id="js-play">播放</a> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" id="js-pause">暂停</a> <div class="play-box"> <div class="left"> <p class="timeline"><span style=""></span></p> <div class="info"> <span class="size">00:00</span> <span class="timeshow">00:00</span> </div> </div> </div> </div> <div class="video"> <video controls autoplay name="media" id="js-video"><source src="http://chipsguide.snaillove.com/public/uploads/file_server_storage/audio/2017/03/23/19/192_20003137.mp3" type="video/mp4"></video> </div> </div> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> $(function(){ audiocontrol('js-video') function audiocontrol(id){ // 音频控制进度条 var audio = document.getelementbyid(id); $(audio).on('loadedmetadata',function(){ audio.pause(); // 进度条控制 $(document).on('touchend','.timeline',function(e){ var x = e.originalevent.changedtouches[0].clientx-this.offsetleft; var x = x < 0 ? 0 : x ; var w = $(this).width(); var place = x > w ? w : x; audio.currenttime = (place/w).tofixed(2)*audio.duration $(this).children().css({width:(place/w).tofixed(2)*100+"%"}) }); // 播放 $(document).on('click','#js-play',function(){ audio.play() }); // 暂停 $(document).on('click','#js-pause',function(){ audio.pause() }); }) setinterval(function () { var currenttime = audio.currenttime; settimeshow(currenttime); }, 1000); // 设置播放时间 function settimeshow(t) { t = math.floor(t); var playtime = secondtomin(audio.currenttime); $(".size").html(playtime); $('.timeshow').text(secondtomin(audio.duration)) $('.timeline').children().css({width:(t/audio.duration).tofixed(4)*100+"%"}) } // 计算时间 function secondtomin(s) { var mm = math.floor(s / 60); var ss = s % 60; if (mm < 10) mm = "0" + mm; if (ss < 10) ss = "0" + ss; var min = mm + ":" + ss; return min.split('.')[0]; } } }) </script> </body> </html>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!