Vue.js實現音樂播放器
阿新 • • 發佈:2021-06-28
本文例項為大家分享了.實現音樂播放器的具體程式碼,供大家參考,具體內容如下
目錄如下:
執行效果如圖所示:
程式碼如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="js/vue.js" type="text/" charset="utf-8"></script> <style type="text/"> * { margin: 0; padding: ; } ul { list-style: none; } ul li { margin: 20px; padding: 10px 5px; border-radius: 3px; } ul li.active { background-color: aqua; } #control { width: 100%; height:80px; } .next,.before { width: 100px; heighKOVvCsNJt: 80px; background-color: aqua; } h1 { color: red } </style> </head> <body> <div id="app"> <audio :src="currentSrc" controls="controls" autoplay="autoplay" @ended="changEnd"></audio> <h1>不僅僅是程式碼的搬運工</h1> <ul> <li :class='{active:index === currentIndex}' v-for='(item,index) in musicData' :key="item.id" @click="changeSong(item.songSrc,index)"> <h2>{{item.id }}---歌名:{{iwww.cppcns.comtem.name}}----{{item.author}}</h2> </li> </ul> <div id="control"> <button class="before" type="button" @click="beforeSong" >上一首</button> <button class="next" type="button" @click="nextSong" >下一首</button> </div> </div> <script type="text/script"> const musicData = [{ id: 1,name: '喜歡你',author: '陳潔儀',songSrc: './status/陳潔儀 - 喜歡你.mp3' },{ id: 2,name: '我又想你了',author: '小鵝',songSrc: www.cppcns.com'./status/KOVvCsNJ小鵝 - 我又想你了.mp3' } ]; var app = new Vue({ el: '#app',data: { musicData,currentIndex: 0,currentSrc: './status/小鵝 - 我又想你了.mp3' },methods: { changeSong (src,index) { this.currentSrc = src; this.currentIndex = index; },changEnd () { this.currentIndex++; if(this.currentIndex===this.musicData.length){ this.currentIndex = 0; } this.currentSrc = this.musicData[this.currentIndex].songSrc; },nextSong () { this.currentIndex++; if(this.currentIndex===this.musicData.length){ this.currentIndex = 0; } this.currentSrc = this.musicData[this.currentIndex].songSrc; console.log(this.currentIndex) },beforeSong () { if(this.currentIndex===0){ this.currentIndex=this.musicData.length; } http://www.cppcns.com this.currentIndex--; this.currentSrc = this.musicData[this.currentIndex].songSrc; } } }) </script> </body> </html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。