1 首先建一个文件夹 放几首歌曲

2 看代码

1)基本版本

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>音乐播放器</title>
<meta name="viewport" content="width=device-width ,initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<style>
* {
padding: 0;
margin: 0;
} ul {
list-style: none;
} ul li {
margin: 20px 20px;
padding: 10px 5px;
border-radius: 3px;
} ul li.active {
background-color: gray;
}
</style>
</head>
<body> <div id="app">
        <!--handerEnded这首歌播放完就波下一曲-->
    <audio v-bind:src="currentSrc" controls autoplay @ended="handerEnded"></audio>
<ul>
<li :class="{active:index===currentIndex}" v-for="(item, index) in musicData" :key="item.id"
@click="handerClick(item.songSrc,index)">
<h2>{{item.id}}-歌名:{{item.name}}</h2>
<p>{{item.author}}</p>
</li>
</ul>
<button @click="nextSong">下一曲</button>
</div> <script>
const musicData = [{
id: 1,
name: '数到五答应我 - 曹格',
author: '曹格',
songSrc: './static/数到五答应我 - 曹格.mp3'
},
{
id: 2,
name: '风吹着我向你跑来 - 焦迈奇',
author: '焦迈奇',
songSrc: './static/风吹着我向你跑来 - 焦迈奇.mp3'
}
]; let app = new Vue({
el: '#app',
data: {
musicData: musicData, // 可以写成 因为是一样的 musicData
currentSrc: './static/数到五答应我 - 曹格.mp3',
currentIndex: 0
},
methods: {
handerClick(src, index) {
// 更改src意味着 currentSrc = 你点的那个li标签里的歌曲的src
// 可以把songcrc传进去 this.currentSrc = src;
this.currentIndex = index
},
handerEnded() {
// 下一曲的播放 要用 ended
// 1. 索引 + 1
// this.currentIndex++;
// // 2. 找到索引的src赋值给原来的src
// if (this.currentIndex === this.musicData.length) {
// this.currentIndex = 0
// }
// this.currentSrc = this.musicData[this.currentIndex].songSrc;
this.nextSong();
},
nextSong(){
this.currentIndex++;
// 2. 找到索引的src赋值给原来的src
if (this.currentIndex === this.musicData.length) {
this.currentIndex = 0
}
this.currentSrc = this.musicData[this.currentIndex].songSrc;
}
}
}); </script>
</body>
</html>

2)计算属性版本

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>音乐播放器</title>
<meta name="viewport" content="width=device-width ,initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<style>
* {
padding: 0;
margin: 0;
} ul {
list-style: none;
} ul li {
margin: 20px 20px;
padding: 10px 5px;
border-radius: 3px;
} ul li.active {
background-color: gray;
}
</style>
</head>
<body> <div id="app"> <audio v-bind:src="getCurrentSongSrc" controls autoplay @ended="handerEnded"></audio>
<ul>
<li :class="{active:index===currentIndex}" v-for="(item, index) in musicData" :key="item.id"
@click="handerClick(index)">
<h2>{{item.id}}-歌名:{{item.name}}</h2>
<p>{{item.author}}</p>
</li>
</ul>
<button @click="nextSong">下一曲</button>
</div> <script>
const musicData = [{
id: 1,
name: '数到五答应我 - 曹格',
author: '曹格',
songSrc: './static/数到五答应我 - 曹格.mp3'
},
{
id: 2,
name: '风吹着我向你跑来 - 焦迈奇',
author: '焦迈奇',
songSrc: './static/风吹着我向你跑来 - 焦迈奇.mp3'
}
]; let app = new Vue({
el: '#app',
data: {
musicData: musicData, // 可以写成 因为是一样的 musicData
// currentSrc: './static/数到五答应我 - 曹格.mp3',
currentIndex: 0
},
// 变动1 不要写死的url了 就自己拿
computed:{
getCurrentSongSrc(){
return this.musicData[this.currentIndex].songSrc
}
},
methods: {
handerClick(index) {
// 就没有必要了 就不要传值了
// this.currentSrc = src;
this.currentIndex = index
},
handerEnded() {
this.nextSong();
},
nextSong(){
this.currentIndex++; if (this.currentIndex === this.musicData.length) {
this.currentIndex = 0
}
// 这个就没有必要了
// this.currentSrc = this.musicData[this.currentIndex].songSrc;
}
}
}); </script>
</body>
</html>

vue小练习--音乐播放器的更多相关文章

  1. 微信小程序音乐播放器

    写在前面 1.入门几天小白的作品,希望为您有帮助,有好的意见或简易烦请赐教 2.微信小程序审核音乐类别已经下架,想要发布选题需慎重.附一个参考链接,感谢https://www.hishop.com.c ...

  2. 微信小程序音乐播放器组件

    wxml <image bindtap="click" src="{{isPlay?'/images/':'/images/'}}"/> JS Pa ...

  3. 用Vue来实现音乐播放器(三十八):歌词滚动列表的问题

    1.频繁切换歌曲时,歌词会跳来跳去 原因: // 歌词跳跃是因为内部有一个currentLyric对像内部有一些功能来完成歌词的跳跃 //每个currentLyric能实现歌曲的播放跳到相应的位置 是 ...

  4. Vue实战:音乐播放器(一) 页面效果

    先看一下效果图 首页 歌单详情页 歌手列表 歌手详情页 排行页面 榜单的详情页(排序样式) 搜索页面 搜索结果 播放器内核 歌词自动滚动 播放列表 用户中心

  5. 用Vue来实现音乐播放器(二十三):音乐列表

    当我们将音乐列表往上滑的时候   我们上面的歌手图片部分也会变小 当我们将音乐列表向下拉的时候   我们的图片会放大 当我们将音乐列表向上滑的时候   我们的图片有一个高斯模糊的效果 并且随着我们的列 ...

  6. 用Vue来实现音乐播放器(十八):右侧快速入口点击高亮

    问题一:当我们点击右侧快速入口的时候  被点击的地方高亮 首先我们要知道右侧快速入口是为什么高亮??因为当watch()监控到scrollY的变化了的时候  将scrollY的值和listHeight ...

  7. 用Vue来实现音乐播放器(九):歌单数据接口分析

    z这里如果我们和之前获取轮播图的数据一样来获取表单的数据  发现根本获取不到 原因是qq音乐在请求头里面加了authority和refer等 但是如果我们通过jsonp实现跨域来请求数据的话  是根本 ...

  8. 用Vue来实现音乐播放器(八):自动轮播图啊

    slider.vue组件的模板部分 <template> <div class="slider" ref="slider"> <d ...

  9. 用Vue来实现音乐播放器(二十一):歌手详情数据抓取

    第一步:在api文件夹下的singer.js中抛出getSingerDetail方法 第二步:在singer-detail.vue组件中引入api文件夹下的singer.js和config.js 第三 ...

随机推荐

  1. Python学习笔记之基础篇(五)字典

    #数据类型划分:可变数据类型 不可变数据类型 #不可变数据类型 : 元组 bool int str --> 可哈希 #可变数据类型 list ,dict set --->不可哈希 ''' ...

  2. vue使用H5实现滚动到页面底部时加载数据

    使用原生vue实现瀑布流,发现无法实现小程序那种滚动到地步触发加载效果,只能自己研究了 实现效果: 实现代码: 首先添加监听滚动事件 mounted() { window.addEventListen ...

  3. 第2节 storm实时看板案例:11、实时看板综合案例工程构建,redis的专业术语

    redis当中的一些专业术语: redis缓存击穿 redis缓存雪崩 redis的缓存淘汰 =========================================== 详见代码

  4. java虚拟机之内存分配

    Java 的自动内存管理主要是针对对象内存的回收和对象内存的分配.同时,Java 自动内存管理最核心的功能是 堆 内存中对象的分配与回收. JDK1.8之前的堆内存示意图: 从上图可以看出堆内存分为新 ...

  5. luogu P2761 软件补丁问题

    网络流(x) 状压(√) 初始状态为全1,合法状态为(state&b1)&(state|b1) == state && (state&b2)&(stat ...

  6. 高级IO-锁与进程和文件

    1.进程终止,那么这个进程建立的锁将全部释放 2.无论何时关闭一个与当前进程相关的文件描述符,那么与这个文件相关的当前进程的锁会全部被释放 3.子进程不会继承父进程的锁,子进程需要调用fcntl对继承 ...

  7. (十)微信小程序---上传图片chooseImage

    官方文档 示例一 wxml <view bindtap="uploadImage">请上传图片</view> <image wx:for=" ...

  8. UITree_study

    1.Create canvas 2.Add TreeView 3.Subscribe and unsubscribe events(订阅和取消订阅事件) 4.Data bind items it's ...

  9. 使用FFmpeg处理视频文件:视频转码、剪切、合并、播放速调整

    安装 略. 转码 最简单命令如下: ffmpeg -i out.ogv -vcodec h264 out.mp4ffmpeg -i out.ogv -vcodec mpeg4 out.mp4ffmpe ...

  10. firewalld学习-zone

    原文地址:http://www.excelib.com/article/290/show firewalld默认提供了九个zone配置文件: block.xml.dmz.xml.drop.xml.ex ...