版本一

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>案例:音乐播放器</title>
  8. <style>
  9. * {
  10. padding: 0;
  11. margin: 0;
  12. }
  13. ul {
  14. list-style: none;
  15. }
  16. ul li {
  17. margin: 20px 20px;
  18. padding: 10px 5px;
  19. border-radius: 3px;
  20. }
  21. ul li.active {
  22. background-color: #D2E2F3;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id='app'>
  28. <audio :src="currentSrc" controls autoplay @ended='handleEnded'></audio>
  29. <ul>
  30. <li :class='{active:index===currentIndex}' v-for='(item,index) in musicData' :key='item.id'
  31. @click='handleClick(item.songSrc,index)'>
  32. <h2>{{item.id}}-歌名:{{item.name}}</h2>
  33. <p>{{item.author}}</p>
  34. </li>
  35. </ul>
  36. <button @click='handleNext'>下一首</button>
  37. </div>
  38. <script src="./vue.js"></script>
  39. <script>
  40. const musicData = [{
  41. id: 1,
  42. name: '于荣光 - 少林英雄',
  43. author: '于荣光',
  44. songSrc: './static/于荣光 - 少林英雄.mp3'
  45. },
  46. {
  47. id: 2,
  48. name: 'Joel Adams - Please Dont Go',
  49. author: 'Joel Adams',
  50. songSrc: './static/Joel Adams - Please Dont Go.mp3'
  51. },
  52. {
  53. id: 3,
  54. name: 'MKJ - Time',
  55. author: 'MKJ',
  56. songSrc: './static/MKJ - Time.mp3'
  57. },
  58. {
  59. id: 4,
  60. name: 'Russ - Psycho (Pt. 2)',
  61. author: 'Russ',
  62. songSrc: './static/Russ - Psycho (Pt. 2).mp3'
  63. }
  64. ];
  65. new Vue({
  66. el: '#app',
  67. data: {
  68. musicData,
  69. currentSrc: './static/于荣光 - 少林英雄.mp3',
  70. currentIndex: 0
  71. },
  72. methods: {
  73. handleClick(src, index) {
  74. this.currentSrc = src;
  75. this.currentIndex = index;
  76. },
  77. handleEnded() {
  78. // // 下一首的播放
  79. // this.currentIndex++;
  80. // this.currentSrc = this.musicData[this.currentIndex].songSrc;
  81. this.handleNext();
  82. },
  83. handleNext() {
  84. this.currentIndex++;
  85. if (this.currentIndex === this.musicData.length) {
  86. this.currentIndex = 0;
  87. }
  88. this.currentSrc = this.musicData[this.currentIndex].songSrc
  89. }
  90. }
  91. })
  92. </script>
  93. </body>
  94. </html>

版本二

计算属性

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>案例:音乐播放器</title>
  8. <style>
  9. * {
  10. padding: 0;
  11. margin: 0;
  12. }
  13. ul {
  14. list-style: none;
  15. }
  16. ul li {
  17. margin: 20px 20px;
  18. padding: 10px 5px;
  19. border-radius: 3px;
  20. }
  21. ul li.active {
  22. background-color: #D2E2F3;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id='app'>
  28. <audio :src="getCurrentSongSrc" controls autoplay @ended='handleEnded'></audio>
  29. <ul>
  30. <li :class='{active:index===currentIndex}' v-for='(item,index) in musicData' :key='item.id'
  31. @click='handleClick(index)'>
  32. <h2>{{item.id}}-歌名:{{item.name}}</h2>
  33. <p>{{item.author}}</p>
  34. </li>
  35. </ul>
  36. <button @click='handleNext'>下一首</button>
  37. </div>
  38. <script src="./vue.js"></script>
  39. <script>
  40. const musicData = [{
  41. id: 1,
  42. name: '于荣光 - 少林英雄',
  43. author: '于荣光',
  44. songSrc: './static/于荣光 - 少林英雄.mp3'
  45. },
  46. {
  47. id: 2,
  48. name: 'Joel Adams - Please Dont Go',
  49. author: 'Joel Adams',
  50. songSrc: './static/Joel Adams - Please Dont Go.mp3'
  51. },
  52. {
  53. id: 3,
  54. name: 'MKJ - Time',
  55. author: 'MKJ',
  56. songSrc: './static/MKJ - Time.mp3'
  57. },
  58. {
  59. id: 4,
  60. name: 'Russ - Psycho (Pt. 2)',
  61. author: 'Russ',
  62. songSrc: './static/Russ - Psycho (Pt. 2).mp3'
  63. }
  64. ];
  65. new Vue({
  66. el: '#app',
  67. data: {
  68. musicData,
  69. currentIndex: 0
  70. },
  71. computed:{
  72. getCurrentSongSrc(){
  73. return this.musicData[this.currentIndex].songSrc;
  74. }
  75. },
  76. methods: {
  77. handleClick(index) {
  78. this.currentIndex = index;
  79. },
  80. handleEnded() {
  81. this.handleNext();
  82. },
  83. handleNext() {
  84. this.currentIndex++;
  85. if (this.currentIndex === this.musicData.length) {
  86. this.currentIndex = 0;
  87. }
  88. }
  89. }
  90. })
  91. </script>
  92. <!-- 18511803134 小马哥 -->
  93. <!-- vue组件 -->
  94. <!-- ajax axios ajax库 -->
  95. </body>
  96. </html>