马黑黑 发表于 2022-8-28 07:02

慵懒的猫送红影

<style>
#papa { left: -214px; width: 1024px; height: 640px; background: gray url('/data/attachment/forum/202208/28/070015bjs2jahfnpaffay0.jpg') no-repeat center/cover;box-shadow: 3px 3px 20px #000; position: relative; z-index: 1; }
#player { padding: 10px; position: absolute; left: 20px; bottom: 20px; width: fit-content; height: fit-content; display: flex; gap: 10px; flex-direction: column; }
#lrctext { font: bold 1.4em sans-serif; color: silver; text-shadow: 1px 1px 2px #000; user-select: none; transition: all 1.5s; }
#btnwrap { width: fit-content; height: fit-content; display: flex; gap: 8px; align-items: center; }
#btnmain { width: 36px; height: 36px; display: grid; place-items: center; background: rgba(0,0,0,.5); border-radius: 50%; cursor: pointer; transition: all 2s; }
#btnmain:hover { background: orange; }
#btnplay {width: 16px; height: 16px; background: #ccc; clip-path: polygon(0 0, 0% 100%, 100% 50%); }
#btnpause { width: 2px; height: 20px; border-style: solid; border-width: 0px 4px; border-color: transparent #eee; display: none; }
#prgline { width: 200px; height: 4px; background: #ccc linear-gradient(to right,red,orange,green,red) no-repeat center left; background-size: 1px 4px; cursor: pointer;}
#tmsg { font: normal 16px sans-serif; color: orange; user-select: none; text-shadow: 1px 1px 1px #000; transition: 1.5s; }
#tmsg:hover, #lrctext:hover { color: tomato; }
</style>

<div id="papa">
        <div id="player">
                <div id="lrctext">lrc歌词</div>
                <div id="btnwrap">
                        <span id="btnmain"><span id="btnplay"></span><span id="btnpause"></span></span>
                        <span id="prgline"></span><span id="tmsg">00:00 | 00:00</span>
                </div>
        </div>
</div>

<script>
let lrcAr = [
        ['00.00', '纯音乐 - 慵懒的猫'],
        ['170.00','感谢欣赏']
];
let aud = new Audio();

aud.src = 'https://music.163.com/song/media/outer/url?id=1413361244.mp3';
aud.autoplay = true;
aud.loop = true;
btnmain.onclick = () => aud.paused ? aud.play() : aud.pause();
prgline.onclick = (e) => aud.currentTime = aud.duration * e.offsetX / prgline.offsetWidth;
aud.addEventListener('pause', () => btnstate());
aud.addEventListener('play',() => btnstate());
aud.addEventListener('timeupdate', () => {
        prgline.style.backgroundSize = prgline.offsetWidth * aud.currentTime / aud.duration + 'px 4px';
        tmsg.innerText = toMin(aud.duration) + ' | ' + toMin(aud.currentTime);
        for(j=0; j<lrcAr.length; j++) {
                if(aud.currentTime >= lrcAr) lrctext.innerText = lrcAr;
        }
});
let btnstate = () => aud.paused ? (btnplay.style.display = 'block', btnpause.style.display = 'none') : (btnplay.style.display = 'none', btnpause.style.display = 'block');
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>

马黑黑 发表于 2022-8-28 07:04

代码分享(全)
<style>
#papa { margin: auto; width: 1024px; height: 640px; background: gray url('/data/attachment/forum/202208/28/070015bjs2jahfnpaffay0.jpg') no-repeat center/cover;box-shadow: 3px 3px 20px #000; position: relative; z-index: 1; }
#player { padding: 10px; position: absolute; left: 20px; bottom: 20px; width: fit-content; height: fit-content; display: flex; gap: 10px; flex-direction: column; }
#lrctext { font: bold 1.4em sans-serif; color: silver; text-shadow: 1px 1px 2px #000; user-select: none; transition: all 1.5s; }
#btnwrap { width: fit-content; height: fit-content; display: flex; gap: 8px; align-items: center; }
#btnmain { width: 36px; height: 36px; display: grid; place-items: center; background: rgba(0,0,0,.5); border-radius: 50%; cursor: pointer; transition: all 2s; }
#btnmain:hover { background: orange; }
#btnplay {width: 16px; height: 16px; background: #ccc; clip-path: polygon(0 0, 0% 100%, 100% 50%); }
#btnpause { width: 2px; height: 20px; border-style: solid; border-width: 0px 4px; border-color: transparent #eee; display: none; }
#prgline { width: 200px; height: 4px; background: #ccc linear-gradient(to right,red,orange,green,red) no-repeat center left; background-size: 1px 4px; cursor: pointer;}
#tmsg { font: normal 16px sans-serif; color: orange; user-select: none; text-shadow: 1px 1px 1px #000; transition: 1.5s; }
#tmsg:hover, #lrctext:hover { color: tomato; }
</style>

<div id="papa">
        <div id="player">
                <div id="lrctext">lrc歌词</div>
                <div id="btnwrap">
                        <span id="btnmain"><span id="btnplay"></span><span id="btnpause"></span></span>
                        <span id="prgline"></span><span id="tmsg">00:00 | 00:00</span>
                </div>
        </div>
</div>

<script>
let lrcAr = [
        ['00.00', '纯音乐 - 慵懒的猫'],
        ['170.00','感谢欣赏']
];
let aud = new Audio();

aud.src = 'https://music.163.com/song/media/outer/url?id=1413361244.mp3';
aud.autoplay = true;
aud.loop = true;
btnmain.onclick = () => aud.paused ? aud.play() : aud.pause();
prgline.onclick = (e) => aud.currentTime = aud.duration * e.offsetX / prgline.offsetWidth;
aud.addEventListener('pause', () => btnstate());
aud.addEventListener('play',() => btnstate());
aud.addEventListener('timeupdate', () => {
        prgline.style.backgroundSize = prgline.offsetWidth * aud.currentTime / aud.duration + 'px 4px';
        tmsg.innerText = toMin(aud.duration) + ' | ' + toMin(aud.currentTime);
        for(j=0; j<lrcAr.length; j++) {
                if(aud.currentTime >= lrcAr) lrctext.innerText = lrcAr;
        }
});
let btnstate = () => aud.paused ? (btnplay.style.display = 'block', btnpause.style.display = 'none') : (btnplay.style.display = 'none', btnpause.style.display = 'block');
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>

马黑黑 发表于 2022-8-28 07:16

和N久以前做过的一样,本帖的播放器使用HTML+CSS绘制,不适用任何图片,所以CSS和HTML代码略微多一些些。

播放器进度显示与控制使用线性渐变背景实现,利用 backgroundSize 属性模拟进度,它是多彩的,随着播放进度的缓慢伸懒腰,伸到一定长度是会很漂亮。可以手动单击进度轨迹以改变当前播放进度。

这个播放器,如果不含歌词,也就是短短的20来行代码,做到了极其精简了。算法也恰到好处,几乎没有冗余的代码。

使用本帖代码,请确保播放器代码不受侵扰,亦即,如下这一部分的HTML代码,

      <div id="player">
                <div id="lrctext">lrc歌词</div>
                <div id="btnwrap">
                        <span id="btnmain"><span id="btnplay"></span><span id="btnpause"></span></span>
                        <span id="prgline"></span><span id="tmsg">00:00 | 00:00</span>
                </div>
      </div>

如非必要,代码间不要插入插入其他代码。帖子需要添加的元素代码,应放置在它们的外面、父框的里面,所添加的元素代码与播放器代码是平行或并列关系。

红影 发表于 2022-8-28 08:23

一进坛子,先看到有礼物,先美美地转一圈,再细看{:4_187:}

红影 发表于 2022-8-28 08:23

哇,彩虹进度条,太美了{:4_199:}

红影 发表于 2022-8-28 08:27

鼠标移动上去按钮的颜色变化和时间数字颜色一致,黑黑真细心{:4_199:}

朵拉 发表于 2022-8-28 08:28

棒棒哒制作,影宝收礼开心{:4_204:}

红影 发表于 2022-8-28 08:31

鼠标移动上去,连歌词也会变色的呢{:4_187:}

红影 发表于 2022-8-28 08:36

看那小猫的眼神,分明很目光炯炯,哪里慵懒了{:4_189:}

红影 发表于 2022-8-28 08:37

朵拉 发表于 2022-8-28 08:28
棒棒哒制作,影宝收礼开心

谢谢朵宝,这个播放器太美了,影子看到这个非常开心呢{:4_187:}

马黑黑 发表于 2022-8-28 08:47

红影 发表于 2022-8-28 08:36
看那小猫的眼神,分明很目光炯炯,哪里慵懒了

额,那是因为屏幕前是镁铝。俺面前,它眼睛都没睁开呢

马黑黑 发表于 2022-8-28 08:48

朵拉 发表于 2022-8-28 08:28
棒棒哒制作,影宝收礼开心

非常简洁的,如果你喜欢这个样纸,你也可以做做

加林森 发表于 2022-8-28 08:48

制作漂亮,小猫好玩。红影收礼开森!

马黑黑 发表于 2022-8-28 08:53

红影 发表于 2022-8-28 08:23
一进坛子,先看到有礼物,先美美地转一圈,再细看

大王叫你去巡山?{:4_170:}

马黑黑 发表于 2022-8-28 08:54

红影 发表于 2022-8-28 08:23
哇,彩虹进度条,太美了

不需要彩虹时,linear-gradient线性渐变需要两个相同的颜色,同理,需要什么样的彩虹颜色,可以定义。

朵拉 发表于 2022-8-28 08:56

马黑黑 发表于 2022-8-28 08:48
非常简洁的,如果你喜欢这个样纸,你也可以做做

正在制作,需要马师移步{:4_190:}

马黑黑 发表于 2022-8-28 09:12

朵拉 发表于 2022-8-28 08:56
正在制作,需要马师移步

看到了,很美,正在欣赏呢

马黑黑 发表于 2022-8-28 10:42

播放器是有 #player CSS选择器收拢,这一点可以从 HTML 代码结构瞧得出来。播放器的位置通过它调整:

#player { padding: 10px; position: absolute; left: 20px; bottom: 20px;

用于绝对定位(absolute)的元素,可以通过 left、right ↔ top、bottom 两两组合快速进行物理定位。比如上方的红色部分,仅将 bottom 改为 top,播放器就在帖子的左上角方向;或仅改 left 为 right ,播放器就位于右下角;若left和bottom同时修改为right和top,那就是右上角。

加林森 发表于 2022-8-28 10:54

马黑黑 发表于 2022-8-28 10:42
播放器是有 #player CSS选择器收拢,这一点可以从 HTML 代码结构瞧得出来。播放器的位置通过它调整:

#p ...

谢谢老黑了。{:4_190:}

醉美水芙蓉 发表于 2022-8-28 13:08

页: [1] 2 3 4 5
查看完整版本: 慵懒的猫送红影