利用AI写的悬浮音乐播放器

本文摘要音乐对接的api api可以随时自己换<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, in...

音乐对接的api api可以随时自己换

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>星宇悬浮音乐播放器</title>
<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* 悬浮播放器主容器 */
.music-float {
  position: fixed;
  left: 20px;
  bottom: 20px;
  z-index: 9999;
  width: 65px;
  height: 65px;
  border-radius: 100px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(25px);
  -webkit-backdrop-filter: blur(25px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2);
  transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1);
  cursor: pointer;
  animation: breathe 3.2s infinite ease-in-out;
  overflow: hidden;
}

/* 呼吸动画 */
@keyframes breathe {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.06); }
}

/* 展开状态 */
.music-float.expanded {
  width: 360px;
  height: 140px;
  border-radius: 24px;
  animation: none;
  background-size: cover;
  background-position: center;
}

/* 背景遮罩(毛玻璃+暗化) */
.music-float::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  z-index: 0;
  opacity: 0;
  transition: opacity 0.4s;
}
.music-float.expanded::before {
  opacity: 1;
}

/* 专辑封面 */
.cover {
  position: absolute;
  left: 8px;
  top: 8px;
  width: 49px;
  height: 49px;
  border-radius: 50%;
  overflow: hidden;
  z-index: 2;
  transition: all 0.4s;
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}
.music-float.expanded .cover {
  width: 76px;
  height: 76px;
  left: 16px;
  top: 32px;
  border-radius: 18px;
}
.cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  animation: rotate 20s linear infinite;
  animation-play-state: paused;
}
.music-float.playing .cover img {
  animation-play-state: running;
}
@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* 小播放按钮 */
.play-icon {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  font-size: 18px;
  z-index: 2;
}
.music-float.expanded .play-icon {
  opacity: 0;
  pointer-events: none;
}

/* 展开内容 */
.content {
  position: absolute;
  left: 110px;
  top: 22px;
  right: 20px;
  bottom: 20px;
  z-index: 2;
  color: #fff;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  opacity: 0;
  visibility: hidden;
  transition: all 0.4s;
  text-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.music-float.expanded .content {
  opacity: 1;
  visibility: visible;
}

/* 歌曲信息 */
.info {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.artist {
  font-size: 12px;
  opacity: 0.8;
  margin-left: 8px;
  font-weight: 400;
}

/* 进度条 */
.progress-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}
.time {
  font-size: 11px;
  opacity: 0.9;
  width: 40px;
  text-align: center;
}
.bar {
  flex: 1;
  height: 5px;
  background: rgba(255,255,255,0.25);
  border-radius: 10px;
  cursor: pointer;
  overflow: hidden;
}
.progress {
  height: 100%;
  width: 0%;
  background: #fff;
  border-radius: 10px;
  transition: width 0.1s linear;
}

/* 控制面板 */
.controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.btns {
  display: flex;
  gap: 12px;
}
.btn {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: rgba(255,255,255,0.1);
  border: none;
  color: #fff;
  font-size: 16px;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: 0.2s;
}
.btn:hover {
  background: rgba(255,255,255,0.25);
  transform: scale(1.08);
}

/* 音量 */
.volume {
  display: flex;
  align-items: center;
  gap: 8px;
}
.volume span {
  font-size: 14px;
}
.volume input {
  width: 70px;
  height: 5px;
  -webkit-appearance: none;
  background: rgba(255,255,255,0.25);
  border-radius: 10px;
}
.volume input::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 10px;
  height: 10px;
  background: #fff;
  border-radius: 50%;
}

audio { display: none; }
</style>
</head>
<body>

<div class="music-float" id="player">
  <div class="cover"><img id="coverImg" src=""></div>
  <div class="play-icon" id="miniPlay">▶</div>

  <div class="content">
    <div class="info">
      <span id="title"></span>
      <span class="artist" id="artist"></span>
    </div>
    <div class="progress-wrap">
      <span class="time" id="curr">0:00</span>
      <div class="bar" id="bar">
        <div class="progress" id="progress"></div>
      </div>
      <span class="time" id="dur">0:00</span>
    </div>
    <div class="controls">
      <div class="btns">
        <button class="btn" id="prev">⏮</button>
        <button class="btn" id="play">▶</button>
        <button class="btn" id="next">⏭</button>
      </div>
      <div class="volume">
        <span>🔊</span>
        <input type="range" id="vol" min="0" max="100" value="65">
      </div>
    </div>
  </div>
</div>

<audio id="audio" muted>

<script>
const p = document.getElementById('player');
const coverImg = document.getElementById('coverImg');
const title = document.getElementById('title');
const artist = document.getElementById('artist');
const audio = document.getElementById('audio');
const miniPlay = document.getElementById('miniPlay');
const playBtn = document.getElementById('play');
const bar = document.getElementById('bar');
const progress = document.getElementById('progress');
const curr = document.getElementById('curr');
const dur = document.getElementById('dur');
const vol = document.getElementById('vol');

// 格式化时间
function fmt(t) {
  let m = Math.floor(t / 60);
  let s = Math.floor(t % 60).toString().padStart(2, '0');
  return m + ':' + s;
}

// 播放状态同步
function setPlayState() {
  p.classList.add('playing');
  miniPlay.textContent = '⏸';
  playBtn.textContent = '⏸';
}
function setPauseState() {
  p.classList.remove('playing');
  miniPlay.textContent = '▶';
  playBtn.textContent = '▶';
}

// 加载并自动播放
async function loadMusic() {
  try {
    const res = await fetch('https://api.zxki.cn/api/wyyrg');
    const data = await res.json();
    if(data.code === 1 && data.data) {
      const d = data.data;
      title.textContent = d.name;
      artist.textContent = d.artistsname;
      coverImg.src = d.picurl;
      audio.src = d.url;
      p.style.backgroundImage = `url(${d.picurl})`;
      
      // 默认音量65%
      audio.volume = 0.65;
      vol.value = 65;

      // 解决浏览器自动播放限制:先静音播放再解除静音
      audio.muted = true;
      await audio.play();
      audio.muted = false;
      setPlayState();
    }
  } catch (e) {
    console.log('音乐加载失败',e);
  }
}

// 播放暂停切换
function togglePlay() {
  if(audio.paused){
    audio.play();
    setPlayState();
  }else{
    audio.pause();
    setPauseState();
  }
}

// 鼠标悬浮展开/移开收缩
p.addEventListener('mouseenter', () => p.classList.add('expanded'));
p.addEventListener('mouseleave', () => p.classList.remove('expanded'));

// 按钮事件
miniPlay.addEventListener('click', togglePlay);
playBtn.addEventListener('click', togglePlay);

// 进度更新
audio.addEventListener('timeupdate', () => {
  progress.style.width = (audio.currentTime / audio.duration * 100) + '%';
  curr.textContent = fmt(audio.currentTime);
});
audio.addEventListener('loadedmetadata', () => {
  dur.textContent = fmt(audio.duration);
});
bar.addEventListener('click', (e) => {
  audio.currentTime = e.offsetX / bar.clientWidth * audio.duration;
});

// 音量调节
vol.addEventListener('input', () => {
  audio.volume = vol.value / 100;
});

// 切歌重新加载
document.getElementById('next').addEventListener('click', loadMusic);
document.getElementById('prev').addEventListener('click', loadMusic);

// 歌曲结束
audio.addEventListener('ended', () => {
  setPauseState();
  progress.style.width = '0%';
  audio.currentTime = 0;
});

// 页面加载完毕立即执行自动播放
window.addEventListener('load', loadMusic);
</script>
</body>
</html>

效果一般般 有能力的自己优化一下

觉得内容不错?我要

评论 暂无评论
暂无评论,快来抢沙发吧~