|
|

楼主 |
发表于 2023-7-16 21:22
|
显示全部楼层
本帖最后由 马黑黑 于 2023-7-16 22:44 编辑
代码(已修正频谱在手机浏览器下颠倒的现象,但未能解决手机版下频谱的抖动问题)- <style>
- #mydiv {
- margin: 0 0 0 calc(50% - 593px);
- width: 1024px;
- height: 640px;
- border: 1px solid gray;
- overflow: hidden;
- position: relative;
- --state: paused;
- }
- .leaf {
- position: absolute;
- width: 50px;
- height: 50px;
- top: -100px;
- border-radius: 0% 100%;
- background: linear-gradient(45deg, green, lightgreen);
- animation: drop 6s var(--delay) infinite linear var(--state);
- --delay: 0s;
- }
- .leaf:nth-of-type(2) { --delay: -2s; }
- .leaf:nth-of-type(3) { --delay: -4s; }
- @keyframes drop { to { transform: rotate(-10deg) translateY(760px); } }
- </style>
- <div id="mydiv">
- <span class="leaf"></span>
- <span class="leaf"></span>
- <span class="leaf"></span>
- </div>
- <script>
- let mState = () => mydiv.style.setProperty('--state', aud.paused ? 'paused' : 'running');
- let msort = (ar) => {
- let newAr = [];
- ar.forEach((v,k) => k % 2 === 0 ? newAr.unshift(v) : newAr.push(v));
- return newAr;
- }
- class Pinpu {
- constructor(pa) {
- this.pa = pa;
- this.aud = document.createElement('audio');
- this.wrap = document.createElement('div');
- this.pinpu = { num: 60, width: 6, height: 100 }
- }
- createPinpu() {
- let styles = document.styleSheets;
- let mcss = document.styleSheets[styles.length - 1];
- mcss.insertRule(`.mplayer { position: absolute; left: 50%; bottom: 10px; transform: translate(-50%); display: flex; align-items: flex-end; justify-items: flex-start; cursor: pointer; }`, mcss.rules.length);
- mcss.insertRule(`@keyframes up { from { height: 0px; } to { height: var(--height); } }`, mcss.rules.length);
- this.wrap.className = 'mplayer';
- this.aud.id = 'aud';
- this.aud.autoplay = true;
- this.aud.loop = true;
- this.pa.appendChild(this.wrap);
- this.pa.appendChild(this.aud);
- Array.from({length: this.pinpu.num}).forEach((item,key) => {
- item = document.createElement('span');
- let ar = Array.from(Array(this.pinpu.num), (v,k) => this.pinpu.num - k -1);
- item.style.cssText = `
- margin-right: 2px;
- align-self: flex-end;
- width: ${this.pinpu.width}px;
- background: #${Math.random().toString(16).substr(-6)};
- animation: up ${0.3 + Math.random() * 0.3}s -${Math.random()}s infinite alternate linear var(--state);
- --height: ${this.pinpu.height / 2 + msort(ar)[key] * 1.2}px;
- `;
- this.wrap.appendChild(item);
- });
- this.click();
- }
- click() {
- this.wrap.onclick = () => this.aud.paused ? this.aud.play() : this.aud.pause();
- }
- }
- let pp = new Pinpu(mydiv);
- pp.pinpu.num = 80;
- pp.pinpu.width = 3;
- pp.pinpu.height = 100;
- pp.aud.src = 'https://music.163.com/song/media/outer/url?id=25707174';
- pp.createPinpu();
- aud.addEventListener('play', mState, false);
- aud.addEventListener('pause', mState, false);
- </script>
复制代码
|
|