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. Flutter 使用json_model解析json生成dart文件

    一.json_serializable使用步骤 1.集成json_serializable pubspec.yaml 添加以下依赖 dependencies: json_annotation: ^2. ...

  2. kafka-console-consumer接收不到flume推送过来的消息

    原因和解决方法:需要先启动kafka,再启动flume,两者启动有先后顺序.

  3. JS截取腾讯视频和去除视频广告

    一:腾讯视频截取 H5视频播放除了video标签以外,还有iframe嵌套视频 项目需求是用户输入腾讯视频的html链接,如 https://v.qq.com/x/page/y0116k2vspw.h ...

  4. linux修改键盘按键

    我的电脑:Fedora-19 $ uname -a Linux localhost.localdomain 3.11.10-200.fc19.i686 #1 SMP Mon Dec 2 20:48:2 ...

  5. HDU-1312题解(DFS)

    HDU-1312-DFS Written by Void-Walker    2020-02-01 09:09:25 1.题目传送门:http://acm.hdu.edu.cn/showproblem ...

  6. 5.8 Nginx 常用功能的配置

  7. IEEE Spectrum 2014年十大编程语言盘点

    近日,IEEE Spectrum推出 了一个最流行的编程语言排行榜.排行榜筛选了 12 项指标,综合了 10 个来源(含 IEEE Xplore.Google.GitHub)的数据,最终评选出了下面这 ...

  8. centos查找文件\目录\内容命令

    1.查找文件 find / -name 'php.ini'12.查找目录 find / -name 'path' find / -name 'path' -type d13.查找内容 find . | ...

  9. Linux-kernel-timeline

    Linux kernel Protocol Location HTTP https://www.kernel.org/pub/ GIT https://git.kernel.org/ RSYNC rs ...

  10. 【SQL必知必会笔记(3)】SELECT语句的WHERE子句数据过滤操作

    上个笔记主要介绍了利用SELECT语句检索单个/多个/所有列,并利用DISTINCT关键字检索具有唯一性的值.利用LIMIT/OFFSET子句限制结果:以及利用ORDER BY子句排序检索出的数据,主 ...