|
|

楼主 |
发表于 2022-10-21 19:29
|
显示全部楼层
CSS+HTML。样式和之前的 svg 播放器一样。代码:
- <style>
- #papa { margin: auto; width: 740px; height: 460px; background: #fff; box-shadow: 3px 3px 20px #000; display: grid; place-items: center; position: relative; }
- #mplayer { position: absolute; bottom: 20px; display: grid; grid-template-columns: auto auto auto; place-items: center; gap: 6px; user-select: none; }
- #btnplay { width: 40px; height: 40px; text-align: center; font: normal 40px/40px sans-serif; color: green; cursor: pointer; animation: rot linear 4s infinite; }
- #prog { --ww: 0px; width: 300px; height: 12px; border: 1px solid green; border-radius: 6px; background: snow; position: relative; }
- #prog::before { position: absolute; content: ''; width: var(--ww); height: 12px; border-radius: 6px; background: snow linear-gradient(90deg, green, red); }
- #audtime { font: normal 14px sans-serif; color: green; }
- @keyframes rot { to { transform: rotate(1turn); } }
- </style>
- <div id="papa">
- <div id="mplayer">
- <span id="btnplay">❁</span>
- <span id="prog"></span>
- <span id="audtime">00:00 | 00:00</span>
- </div>
- </div>
- <script>
- let aud = new Audio();
- aud.autoplay = true;
- aud.loop = true;
- aud.src = 'https://music.163.com/song/media/outer/url?id=1421117420.mp3';
- btnplay.onclick = () => aud.paused ? aud.play() : aud.pause();
- prog.onclick = (e) => aud.currentTime = aud.duration * e.offsetX / prog.offsetWidth;
- aud.addEventListener('pause', () => mState());
- aud.addEventListener('play', () => mState());
- aud.addEventListener('timeupdate', () => {
- let prg = aud.currentTime * prog.offsetWidth / aud.duration < 6 ? 6 : aud.currentTime * prog.offsetWidth / aud.duration;
- prog.style.setProperty('--ww', prg + 'px');
- audtime.innerText = toMin(aud.currentTime) + ' | ' + toMin(aud.duration);
- });
- let mState = () => aud.paused ? (btnplay.style.animationPlayState='paused') : (btnplay.style.animationPlayState='running');
- 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;}
- if(aud.paused) btnplay.style.animationPlayState = 'paused';
- </script>
复制代码
|
|