永不消逝的电波
<style>#papa { left: -214px; width: 1024px; height: 640px; background: gray url('https://638183.freep.cn/638183/t22/hl/ewav.png') no-repeat center/cover; box-shadow: 3px 3px 20px #000; display:grid; place-items: center; user-select: none; overflow: hidden; position: relative; z-index: 1; }
#mplayer { position: absolute; bottom: 10px; width: fit-content; height: fit-content; display: flex; align-items: center; gap: 8px; z-index: 9; }
#btnwrap { position: relative; width: 36px; height: 36px; display: grid; place-items: center; border-radius: 50%; background: linear-gradient(to top right, lightblue, blue); cursor: pointer; }
#btnwrap:hover { background: #000 linear-gradient(to top right, snow, teal); }
#btnplay { width: 20px; height: 20px; 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; }
#prog { width: 300px; height: 2px; background: #ccc linear-gradient(to right,red,orange,blue,tomato) no-repeat; background-size: 1px 2px; cursor: pointer; position: relative;}
#prog::before { position: absolute; content: ''; top: -7px; width: inherit; height: 15px; }
#prog:hover::before { background: rgba(200,200,200,.15); }
#tmsg { font: normal 16px sans-serif; color: snow;}
#lrc { position: absolute; left: 20px; top: 20px; font: bold 1.5em sans-serif; color: skyblue; text-shadow: 2px 2px 2px #222; opacity: 1; animation: opa 1.5s infinite alternate; }
#wave { position: absolute; display: block; top: 0; }
@keyframes opa { to { opacity: .3;} }
</style>
<div id="papa">
<div id="mplayer"><span id="btnwrap"><span id="btnplay"></span><span id="btnpause"></span></span><span id="prog"></span><span id="tmsg">00:00 | 00:00</span></div>
<div id="lrc"></div>
<svg id="wave" width="600" height="600"> <!-- 模拟电波 -->
<circle cx="300" cy="300" r="0" fill="none" stroke="lightgreen" stroke-width="4">
<animate attributeName="r" begin="0s" from="0" to="290" dur="2s" repeatCount="indefinite"></animate>
<animate attributeName="stroke-opacity" begin="0s" from="0.5" to="0" dur="2s" repeatCount="indefinite"></animate>
</circle>
<circle cx="300" cy="300" r="0" fill="none" stroke="skyblue" stroke-width="3">
<animate attributeName="r" begin="0.5s" from="0" to="290" dur="2s" repeatCount="indefinite"></animate>
<animate attributeName="stroke-opacity" begin="0.5s" from="0.5" to="0" dur="2s" repeatCount="indefinite"></animate>
</circle>
<circle cx="300" cy="300" r="0" fill="none" stroke="lightblue" stroke-width="2">
<animate attributeName="r" begin="1s" from="0" to="290" dur="2s" repeatCount="indefinite"></animate>
<animate attributeName="stroke-opacity" begin="1s" from="0.5" to="0" dur="2s" repeatCount="indefinite"></animate>
</circle>
<circle cx="300" cy="300" r="0" fill="none" stroke="red" stroke-width="1">
<animate attributeName="r" begin="1.5s" from="0" to="290" dur="2s" repeatCount="indefinite"></animate>
<animate attributeName="stroke-opacity" begin="1.5s" from="0.5" to="0" dur="2s" repeatCount="indefinite"></animate>
</circle>
</svg>
</div>
<script>
let lrcAr = [
['0.00','永不消逝的电波'],
['240.00','感谢支持']
];
let aud = new Audio();
aud.src = 'https://music.163.com/song/media/outer/url?id=1860681142.mp3';
aud.autoplay = true;
aud.loop = true;
btnwrap.onclick = () => aud.paused ? aud.play() : aud.pause();
prog.onclick = (e) => aud.currentTime = aud.duration * e.offsetX / prog.offsetWidth;
aud.addEventListener('pause', () => btnstate());
aud.addEventListener('play',() => btnstate());
aud.addEventListener('timeupdate', () => {
prog.style.backgroundSize = prog.offsetWidth * aud.currentTime / aud.duration + 'px 2px';
tmsg.innerText = toMin(aud.duration) + ' | ' + toMin(aud.currentTime);
for(j=0; j<lrcAr.length; j++) {
if(aud.currentTime >= lrcAr) lrc.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>
代码:
<style>
#papa { left: -214px; width: 1024px; height: 640px; background: gray url('https://638183.freep.cn/638183/t22/hl/ewav.png') no-repeat center/cover; box-shadow: 3px 3px 20px #000; display:grid; place-items: center; user-select: none; overflow: hidden; position: relative; z-index: 1; }
#mplayer { position: absolute; bottom: 10px; width: fit-content; height: fit-content; display: flex; align-items: center; gap: 8px; z-index: 9; }
#btnwrap { position: relative; width: 36px; height: 36px; display: grid; place-items: center; border-radius: 50%; background: linear-gradient(to top right, lightblue, blue); cursor: pointer; }
#btnwrap:hover { background: #000 linear-gradient(to top right, snow, teal); }
#btnplay { width: 20px; height: 20px; 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; }
#prog { width: 300px; height: 2px; background: #ccc linear-gradient(to right,red,orange,blue,tomato) no-repeat; background-size: 1px 2px; cursor: pointer; position: relative;}
#prog::before { position: absolute; content: ''; top: -7px; width: inherit; height: 15px; }
#prog:hover::before { background: rgba(200,200,200,.15); }
#tmsg { font: normal 16px sans-serif; color: snow;}
#lrc { position: absolute; left: 20px; top: 20px; font: bold 1.5em sans-serif; color: skyblue; text-shadow: 2px 2px 2px #222; opacity: 1; animation: opa 1.5s infinite alternate; }
#wave { position: absolute; display: block; top: 0; }
@keyframes opa { to { opacity: .3;} }
</style>
<div id="papa">
<div id="mplayer"><span id="btnwrap"><span id="btnplay"></span><span id="btnpause"></span></span><span id="prog"></span><span id="tmsg">00:00 | 00:00</span></div>
<div id="lrc"></div>
<svg id="wave" width="600" height="600"> <!-- 模拟电波 -->
<circle cx="300" cy="300" r="0" fill="none" stroke="lightgreen" stroke-width="4">
<animate attributeName="r" begin="0s" from="0" to="290" dur="2s" repeatCount="indefinite"></animate>
<animate attributeName="stroke-opacity" begin="0s" from="0.5" to="0" dur="2s" repeatCount="indefinite"></animate>
</circle>
<circle cx="300" cy="300" r="0" fill="none" stroke="skyblue" stroke-width="3">
<animate attributeName="r" begin="0.5s" from="0" to="290" dur="2s" repeatCount="indefinite"></animate>
<animate attributeName="stroke-opacity" begin="0.5s" from="0.5" to="0" dur="2s" repeatCount="indefinite"></animate>
</circle>
<circle cx="300" cy="300" r="0" fill="none" stroke="lightblue" stroke-width="2">
<animate attributeName="r" begin="1s" from="0" to="290" dur="2s" repeatCount="indefinite"></animate>
<animate attributeName="stroke-opacity" begin="1s" from="0.5" to="0" dur="2s" repeatCount="indefinite"></animate>
</circle>
<circle cx="300" cy="300" r="0" fill="none" stroke="red" stroke-width="1">
<animate attributeName="r" begin="1.5s" from="0" to="290" dur="2s" repeatCount="indefinite"></animate>
<animate attributeName="stroke-opacity" begin="1.5s" from="0.5" to="0" dur="2s" repeatCount="indefinite"></animate>
</circle>
</svg>
</div>
<script>
let lrcAr = [
['0.00','永不消逝的电波'],
['240.00','感谢支持']
];
let aud = new Audio();
aud.src = 'https://music.163.com/song/media/outer/url?id=1860681142.mp3';
aud.autoplay = true;
aud.loop = true;
btnwrap.onclick = () => aud.paused ? aud.play() : aud.pause();
prog.onclick = (e) => aud.currentTime = aud.duration * e.offsetX / prog.offsetWidth;
aud.addEventListener('pause', () => btnstate());
aud.addEventListener('play',() => btnstate());
aud.addEventListener('timeupdate', () => {
prog.style.backgroundSize = prog.offsetWidth * aud.currentTime / aud.duration + 'px 2px';
tmsg.innerText = toMin(aud.duration) + ' | ' + toMin(aud.currentTime);
for(j=0; j<lrcAr.length; j++) {
if(aud.currentTime >= lrcAr) lrc.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>
帖子中间的电波图案,使用 svg 模拟,其实是画四个圆圈,通过 fill 和 stroke 的颜色设置弄成圆环。
每个圆环都加入两个 animate 动画,一个做从小到大的变化,另一个做透明度变化,各个圆环运行动画周期时长一样、开始运行时间各有间隔,从而组成帖子展现出来的样貌。这里仅以一个圆环做例子:
<!-- 模拟电波 -->
<svg id="wave" width="600" height="600">
<circle cx="300" cy="300" r="0" fill="none" stroke="lightgreen" stroke-width="4">
<animate attributeName="r" begin="0s" from="0" to="290" dur="2s" repeatCount="indefinite"></animate>
<animate attributeName="stroke-opacity" begin="0s" from="0.5" to="0" dur="2s" repeatCount="indefinite"></animate>
</circle>
<!-- 其他圆环代码略 -->
</svg>
两个动画都是 animate,一个管半径变化,一个管透明变化。
在 svg,animate 用于设置基于元素属性的动画。它需要给出元素要变化的属性名称 attributeName,变化描述 from ... to,变化周期时长 dur,变化的重复次数 repeatCount,还有其他的可选参数。如上面的代码案例,animate 以标签的形式放置于要运行动画的元素的起始和收尾标签代码中。circle 标签是自闭合标签,为了启用 animate 动画,变为 <circle> ... </circle> 的常规标签的闭合方式。
本帖仅使用一个 svg 用以模拟电波,其余的是常规的 HTML+CSS+JS 组合。
感谢老师每天分享精彩代码,欣赏问好!{:4_187:} 梦缘 发表于 2022-9-11 08:51
感谢老师每天分享精彩代码,欣赏问好!
{:4_190:} 原来第二个动画是透明度变化,我还去想位置变化,看了解说才明白{:4_173:} 这个电波效果真漂亮,用SVG的动画竟能做出这么炫的效果呢。文字也从原来的水平移动变成了明暗变化,非常相配,给黑黑点赞{:4_187:} 歌曲也选得好,和电波的节奏很相配呢{:4_187:} 非常喜欢的效果。队长还没上来,我先越权亮起来了{:4_173:} 红影 发表于 2022-9-11 09:25
原来第二个动画是透明度变化,我还去想位置变化,看了解说才明白
电波的变化无需改变位置,它变大变小就成,然后扩散到最大时消失 红影 发表于 2022-9-11 09:26
这个电波效果真漂亮,用SVG的动画竟能做出这么炫的效果呢。文字也从原来的水平移动变成了明暗变化,非常相 ...
文字是CSS关键帧动画,这个大家都能想到做到的 红影 发表于 2022-9-11 09:28
非常喜欢的效果。队长还没上来,我先越权亮起来了
这个效果应该还可以用来模拟涟漪 红影 发表于 2022-9-11 09:27
歌曲也选得好,和电波的节奏很相配呢
曲子是同名舞台剧还是电影的片尾曲吧 看你设置了蓝绿等电波颜色,也有红,为什么感觉不到红色呢? 黑黑又搞特务活动。。。{:5_117:} 红影 发表于 2022-9-11 09:44
看你设置了蓝绿等电波颜色,也有红,为什么感觉不到红色呢?
红色有阿,看不到是因为它细吧,还是因为你的显示器老旧了呢 东篱闲人 发表于 2022-9-11 09:46
黑黑又搞特务活动。。。
局势紧张,别人都弄俺们好久了,俺们也得弄弄 来学习老黑的教程。 马黑黑 发表于 2022-9-11 09:41
电波的变化无需改变位置,它变大变小就成,然后扩散到最大时消失
嗯嗯,这样会很自然。原来可以将两个动画套用在一起。 马黑黑 发表于 2022-9-11 09:41
文字是CSS关键帧动画,这个大家都能想到做到的
都会做,却不一定会想到这样做,黑黑这个搭配弄得好{:4_199:}