n/chit-chatu/mc838 · 2025-07-03
注册 登录
n/chit-chat · 2025-07-03 · 已编辑 · 847 次浏览
u/mc838钻石

分享个V1 哪吒面板添加音乐

我的小哪吒面板:点击打开链接

分享个哪吒面板添加音乐

[upvote]```

加载中...


# 响应老友要求再修改一版。
# 位置固定在浏览器左下角,靠左边缘,点击按钮展开播放器。

![image](upload://49OUhH6qt36XlM4wq0nR0YwHOdF.png)

[upvote]```
<style>
#music-container {
  position: fixed;
  left: 0;
  bottom: 20px;
  display: flex;
  align-items: center;
  background: #222;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 0 6px rgba(0,0,0,0.7);
  transition: width 0.3s ease;
  width: 18px;
  height: 50px; 
  z-index: 9999;
  font-family: "微软雅黑", Arial, sans-serif;
  font-size: 9px;
}

#music-container.expanded {
  width: 190px;
  height: 50px;
}

#toggle-btn {
  width: 18px;
  height: 50px;  
  background: #333;
  border: none;
  outline: none;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0;
  order: 2;
  flex-shrink: 0; 
  z-index: 10;    
}

#toggle-btn svg {
  fill: #fff;
  width: 11px;
  height: 11px;
  transition: transform 0.3s ease;
}

#toggle-btn.collapsed svg {
  transform: rotate(0deg);
}

#toggle-btn.expanded svg {
  transform: rotate(180deg);
}

#music-player {
  flex: 1 1 auto;  
  display: none;
  flex-direction: row;
  align-items: center;
  padding: 4px 6px;
  color: #eee;
  white-space: nowrap;
  order: 1;
  user-select: none;
  overflow: hidden; 
}

#music-container.expanded #music-player {
  display: flex;
}

#music-player button {
  background: #444;
  border: none;
  color: #eee;
  padding: 4px 5px; 
  margin: 0 2px;
  border-radius: 3px;
  cursor: pointer;
  height: 32px; 
  flex-shrink: 0;
}
#music-player button svg {
  width: 11px;
  height: 11px;
  fill: #eee;
}

#song-info {
  margin-left: 8px;
  max-width: 110px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 10px;
}
</style>

<div id="music-container" class="collapsed" aria-label="音乐播放器容器">
  <button id="toggle-btn" class="collapsed" title="展开播放器" aria-label="展开音乐播放器">
    <svg viewBox="0 0 24 24"><path d="M8 5l8 7-8 7z"/></svg>
  </button>
  <div id="music-player" aria-label="音乐播放器">
    <button id="prev" title="上一首" aria-label="上一首">
      <svg viewBox="0 0 24 24"><path d="M16 19V5l-11 7z"/></svg>
    </button>
    <button id="play-pause" title="播放" aria-label="播放/暂停">
      <svg id="play-icon" viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>
      <svg id="pause-icon" viewBox="0 0 24 24" style="display:none;">
        <rect x="6" y="5" width="4" height="14"/>
        <rect x="14" y="5" width="4" height="14"/>
      </svg>
    </button>
    <button id="next" title="下一首" aria-label="下一首">
      <svg viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>
    </button>
    <span id="song-info">加载中...</span>
  </div>
</div>

<script>
  const toggleBtn = document.getElementById('toggle-btn');
  const musicContainer = document.getElementById('music-container');

  toggleBtn.addEventListener('click', () => {
    const isExpanded = musicContainer.classList.toggle('expanded');
    if (isExpanded) {
      musicContainer.classList.remove('collapsed');
      toggleBtn.classList.remove('collapsed');
      toggleBtn.classList.add('expanded');
      toggleBtn.title = "收起播放器";
      toggleBtn.setAttribute('aria-label', '收起音乐播放器');
    } else {
      musicContainer.classList.remove('expanded');
      musicContainer.classList.add('collapsed');
      toggleBtn.classList.remove('expanded');
      toggleBtn.classList.add('collapsed');
      toggleBtn.title = "展开播放器";
      toggleBtn.setAttribute('aria-label', '展开音乐播放器');
    }
  });

  // 播放列表示例,这里只放了1首,替换为你的列表即可
  const songs = [
    {
      "title": "说好的幸福呢 - 周杰伦",
      "src": "https://music.gvrander.eu.org/music/%E8%AF%B4%E5%A5%BD%E7%9A%84%E5%B9%B8%E7%A6%8F%E5%91%A2-%E5%91%A8%E6%9D%B0%E4%BC%A6.flac"
    }
  ];

  let currentSongIndex = Math.floor(Math.random() * songs.length);
  let audio = null;
  let isPlaying = false;
  let hasInteracted = false;

  function updateButtonState() {
      document.getElementById('play-icon').style.display = isPlaying ? 'none' : 'block';
      document.getElementById('pause-icon').style.display = isPlaying ? 'block' : 'none';
      document.getElementById('play-pause').title = isPlaying ? '暂停' : '播放';
  }

  function updateSongInfo() {
      document.getElementById('song-info').textContent = songs[currentSongIndex]?.title || '未知音轨';
  }

  function initAudio() {
      if (!audio) {
          audio = new Audio();
          audio.preload = "auto";
          audio.addEventListener('ended', () => {
              let nextIndex;
              do {
                  nextIndex = Math.floor(Math.random() * songs.length);
              } while (nextIndex === currentSongIndex && songs.length > 1);
              currentSongIndex = nextIndex;
              loadAndPlay(true);
          });
          audio.addEventListener('play', () => { isPlaying = true; updateButtonState(); });
          audio.addEventListener('pause', () => { isPlaying = false; updateButtonState(); });
      }
  }

  async function loadAndPlay(autoplay = true) {
      try {
          initAudio();
          if (audio.src !== songs[currentSongIndex].src) {
              audio.src = songs[currentSongIndex].src;
              await new Promise((resolve, reject) => {
                  audio.addEventListener('loadedmetadata', resolve, { once: true });
                  audio.addEventListener('error', reject, { once: true });
              });
          }
          updateSongInfo();
          if (autoplay) await audio.play();
      } catch (err) {
          console.log("播放失败:", err);
          isPlaying = false;
          updateButtonState();
          document.getElementById('song-info').textContent = '播放失败,换首试试';
      }
  }

  async function togglePlayPause() {
      if (!audio) return;
      try {
          audio.paused ? await audio.play() : audio.pause();
      } catch (err) {
          console.log("播放控制失败:", err);
      }
  }

  function playOnFirstInteraction() {
      if (hasInteracted) return;
      hasInteracted = true;
      window.removeEventListener('click', playOnFirstInteraction);
      window.removeEventListener('keydown', playOnFirstInteraction);
      loadAndPlay(true);
  }

  window.addEventListener('click', playOnFirstInteraction, { once: true });
  window.addEventListener('keydown', playOnFirstInteraction, { once: true });

  window.addEventListener('load', () => {
      initAudio();
      updateSongInfo();
      updateButtonState();
  });

  document.getElementById("play-pause").addEventListener("click", togglePlayPause);
  document.getElementById("next").addEventListener("click", async () => {
      let nextIndex;
      do {
          nextIndex = Math.floor(Math.random() * songs.length);
      } while (nextIndex === currentSongIndex && songs.length > 1);
      currentSongIndex = nextIndex;
      await loadAndPlay(true);
  });
  document.getElementById("prev").addEventListener("click", async () => {
      let prevIndex;
      do {
          prevIndex = Math.floor(Math.random() * songs.length);
      } while (prevIndex === currentSongIndex && songs.length > 1);
      currentSongIndex = prevIndex;
      await loadAndPlay(true);
  });
</script>
```[/upvote]
6
32
登录后参与讨论。
u/udc2b6nr 广告
QuicksilverVPN

快银 VPN,一键连接全球节点,线路稳定、延迟低。支持 Windows、macOS 等多平台,TUN 全局模式覆盖全部应用流量。无需复杂配置,下载即用。

umami.dnsl.top 了解更多

32 条回复

排序
  1. jay0529 黄金 2025-07-04

    我天!我在上班打开的,是个大光腚 :xhj43:image

    3
  2. mc838 钻石 楼主 2025-07-04

    就问你爱不爱看

    2
  3. houmusic 铂金 2025-07-04

    图爱看

    3
  4. mokl 黄金 2025-07-03

    正好需要,感谢

    2
  5. AshleyPatricia 黄金 不离不弃 2025-07-04

    看看呢

    2
  6. code001 黄金 3 YEARS 2025-07-04

    看看

    2
  7. zm_z 青铜 10 个月前

    看看

    2
  8. acwea904 黄金 3 YEARS 10 个月前

    看看

    2
  9. pnsroc 白银 2025-07-03

    看看好用不,支持一下

    1
  10. 9527kkk 💬 铂金 3 YEARS 2025-07-03

    学习一下

    1
  11. lovoup 钻石 3 YEARS 2025-07-04

    学习一下

    2
  12. berrice 黄金 不离不弃 2025-07-04

    看看2

    1
  13. xiwu 🚗 黄金 2 YEARS 2025-07-04

    学习一下

    1
  14. kissbye 钻石 2025-07-04

    操作这么骚的吗

    1
  15. is-unlock 黄金 10 个月前

    看一看

    1
  16. xing116166 白银 2 YEARS 10 个月前

    哪吒是个好东西

    1
  17. kevin666 黄金 10 个月前

    学习一下

    1
  18. changtom 黄金 3 YEARS 10 个月前

    背景图真漂亮

    1
  19. qwe999 黄金 10 个月前

    看看

    1
  20. joysuff 白银 3 YEARS 10 个月前

    看看

    1
  21. denkun 白银 10 个月前

    太帅了

    1
  22. M2026 💬 铂金 2 YEARS 10 个月前

    学习一下!谢谢分享!

    1
  23. syshxt 黄金 10 个月前

    嗯,不错,我的面板也搞上了

    1
  24. owwo 白银 10 个月前

    看看好看吗

    1
  25. FrankWkd 黄金 10 个月前

    这个不错哇

    1
  26. Ayiw_Stm 白银 7 个月前

    来看看

    1
  27. Raymond 青铜 7 个月前

    学习一下

    0
  28. huangsiting 黄金 7 个月前

    看看

    0
  29. ncpd 黄金 7 个月前

    支持这个好

    0
  30. alexs 黄金 7 个月前

    支持

    0
  31. demop 黄金 7 个月前

    音不音乐不重要,主要是图片太诱人了

    0
  32. wxk 💯 钻石 7 个月前

    正好需要,感谢

    0