马黑黑 发表于 2022-11-13 06:53

星座(双语歌词同步)

<style>
#papa { margin: 120px 0 0 calc(50% - 681px); display: grid; place-items: center; width: 1200px; height: 750px; background: #000 url('https://638183.freep.cn/638183/t22/webp/0b0.webp') no-repeat center/cover; box-shadow: 3px 3px 20px #000; perspective: 1000px; overflow: hidden; user-select: none; position: relative; z-index: 1; }
#mplayer { position: absolute; right: 136px; z-index: 1000; }
#btnwrap { display: block; fill: hsla(30, 20%, 80%, .9); cursor: pointer; }
#btnwrap:hover { fill: hsla(90, 100%, 100%, .9); }
#tmsg { fill: hsla(30, 100%, 90%, .9); stroke: none; font: bold 1em sans-serif; }
#lrc { --motion: cover1; --tt: 1s; --state: running; position: absolute; top: 10px; font: bold 2.4em sans-serif; color: hsl(100,100%,90%); -webkit-background-clip: text; filter: drop-shadow(1px 1px 2px hsla(0,100%,0%,.85)); z-index: 999; }
#lrc::before { position: absolute; content: attr(data-lrc); width: 20%; height: 100%; color: transparent; overflow: hidden; white-space: pre; background: linear-gradient(180deg,hsla(19, 48%, 22%,.35),hsla(19, 28%, 42%,.65)); filter: inherit; -webkit-background-clip: text; animation: var(--motion) var(--tt) linear forwards; animation-play-state: var(--state); }
#papa > span { position: absolute; right: 160px; opacity: .75; color: olive; font: bold 3em serif; display: grid; place-items: center; }
@keyframes cover1 { from { width: 0; } to { width: 100%; } }
@keyframes cover2 { from { width: 0; } to { width: 100%; } }
</style>

<div id="papa">
        <img src="https://638183.freep.cn/638183/t22/gif/0b01.gif" alt="" style="position: absolute; left: 0; top: 0; mix-blend-mode: screen;" />
        <svg id="mplayer" width="120" height="120">
                <g id="mama" transform="rotate(-90, 60, 60)" style="cursor: pointer;">
                        <circle id="track" cx="60" cy="60" r="50" fill="none" stroke-width="10" stroke="hsla(100, 20%, 80%, .65)" />
                        <circle id="prog" cx="60" cy="60" r="50" fill="none" stroke-width="10" stroke="hsla(100,50%,50%,.55)" />
                </g>
                <g id="btnwrap">
                        <path id="btnplay" d="M 50 50,50 70,70, 60 z"></path>
                        <path id="btnpause" d="M 52 50,52 70,57 70,57 50,52 50 z M 60 50,60 70,65 70,65 50,60 50 z" style="display: none;"></path>
                        <path d="M 57 50,60 50,60 70,57 70 z" fill="transparent" />
                </g>
                <path id="curPath" d="M 20 70 Q 60 0 100 70" fill="none" stroke="none"/>
                <path id="durPath" d="M 20 55 Q 60 110 100 55" fill="none" stroke="none"/>
                <g id="tmsg">
                        <text x="34" y="0"><textPath id="curMsg" xlink:href="#curPath" dominant-baseline="text-after-edge">00:00</textPath></text>
                        <text x="29" y="0"><textPath id="durMsg" xlink:href="#durPath" dominant-baseline="text-before-edge">00:00</textPath></text>
                </g>
        </svg>
        <div id="lrc" data-lrc="花潮论坛lrc在线">花潮论坛lrc在线</div>
</div>
<audio id="aud" src="http://www.kumeiwp.com/sub/filestores/2022/11/12/be5c581d8e61fb055f96dac4e419b549.mp3" loop autoplay></audio>

<script>
(function() {
        (function() {let total = 12, rr = 150;Array.from({length: total}).forEach((item,key) => {item = document.createElement('span');item.innerText = String.fromCharCode(9800+key);item.style.transform = `rotate(${360 / total * key}deg) translateY(${rr}px) rotate(-${360 / total * key}deg`;papa.appendChild(item);});})();
        let mKey = 0, mFlag = true, cc = { x: 1*track.getAttribute('cx'), y: 1*track.getAttribute('cy'), len: track.getTotalLength(),};
        let lrcAr = [,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,];
        prog.style.strokeDasharray = prog.style.strokeDashoffset =cc.len;
        btnwrap.onclick = () => aud.paused ? aud.play() : aud.pause();
        mama.onclick = (e) => {let deg = Math.atan2(e.offsetY - cc.y, e.offsetX - cc.x) * 180 / Math.PI;deg += (e.offsetX < cc.x && e.offsetY < cc.y) ? 450 : 90;aud.currentTime = aud.duration * deg / 360;};
        aud.addEventListener('pause', () => mState());
        aud.addEventListener('play', () => mState());
        aud.addEventListener('seeked', () => calcKey());
        aud.addEventListener('timeupdate', () => {prog.style.strokeDashoffset = cc.len - cc.len * aud.currentTime / aud.duration;curMsg.textContent = toMin(aud.currentTime);durMsg.textContent = toMin(aud.duration);for (j = 0; j < lrcAr.length; j++) {if (aud.currentTime >= lrcAr) {if (mKey === j) showLrc(lrcAr);else continue;}}});
        let showLrc = (time) => {let name = mFlag ? 'cover1' : 'cover2';lrc.innerHTML = lrcAr;lrc.dataset.lrc = lrcAr.replace(/<br>/,'\n');lrc.style.setProperty('--motion', name);lrc.style.setProperty('--tt', time + 's');lrc.style.setProperty('--state', 'running');mKey += 1;mFlag = !mFlag;};
        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 = () => aud.paused ? (btnplay.style.display = 'block', btnpause.style.display = 'none', lrc.style.setProperty('--state', 'paused')) : (btnplay.style.display = 'none', btnpause.style.display = 'block', lrc.style.setProperty('--state', '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;};
        papa.oncontextmenu = () => false;
})();
</script>

马黑黑 发表于 2022-11-13 07:08

关于双语同步:

一、歌词数组结构

let lrcAr = [
    <br>翻译歌词",4.3],
    //......
];

用折行标签 <br> 将两种语言的歌词隔开。

二、JS对歌词的处理 :歌词显示函数 showLrc() 的改动

let showLrc = (time) => {
        let name = mFlag ? 'cover1' : 'cover2';
        lrc.innerHTML = lrcAr;
        lrc.dataset.lrc = lrcAr.replace(/<br>/, '\n');
        lrc.style.setProperty('--motion', name);
        lrc.style.setProperty('--tt', time + 's');
        lrc.style.setProperty('--state', 'running');
        mKey += 1;
        mFlag = !mFlag;
};

变化仅红色语句,主要是处理伪元素的 attr 接收文本属性的问题,用 \n 替换 <br> ,“\n” 是换行符号。

三、CSS 中 lrc::before 的改变:

伪元素通过 attr 接收 \n 为换行符需要一个前提,就是 white-space 属性,之前我们设置为 nowrap,现在要改为 pre,就这一个地方的变化 :

white-space: pre;

马黑黑 发表于 2022-11-13 07:10

帖子的左上角是一幅小图

右边的星座,我开有转帖介绍: https://www.huachaowang.com/forum.php?mod=viewthread&tid=64553&extra=page%3D1

樵歌 发表于 2022-11-13 07:36

那菇凉脖子仰望成那个角度,你都忍心,真是不知道惜花{:4_189:}

红影 发表于 2022-11-13 07:54

折行的歌词也可以实现同步,真没想到。黑黑厉害{:4_199:}

红影 发表于 2022-11-13 07:55

把星座放在圆环歌词的外面,这个构思好{:4_199:}

亦是金 发表于 2022-11-13 08:35

谢谢黑黑老师教学,我也照葫芦画瓢,试着做一个!{:4_191:}

梦油 发表于 2022-11-13 09:29

黑黑先生总是有出奇制胜的手段和技巧。

马黑黑 发表于 2022-11-13 12:10

梦油 发表于 2022-11-13 09:29
黑黑先生总是有出奇制胜的手段和技巧。

思路人人有的

马黑黑 发表于 2022-11-13 12:13

樵歌 发表于 2022-11-13 07:36
那菇凉脖子仰望成那个角度,你都忍心,真是不知道惜花

这可是不得脖子病的绝招,你也学学

马黑黑 发表于 2022-11-13 12:15

红影 发表于 2022-11-13 07:54
折行的歌词也可以实现同步,真没想到。黑黑厉害

这个可以想得到的吧,只是之前懒得做

马黑黑 发表于 2022-11-13 12:15

亦是金 发表于 2022-11-13 08:35
谢谢黑黑老师教学,我也照葫芦画瓢,试着做一个!

{:4_190:}

小辣椒 发表于 2022-11-13 12:15

黑黑今天这个图图效果真美,美女含情脉脉

马黑黑 发表于 2022-11-13 12:16

红影 发表于 2022-11-13 07:55
把星座放在圆环歌词的外面,这个构思好

里面的圆形的是播放控制器

小辣椒 发表于 2022-11-13 12:16

这个双语我感觉音乐难找,可以汉语转双语吗

马黑黑 发表于 2022-11-13 12:17

小辣椒 发表于 2022-11-13 12:16
这个双语我感觉音乐难找,可以汉语转双语吗

你发外语歌才用到双语的,发中文歌没有必要

小辣椒 发表于 2022-11-13 12:19

马黑黑 发表于 2022-11-13 12:17
你发外语歌才用到双语的,发中文歌没有必要

这个我还得去找一下,看看有没有简单一点的{:4_170:}

马黑黑 发表于 2022-11-13 12:20

小辣椒 发表于 2022-11-13 12:19
这个我还得去找一下,看看有没有简单一点的

其实很多外语的歌词同步都有双语呢

红影 发表于 2022-11-13 13:46

马黑黑 发表于 2022-11-13 12:15
这个可以想得到的吧,只是之前懒得做

这个还是挺难的,没看到前肯定不会。

马黑黑 发表于 2022-11-13 13:47

红影 发表于 2022-11-13 13:46
这个还是挺难的,没看到前肯定不会。

原理不复杂
页: [1] 2 3 4 5 6 7
查看完整版本: 星座(双语歌词同步)