1.视觉设计:
使用浅紫色(#9370DB)为主色调,搭配渐变背景
圆角设计元素和柔和阴影提升现代感
动态波纹效果增强播放视觉反馈
响应式布局适应不同屏幕尺寸
2.核心功能:
音乐播放/暂停控制
进度条显示和跳转功能
上一曲/下一曲切换
本地音乐文件上传
实时搜索过滤功能
播放列表管理
3.技术实现:
使用HTML5 Audio API处理音频
CSS动画实现波纹效果
Flex布局构建响应式界面
事件监听实现用户交互
使用FileReader处理本地文件上传
4.使用说明:
直接运行即可使用示例音乐
点击上传按钮添加本地音乐文件
使用搜索框快速定位歌曲
点击进度条任意位置跳转播放
鼠标悬停按钮有放大交互效果
5.效果展示:
6.代码再现:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Modern Music Player</title>
<!-- 引入字体图标 -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<style>
/* 基础重置和全局样式 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
/* 播放器容器样式 */
.player-container {
background: rgba(255, 255, 255, 0.95);
width: 100%;
max-width: 400px;
border-radius: 20px;
box-shadow: 0 15px 30px rgba(0,0,0,0.1);
padding: 30px;
backdrop-filter: blur(10px);
}
/* 播放器头部样式 */
.player-header {
display: flex;
justify-content: space-between;
margin-bottom: 25px;
}
/* 搜索框样式 */
.search-box {
background: #f5f5f5;
border: none;
padding: 12px 20px;
border-radius: 30px;
width: 70%;
font-size: 14px;
transition: all 0.3s;
}
.search-box:focus {
outline: none;
box-shadow: 0 0 10px rgba(147, 112, 219, 0.3);
background: white;
}
/* 上传按钮样式 */
.upload-btn {
background: #9370DB;
color: white;
border: none;
padding: 12px 20px;
border-radius: 30px;
cursor: pointer;
transition: transform 0.2s;
}
.upload-btn:hover {
transform: scale(1.05);
background: #7B68EE;
}
/* 专辑封面样式 */
.album-art {
width: 200px;
height: 200px;
border-radius: 15px;
margin: 0 auto 25px;
overflow: hidden;
position: relative;
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}
/* 动态波纹效果 */
.wave {
position: absolute;
bottom: 0;
width: 100%;
height: 40%;
background: linear-gradient(to top, rgba(147, 112, 219, 0.3), transparent);
animation: wave 1.5s infinite linear;
}
@keyframes wave {
0% { transform: translateX(-100%); }
100% { transform: translateX(100%); }
}
/* 播放控制按钮样式 */
.controls {
display: flex;
justify-content: center;
gap: 20px;
margin: 25px 0;
}
.control-btn {
background: none;
border: none;
cursor: pointer;
color: #9370DB;
font-size: 24px;
padding: 15px;
border-radius: 50%;
transition: all 0.3s;
}
.control-btn:hover {
background: rgba(147, 112, 219, 0.1);
transform: scale(1.1);
}
/* 进度条样式 */
.progress-container {
background: #eee;
height: 4px;
border-radius: 2px;
margin: 20px 0;
cursor: pointer;
}
.progress {
background: #9370DB;
height: 100%;
width: 0%;
border-radius: 2px;
transition: width 0.1s linear;
}
/* 歌曲列表样式 */
.playlist {
list-style: none;
max-height: 200px;
overflow-y: auto;
}
.song-item {
padding: 12px;
margin: 8px 0;
background: #f8f9fa;
border-radius: 10px;
cursor: pointer;
transition: all 0.3s;
}
.song-item:hover {
background: rgba(147, 112, 219, 0.1);
transform: translateX(5px);
}
/* 隐藏默认文件上传按钮 */
#upload-input {
display: none;
}
</style>
</head>
<body>
<div class="player-container">
<!-- 播放器头部 -->
<div class="player-header">
<input type="text" class="search-box" placeholder="搜索歌曲...">
<button class="upload-btn" onclick="document.getElementById('upload-input').click()">
<i class="fas fa-upload"></i> 上传
</button>
<input type="file" id="upload-input" accept="audio/*">
</div>
<!-- 专辑封面区域 -->
<div class="album-art">
<div class="wave"></div>
</div>
<!-- 播放控制 -->
<div class="controls">
<button class="control-btn" onclick="prevSong()"><i class="fas fa-step-backward"></i></button>
<button class="control-btn" id="play-btn" onclick="togglePlay()"><i class="fas fa-play"></i></button>
<button class="control-btn" onclick="nextSong()"><i class="fas fa-step-forward"></i></button>
</div>
<!-- 进度条 -->
<div class="progress-container" onclick="seek(event)">
<div class="progress" id="progress"></div>
</div>
<!-- 播放列表 -->
<ul class="playlist" id="playlist"></ul>
</div>
<script>
// 初始化音乐播放器
const audio = new Audio();
let currentSongIndex = 0;
let isPlaying = false;
// 示例音乐列表(可以替换或扩展)
const songs = [
{ title: '示例音乐1', src: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3' },
{ title: '示例音乐2', src: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3' }
];
// 初始化播放列表
function initPlaylist() {
const playlist = document.getElementById('playlist');
songs.forEach((song, index) => {
const li = document.createElement('li');
li.className = 'song-item';
li.textContent = song.title;
li.onclick = () => playSong(index);
playlist.appendChild(li);
});
}
// 播放/暂停切换
function togglePlay() {
if (isPlaying) {
audio.pause();
document.querySelector('.wave').style.display = 'none';
} else {
audio.play();
document.querySelector('.wave').style.display = 'block';
}
isPlaying = !isPlaying;
document.getElementById('play-btn').innerHTML = isPlaying ?
'<i class="fas fa-pause"></i>' : '<i class="fas fa-play"></i>';
}
// 更新进度条
audio.ontimeupdate = () => {
const progress = (audio.currentTime / audio.duration) * 100;
document.getElementById('progress').style.width = `${progress}%`;
};
// 点击进度条跳转播放位置
function seek(e) {
const width = e.target.clientWidth;
const clickX = e.offsetX;
audio.currentTime = (clickX / width) * audio.duration;
}
// 播放指定歌曲
function playSong(index) {
currentSongIndex = index;
audio.src = songs[index].src;
audio.play();
isPlaying = true;
document.getElementById('play-btn').innerHTML = '<i class="fas fa-pause"></i>';
document.querySelector('.wave').style.display = 'block';
}
// 下一首
function nextSong() {
currentSongIndex = (currentSongIndex + 1) % songs.length;
playSong(currentSongIndex);
}
// 上一首
function prevSong() {
currentSongIndex = (currentSongIndex - 1 + songs.length) % songs.length;
playSong(currentSongIndex);
}
// 文件上传处理
document.getElementById('upload-input').addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const url = URL.createObjectURL(file);
songs.push({ title: file.name, src: url });
const li = document.createElement('li');
li.className = 'song-item';
li.textContent = file.name;
li.onclick = () => playSong(songs.length - 1);
document.getElementById('playlist').appendChild(li);
}
});
// 搜索功能
document.querySelector('.search-box').addEventListener('input', function(e) {
const searchTerm = e.target.value.toLowerCase();
document.querySelectorAll('.song-item').forEach(item => {
const text = item.textContent.toLowerCase();
item.style.display = text.includes(searchTerm) ? 'block' : 'none';
});
});
// 初始化播放列表
initPlaylist();
</script>
</body>
</html>