|
|

楼主 |
发表于 2022-9-10 07:24
|
显示全部楼层
代码分享
- <style>
- #papa { margin: auto; width: 1024px; height: 640px; background: #444 url('https://638183.freep.cn/638183/Pic/2022/time.jpg') no-repeat center/cover; display: grid; place-items: center; box-shadow: 3px 3px 20px #000; position: relative; z-index: 1; }
- #mplayer { position: absolute; }
- #tmsg { font: normal 1em sans-serif; fill: #ccc; }
- #lrc { position: absolute; top: 10px; left: 10px; font: bold 2em sans-serif; letter-spacing: 3px; animation: mov 20s linear infinite alternate; }
- @keyframes mov { to { left: 614px; } }
- </style>
- <div id="papa">
- <svg id="mplayer" viewBox="0 0 100 100" width="100" height="100">
- <circle id="track" cx="50" cy="50" r="45" fill="none" stroke="snow" stroke-width="6" style="cursor: pointer" />
- <line id="hand" x1="50" y1="50" x2="50" y2="10" stroke="red" stroke-width="1" />
- <circle cx="50" cy="50" r="4" fill="red" />
- <path id="curPath" d="M 10 60 Q 50 0 90 60" fill="none" stroke="none"/>
- <path id="durPath" d="M 0 38 Q 50 100 100 38" fill="none" stroke="none"/>
- <g id="tmsg">
- <text x="52%" y="0"><textPath id="curMsg" xlink:href="#curPath" text-anchor="middle" dominant-baseline="text-after-edge">00:00</textPath></text>
- <text x="60%" y="0"><textPath id="durMsg" xlink:href="#durPath" text-anchor="middle" dominant-baseline="text-before-edge">00:00</textPath></text>
- </g>
- <circle id="btnplay" cx="50" cy="50" r="35" fill="transparent" style="cursor: pointer" />
- </svg>
- <svg id="lrc" width="400" height="60">
- <text id="lrctxt" x="50%" y="30" fill="none" stroke="snow" text-anchor="middle" dominant-baseline="middle">纯音乐 - 时间</text>
- </svg>
- </div>
- <script>
- let cc = { x: 1*track.getAttribute('cx'), y: 1*track.getAttribute('cy'), };
- let aud = new Audio();
- aud.src = 'https://music.163.com/song/media/outer/url?id=1920607219.mp3';
- aud.autoplay = true;
- aud.loop = true;
- aud.addEventListener('timeupdate', () => {
- let progress = aud.currentTime * 360 / aud.duration;
- hand.setAttribute('transform', 'rotate(' + progress +', 50, 50)');
- curMsg.textContent = toMin(aud.currentTime);
- durMsg.textContent = toMin(aud.duration);
- });
- track.onclick = (e) => {
- let angle = Math.atan2(e.offsetY - cc.y, e.offsetX - cc.x) * 180 / Math.PI;
- angle+= (e.offsetX < cc.x && e.offsetY < cc.y) ? 450 : 90;
- aud.currentTime = aud.duration * angle / 360;
- }
- btnplay.onclick = () => aud.paused ? aud.play() : aud.pause();
- let toMin = (val) => {
- if (!val) return '00:00';
- val = Math.floor(val);
- let min = parseInt(val / 60), sec = parseFloat(val % 60);
- if(min < 10) min = '0' + min;
- if(sec < 10) sec = '0' + sec;
- return min + ':' + sec;
- }
- </script>
复制代码
|
|