可能(css-doodle播放器+lrc歌词同步演示)
本帖最后由 马黑黑 于 2023-5-12 21:48 编辑 <br /><br /><style>#papa {margin: 0 0 0 calc(50% - 531px);width: 900px;height: 559px;background: tan url('https://638183.freep.cn/638183/t23/1/yujb.jpg');box-shadow: 0 0 8px #000;display: grid;place-items: center;position: relative;--state: paused;}
css-doodle{position: absolute;}
</style>
<div id="papa">
<css-doodle id="mplayer">
:doodle {
@grid: 2 / 300px 60px;
color: var(--color);
bottom: 0;
z-index: 100;
--prog: 0%; --size: 40px; --ttmsg1: '00:00'; --ttmsg2: '00:00'; --color: lightgreen;
}
/* 时间信息 : 左 */
@nth(1) {
@place: 25% 80%;
:after { content: var(--ttmsg1); }
}
/* 控制器 */
@nth(2) {
@size: var(--size);
@shape: windmill;
@place: 50% 35%;
background: var(--color);
animation: rot 6s infinite linear var(--state);
}
/* 时间信息 : 右 */
@nth(3) {
@place: 75% 80%;
:after { content: var(--ttmsg2); }
}
/* 进度条 */
@nth(4) {
@place: 50% 80%;
@size: 100% 2px;
background: #bbb;
display: grid;
place-items: center start;
:before {
content: '';
width: var(--prog);
height: 100%;
background: var(--color);
}
}
@keyframes rot { to { transform: rotate(1turn); } }
</css-doodle>
<css-doodle id="lrc">
:doodle {
@size: auto 4em;
top: 10px;
--geci: "花潮lrc在线"; --motion: cover2; --tt: 1s;
}
/* 单元格两个伪元素显示lrc歌词 */
display: grid;
place-items: center start;
:before, :after {
content: var(--geci);
color: gray; /* 歌词底色 */
font: bold 2em sans-serif;
text-shadow: 1px 1px 2px #000;
white-space: pre;
}
:after {
position: absolute;
width: 0;
color: darkgreen; /* 同步歌词颜色 */
overflow: hidden;
animation: var(--motion) var(--tt) linear forwards var(--state);
}
@keyframes cover1 { from { width: 0; } to { width: 100%; } }
@keyframes cover2 { from { width: 0; } to { width: 100%; } }
</css-doodle>
<audio id="aud" src="https://music.163.com/song/media/outer/url?id=1950343972" autoplay loop></audio>
</div>
<script>
(function() {
let script = document.createElement('script');
script.src = 'https://638183.freep.cn/638183/web/api/css-doodle.js';
document.head.appendChild(script);
let slip = 0.5, mFlag = true, mKey = 0, clickIdx = 0, progChg = 0, cursors = ['default','pointer','pointer'];
let lrcAr = [,,,,,,,,,,,,,,,,,,,,,,,,,,,,,];
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;};
let calcKey = () => {for (j = 0;j < lrcAr.length;j++) {if (aud.currentTime <= lrcAr) {mKey = j - 1;break;}}if (mKey < 0) mKey = 0;if (mKey > lrcAr.length - 1) mKey = lrcAr.length - 1;let time = lrcAr - (aud.currentTime - lrcAr);showLrc(time);};
let mState = () => papa.style.setProperty('--state', aud.paused ? 'paused' : 'running');
let showLrc = (time) => {lrc.style.setProperty('--motion', mFlag ? 'cover1' : 'cover2');lrc.style.setProperty('--geci', '"' + lrcAr + '"');lrc.style.setProperty('--tt', time + 's');mKey += 1;mFlag = !mFlag;};
aud.addEventListener('play', mState, false);
aud.addEventListener('pause', mState, false);
aud.addEventListener('seeked', () => calcKey());
aud.addEventListener('timeupdate', () => {for (j = 0; j < lrcAr.length; j++) {if (aud.currentTime - slip >= lrcAr) {cKey = j;if (mKey === j) showLrc(lrcAr);else continue;}}mplayer.style.setProperty('--ttmsg1', `'${toMin(aud.currentTime)}'`);mplayer.style.setProperty('--ttmsg2', `'${toMin(aud.duration)}'`);mplayer.style.setProperty('--prog',`${100 * aud.currentTime / aud.duration}%`);});
mplayer.onclick = () => {if(clickIdx === 1) aud.paused ? aud.play() : aud.pause();if(clickIdx === 2) aud.currentTime = progChg;};
mplayer.onmousemove = (e) => {let size = parseInt(window.getComputedStyle(mplayer).getPropertyValue('--size')), ww = mplayer.offsetWidth, hh = mplayer.offsetHeight;clickIdx = e.offsetY > hh/1.5 ? 2 : (e.offsetX > (ww - size)/2 && e.offsetX < (ww + size)/2 && e.offsetY < hh/1.5 ? 1 : 0);mplayer.style.cursor = cursors;if(clickIdx > 1) progChg = aud.duration * e.offsetX / ww;};
})();
</script> 帖子代码
<style>
#papa {margin: 0 0 0 calc(50% - 531px);width: 900px;height: 559px;background: tan url('https://638183.freep.cn/638183/t23/1/yujb.jpg');box-shadow: 0 0 8px #000;display: grid;place-items: center;position: relative;--state: paused;}
css-doodle{position: absolute;}
</style>
<div id="papa">
<css-doodle id="mplayer">
:doodle {
@grid: 2 / 300px 60px;
color: var(--color);
bottom: 0;
z-index: 100;
--prog: 0%; --size: 40px; --ttmsg1: '00:00'; --ttmsg2: '00:00'; --color: lightgreen;
}
/* 时间信息 : 左 */
@nth(1) {
@place: 25% 80%;
:after { content: var(--ttmsg1); }
}
/* 控制器 */
@nth(2) {
@size: var(--size);
@shape: windmill;
@place: 50% 35%;
background: var(--color);
animation: rot 6s infinite linear var(--state);
}
/* 时间信息 : 右 */
@nth(3) {
@place: 75% 80%;
:after { content: var(--ttmsg2); }
}
/* 进度条 */
@nth(4) {
@place: 50% 80%;
@size: 100% 2px;
background: #bbb;
display: grid;
place-items: center start;
:before {
content: '';
width: var(--prog);
height: 100%;
background: var(--color);
}
}
@keyframes rot { to { transform: rotate(1turn); } }
</css-doodle>
<css-doodle id="lrc">
:doodle {
@size: auto 4em;
top: 10px;
--geci: "花潮lrc在线"; --motion: cover2; --tt: 1s;
}
/* 单元格两个伪元素显示lrc歌词 */
display: grid;
place-items: center start;
:before, :after {
content: var(--geci);
color: gray; /* 歌词底色 */
font: bold 2em sans-serif;
text-shadow: 1px 1px 2px #000;
white-space: pre;
}
:after {
position: absolute;
width: 0;
color: darkgreen; /* 同步歌词颜色 */
overflow: hidden;
animation: var(--motion) var(--tt) linear forwards var(--state);
}
@keyframes cover1 { from { width: 0; } to { width: 100%; } }
@keyframes cover2 { from { width: 0; } to { width: 100%; } }
</css-doodle>
<audio id="aud" src="https://music.163.com/song/media/outer/url?id=1950343972" autoplay loop></audio>
</div>
<script>
(function() {
let script = document.createElement('script');
script.src = '/css-doodle.min.js';
document.head.appendChild(script);
let slip = 0.5, mFlag = true, mKey = 0, clickIdx = 0, progChg = 0, cursors = ['default','pointer','pointer'];
let lrcAr = [,,,,,,,,,,,,,,,,,,,,,,,,,,,,,];
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;};
let calcKey = () => {for (j = 0;j < lrcAr.length;j++) {if (aud.currentTime <= lrcAr) {mKey = j - 1;break;}}if (mKey < 0) mKey = 0;if (mKey > lrcAr.length - 1) mKey = lrcAr.length - 1;let time = lrcAr - (aud.currentTime - lrcAr);showLrc(time);};
let mState = () => papa.style.setProperty('--state', aud.paused ? 'paused' : 'running');
let showLrc = (time) => {lrc.style.setProperty('--motion', mFlag ? 'cover1' : 'cover2');lrc.style.setProperty('--geci', '"' + lrcAr + '"');lrc.style.setProperty('--tt', time + 's');mKey += 1;mFlag = !mFlag;};
aud.addEventListener('play', mState, false);
aud.addEventListener('pause', mState, false);
aud.addEventListener('seeked', () => calcKey());
aud.addEventListener('timeupdate', () => {for (j = 0; j < lrcAr.length; j++) {if (aud.currentTime - slip >= lrcAr) {cKey = j;if (mKey === j) showLrc(lrcAr);else continue;}}mplayer.style.setProperty('--ttmsg1', `'${toMin(aud.currentTime)}'`);mplayer.style.setProperty('--ttmsg2', `'${toMin(aud.duration)}'`);mplayer.style.setProperty('--prog',`${100 * aud.currentTime / aud.duration}%`);});
mplayer.onclick = () => {if(clickIdx === 1) aud.paused ? aud.play() : aud.pause();if(clickIdx === 2) aud.currentTime = progChg;};
mplayer.onmousemove = (e) => {let size = parseInt(window.getComputedStyle(mplayer).getPropertyValue('--size')), ww = mplayer.offsetWidth, hh = mplayer.offsetHeight;clickIdx = e.offsetY > hh/1.5 ? 2 : (e.offsetX > (ww - size)/2 && e.offsetX < (ww + size)/2 && e.offsetY < hh/1.5 ? 1 : 0);mplayer.style.cursor = cursors;if(clickIdx > 1) progChg = aud.duration * e.offsetX / ww;};
})();
</script>
说明:
帖子使用两个 css-doodle,一个是播放器,一个是歌词,二者分离更便于在帖子布局。
不要因为HTML部分代码多而困扰,实际上,css-doodle应视为独立的标签,帖子的HTML整体结构因此仅是如下所示:
<div id="papa">
<css-doodle id="mplayer"><!-- css-doodle代码 --></css-doodle>
<css-doodle id="lrc"><!-- css-doodle代码 --></css-doodle>
</div> 新播放器带歌词,我喜欢{:5_106:} 南无月 发表于 2023-5-12 12:32
新播放器带歌词,我喜欢
这个基本成熟了,采用的是 div + css-doodle 的方式,还可以再加 css-doodle 以修饰帖子,如果有能力,也可以整合一下,单独使用一个 css-doodle 标签做帖 马黑黑 发表于 2023-5-12 12:34
这个基本成熟了,采用的是 div + css-doodle 的方式,还可以再加 css-doodle 以修饰帖子,如果有能力,也 ...
这个三合一的习惯用法我觉得很好。一个标签加歌词,抄作业可以,自己整么,不可能有能力。{:4_173:} 欣赏老师精美帖,收藏了,慢慢学习体会试用!谢谢老师教程!{:4_190:} 这个好,连播放器还有歌词都是css-doodle做的,被黑黑研究出来做歌词了{:4_199:} 以前黑黑还说这个弄不出来,现在终于弄出来了。辛苦了{:4_187:} @马黑黑有一个问题你这个帖子和套用的帖子都出现不能显示播放器按钮,只在画面上显示代码,是怎么回事? 奇怪了!现在又正常了{:4_203:} 谢谢老师的分享,欣赏问好! 欣赏,学习! 亦是金 发表于 2023-5-12 16:06
@马黑黑有一个问题你这个帖子和套用的帖子都出现不能显示播放器按钮,只在画面上显示代码,是怎么回事?
网络的问题, 引用css-doodle js文件引入失败。 马黑黑 发表于 2023-5-12 12:31
说明:
帖子使用两个 css-doodle,一个是播放器,一个是歌词,二者分离更便于在帖子布局。
右边的数字用剩余时长可以动起来。 欣赏老师佳作!{:4_204:} 焱鑫磊 发表于 2023-5-12 19:06
欣赏老师佳作!
晚上好 醉美水芙蓉 发表于 2023-5-12 17:30
差点错过了!
错不了,都在的 起个网名好难 发表于 2023-5-12 16:59
网络的问题, 引用css-doodle js文件引入失败。
这也正常,必要时刷新一下