💛 圆形封面悬浮音乐播放器
这是一个示例网页内容区域。左侧悬浮的音乐播放器默认是隐藏状态,点击箭头可以展开。播放器功能包括:圆形专辑封面、播放列表、播放进度控制、时间显示等。
网页加载后,音乐将自动播放(需浏览器允许自动播放)。

代码如下
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>圆形封面悬浮音乐播放器</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
min-height: 100vh;
padding: 20px;
color: #fff;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
h1 {
text-align: center;
margin-bottom: 30px;
text-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
}
.content {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 15px;
padding: 30px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}
/* 播放器样式 */
.music-player {
position: fixed;
left: 0;
top: 50%;
transform: translateY(-50%);
width: 80px;
height: auto;
background: rgba(25, 25, 35, 0.95);
border-radius: 0 15px 15px 0;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
z-index: 1000;
transition: all 0.4s ease;
overflow: hidden;
}
.music-player.expanded {
width: 320px;
}
.toggle-btn {
position: absolute;
right: 3px;
top: 28px;
background: none;
border: none;
color: #fff;
font-size: 20px;
cursor: pointer;
z-index: 10;
transition: transform 0.3s ease;
}
.music-player.expanded .toggle-btn {
transform: rotate(180deg);
}
.player-content {
padding: 20px;
opacity: 0;
transition: opacity 0.3s ease;
height: 0;
overflow: hidden;
}
.music-player.expanded .player-content {
opacity: 1;
height: auto;
overflow: visible;
}
/* 圆形专辑封面 */
.album-cover {
width: 200px;
height: 200px;
margin: 0 auto 20px;
border-radius: 50%;
overflow: hidden;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
border: 5px solid rgba(255, 255, 255, 0.1);
animation: rotateCover 20s linear infinite;
animation-play-state: paused;
}
.music-playing .album-cover {
animation-play-state: running;
}
@keyframes rotateCover {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.album-cover img {
width: 100%;
height: 100%;
object-fit: cover;
}
.song-info {
text-align: center;
margin-bottom: 20px;
}
.song-title {
font-size: 18px;
font-weight: 600;
margin-bottom: 5px;
}
.song-artist {
font-size: 14px;
color: #aaa;
}
.progress-container {
margin: 20px 0;
}
.progress-bar {
width: 100%;
height: 5px;
background: rgba(255, 255, 255, 0.2);
border-radius: 5px;
overflow: hidden;
cursor: pointer;
}
.progress {
height: 100%;
background: #4a9eff;
width: 30%;
border-radius: 5px;
transition: width 0.1s ease;
}
.time-info {
display: flex;
justify-content: space-between;
font-size: 12px;
color: #ccc;
margin-top: 5px;
}
.controls {
display: flex;
justify-content: center;
align-items: center;
gap: 15px;
margin: 20px 0;
}
.control-btn {
background: none;
border: none;
color: #fff;
font-size: 18px;
cursor: pointer;
transition: color 0.3s ease;
}
.control-btn:hover {
color: #4a9eff;
}
.play-pause {
width: 50px;
height: 50px;
border-radius: 50%;
-background: #4a9eff;
display: flex;
align-items: center;
justify-content: center;
font-size: 20px;
}
.playlist {
margin-top: 20px;
max-height: 200px;
overflow-y: auto;
}
.playlist-header {
font-size: 16px;
margin-bottom: 10px;
padding-bottom: 5px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.playlist-item {
display: flex;
align-items: center;
padding: 8px 5px;
border-radius: 5px;
cursor: pointer;
transition: background 0.2s ease;
}
.playlist-item:hover {
background: rgba(255, 255, 255, 0.1);
}
.playlist-item.active {
background: rgba(74, 158, 255, 0.2);
}
.playlist-item img {
width: 40px;
height: 40px;
border-radius: 50%;
margin-right: 10px;
object-fit: cover;
}
.playlist-item-info {
flex: 1;
}
.playlist-item-title {
font-size: 14px;
margin-bottom: 2px;
}
.playlist-item-artist {
font-size: 12px;
color: #aaa;
}
.playlist-item-duration {
font-size: 12px;
color: #aaa;
}
/* 隐藏状态下的迷你播放器 */
.mini-player {
display: flex;
flex-direction: column;
align-items: center;
padding: 15px 10px;
}
.mini-cover {
width: 50px;
height: 50px;
border-radius: 50%;
overflow: hidden;
margin-bottom: 10px;
animation: rotateCover 20s linear infinite;
animation-play-state: paused;
}
.music-playing .mini-cover {
animation-play-state: running;
}
.mini-cover img {
width: 100%;
height: 100%;
object-fit: cover;
}
.mini-controls {
display: flex;
gap: 10px;
}
.mini-controls button {
background: none;
border: none;
color: #fff;
font-size: 16px;
cursor: pointer;
}
/* 滚动条样式 */
.playlist::-webkit-scrollbar {
width: 5px;
}
.playlist::-webkit-scrollbar-track {
background: rgba(255, 255, 255, 0.1);
border-radius: 5px;
}
.playlist::-webkit-scrollbar-thumb {
background: rgba(255, 255, 255, 0.3);
border-radius: 5px;
}
/* 响应式设计 */
@media (max-width: 768px) {
.music-player.expanded {
width: 280px;
}
.album-cover {
width: 150px;
height: 150px;
}
}
@media (max-width: 480px) {
.music-player.expanded {
width: 100%;
height: 100vh;
top: 0;
left: 0;
transform: none;
border-radius: 0;
}
.toggle-btn {
top: 20px;
right: 20px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>网页内容区域</h1>
<div class="content">
<p>这是一个示例网页内容区域。左侧悬浮的音乐播放器默认是隐藏状态,点击箭头可以展开。</p>
<p>播放器功能包括:圆形专辑封面、播放列表、播放进度控制、时间显示等。</p>
<p>网页加载后,音乐将自动播放(需浏览器允许自动播放)。</p>
</div>
</div>
<!-- 音乐播放器 - 默认隐藏 -->
<div class="music-player">
<button class="toggle-btn">
<i class="fas fa-chevron-right"></i>
</button>
<!-- 迷你播放器(隐藏状态显示) -->
<div class="mini-player">
<div class="mini-cover">
<img src="https://picsum.photos/id/1025/200/200" alt="专辑封面">
</div>
<div class="mini-controls">
<button class="mini-play-pause">
<i class="fas fa-play"></i>
</button>
<button class="mini-next">
<i class="fas fa-step-forward"></i>
</button>
</div>
</div>
<!-- 完整播放器(展开状态显示) -->
<div class="player-content">
<div class="album-cover">
<img src="https://picsum.photos/id/1025/400/400" alt="专辑封面">
</div>
<div class="song-info">
<div class="song-title">带你去旅行</div>
<div class="song-artist">校长</div>
</div>
<div class="progress-container">
<div class="progress-bar">
<div class="progress"></div>
</div>
<div class="time-info">
<span class="current-time">01:30</span>
<span class="total-time">03:45</span>
</div>
</div>
<div class="controls">
<button class="control-btn shuffle">
<i class="fas fa-random"></i>
</button>
<button class="control-btn prev">
<i class="fas fa-step-backward"></i>
</button>
<button class="control-btn play-pause">
<i class="fas fa-pause"></i>
</button>
<button class="control-btn next">
<i class="fas fa-step-forward"></i>
</button>
<button class="control-btn repeat">
<i class="fas fa-redo"></i>
</button>
</div>
<div class="playlist">
<div class="playlist-header">播放列表</div>
<div class="playlist-item active">
<img src="https://picsum.photos/id/1025/100/100" alt="封面">
<div class="playlist-item-info">
<div class="playlist-item-title">酒干倘卖无DJ</div>
<div class="playlist-item-artist">网络</div>
</div>
<div class="playlist-item-duration">04:45</div>
</div>
<div class="playlist-item">
<img src="https://picsum.photos/id/1035/100/100" alt="封面">
<div class="playlist-item-info">
<div class="playlist-item-title"> 骗子动嘴傻子动心 (DJ版)</div>
<div class="playlist-item-artist">大美</div>
</div>
<div class="playlist-item-duration">03:00</div>
</div>
<div class="playlist-item">
<img src="https://picsum.photos/id/1040/100/100" alt="封面">
<div class="playlist-item-info">
<div class="playlist-item-title">爱情诺曼底dj</div>
<div class="playlist-item-artist">张小易</div>
</div>
<div class="playlist-item-duration">05:11</div>
</div>
<div class="playlist-item">
<img src="https://picsum.photos/id/1043/100/100" alt="封面">
<div class="playlist-item-info">
<div class="playlist-item-title">谢谢你的爱(李彤儿)</div>
<div class="playlist-item-artist">李彤儿</div>
</div>
<div class="playlist-item-duration">05:16</div>
</div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const musicPlayer = document.querySelector('.music-player');
const toggleBtn = document.querySelector('.toggle-btn');
const playPauseBtn = document.querySelector('.play-pause');
const miniPlayPauseBtn = document.querySelector('.mini-play-pause');
const progressBar = document.querySelector('.progress-bar');
const progress = document.querySelector('.progress');
const currentTimeEl = document.querySelector('.current-time');
const totalTimeEl = document.querySelector('.total-time');
const playlistItems = document.querySelectorAll('.playlist-item');
const audio = new Audio();
// 模拟音频数据
const songs = [
{
title: "酒干倘卖无DJ",
artist: "网络",
src: "https://pan.5blog.cn/view.php/21367832cf5f5aba290cd3a4c9abdcdb.mp3",
cover: "https://picsum.photos/id/1025/400/400",
duration: "04:45"
},
{
title: " 骗子动嘴傻子动心 (DJ版)",
artist: "大美",
src: "https://pan.5blog.cn/view.php/19db7cd9c8f44770d0c1fd293b266a23.mp3",
cover: "https://picsum.photos/id/1035/400/400",
duration: "03:00"
},
{
title: "爱情诺曼底dj-张小易",
artist: "张小易",
src: "https://pan.5blog.cn/view.php/766beaf43337954eb0029be7e201a23d.mp3",
cover: "https://picsum.photos/id/1040/400/400",
duration: "05:11"
},
{
title: "谢谢你的爱(李彤儿)",
artist: "李彤儿",
src: "https://pan.5blog.cn/view.php/b1d82a2184c9b2ad042d9894ce430c55.mp3",
cover: "https://picsum.photos/id/1043/400/400",
duration: "04:16"
}
];
let currentSongIndex = 0;
let isPlaying = false;
// 初始化播放器
function initPlayer() {
loadSong(currentSongIndex);
// 尝试自动播放
setTimeout(() => {
audio.play().then(() => {
isPlaying = true;
updatePlayPauseButton();
musicPlayer.classList.add('music-playing');
}).catch(error => {
console.log("自动播放被阻止,需要用户交互");
isPlaying = false;
updatePlayPauseButton();
});
}, 1000);
}
// 加载歌曲
function loadSong(index) {
const song = songs[index];
// 更新音频源
audio.src = song.src;
// 更新界面
document.querySelector('.song-title').textContent = song.title;
document.querySelector('.song-artist').textContent = song.artist;
document.querySelector('.album-cover img').src = song.cover;
document.querySelector('.mini-cover img').src = song.cover;
totalTimeEl.textContent = song.duration;
// 更新播放列表活动项
playlistItems.forEach((item, i) => {
item.classList.toggle('active', i === index);
});
// 音频加载完成后的回调
audio.addEventListener('loadedmetadata', () => {
// 可以在这里获取真实的音频时长
// totalTimeEl.textContent = formatTime(audio.duration);
});
}
// 切换播放/暂停
function togglePlayPause() {
if (isPlaying) {
audio.pause();
musicPlayer.classList.remove('music-playing');
} else {
audio.play();
musicPlayer.classList.add('music-playing');
}
isPlaying = !isPlaying;
updatePlayPauseButton();
}
// 更新播放/暂停按钮
function updatePlayPauseButton() {
const icon = isPlaying ? 'fa-pause' : 'fa-play';
playPauseBtn.innerHTML = `<i class="fas ${icon}"></i>`;
miniPlayPauseBtn.innerHTML = `<i class="fas ${icon}"></i>`;
}
// 切换播放器展开/隐藏状态
toggleBtn.addEventListener('click', function() {
musicPlayer.classList.toggle('expanded');
// 更新箭头方向
if (musicPlayer.classList.contains('expanded')) {
toggleBtn.innerHTML = '<i class="fas fa-chevron-left"></i>';
} else {
toggleBtn.innerHTML = '<i class="fas fa-chevron-right"></i>';
}
});
// 播放/暂停按钮事件
playPauseBtn.addEventListener('click', togglePlayPause);
miniPlayPauseBtn.addEventListener('click', togglePlayPause);
// 下一首
document.querySelector('.next').addEventListener('click', function() {
currentSongIndex = (currentSongIndex + 1) % songs.length;
loadSong(currentSongIndex);
if (isPlaying) {
audio.play();
}
});
// 上一首
document.querySelector('.prev').addEventListener('click', function() {
currentSongIndex = (currentSongIndex - 1 + songs.length) % songs.length;
loadSong(currentSongIndex);
if (isPlaying) {
audio.play();
}
});
// 迷你播放器下一首
document.querySelector('.mini-next').addEventListener('click', function() {
currentSongIndex = (currentSongIndex + 1) % songs.length;
loadSong(currentSongIndex);
if (isPlaying) {
audio.play();
}
});
// 点击播放列表项切换歌曲
playlistItems.forEach((item, index) => {
item.addEventListener('click', function() {
currentSongIndex = index;
loadSong(currentSongIndex);
if (isPlaying) {
audio.play();
}
});
});
// 更新进度条
audio.addEventListener('timeupdate', function() {
const currentTime = audio.currentTime;
const duration = audio.duration;
if (duration) {
const progressPercent = (currentTime / duration) * 100;
progress.style.width = `${progressPercent}%`;
currentTimeEl.textContent = formatTime(currentTime);
}
});
// 点击进度条跳转
progressBar.addEventListener('click', function(e) {
const width = this.clientWidth;
const clickX = e.offsetX;
const duration = audio.duration;
if (duration) {
audio.currentTime = (clickX / width) * duration;
}
});
// 音频播放结束,自动下一首
audio.addEventListener('ended', function() {
currentSongIndex = (currentSongIndex + 1) % songs.length;
loadSong(currentSongIndex);
audio.play();
});
// 时间格式化函数
function formatTime(seconds) {
if (isNaN(seconds)) return "00:00";
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}
// 初始化播放器
initPlayer();
});
</script>
</body>
</html>
演示效果
https://pan.5blog.cn/view.php/524d0d9691e114acfac8c7f7e900f0b1.html
阅读:131
发布时间: