vue实现的网易云音乐在线播放和下载功能案例


Posted in Javascript onFebruary 18, 2019

本文实例讲述了vue实现的网易云音乐在线播放和下载功能。分享给大家供大家参考,具体如下:

效果如图:

vue实现的网易云音乐在线播放和下载功能案例

完整代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style lang="">
    html,
    body {
      height: 100%;
      padding: 0;
      margin: 0;
    }
    #app {
      height: 100%;
      display: flex;
    }
    #app>#left {
      flex: 1;
      background-color: skyblue;
      text-align: center;
      /* 超出滚动 */
      overflow: scroll;
    }
    #app>#right {
      flex: 1;
      background-color: orange;
    }
    ul {
      list-style: none;
      padding: 0;
    }
    input {
      width: 469px;
      height: 56px;
      margin: 10px auto;
      border-radius: 10px;
      outline: none;
      font-size: 24px;
      border: 0;
      padding-left: 15px;
    }
    #left li {
      width: 451px;
      /* height: 35px; */
      margin: 0 auto;
      font-weight: 700;
      border: 2px solid black;
      line-height: 35px;
      color: white;
      background-color: cadetblue;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      max-height: 35px;
      -webkit-line-clamp: 1;
      -webkit-box-orient: vertical;
    }
    #left li:hover {
      cursor: pointer;
      background-color: greenyellow;
      color: red;
    }
    #right {
      position: relative;
      overflow: scroll;
    }
    audio {
      /* position: absolute;
      left: 50%;
      transform: translateX(-50%) translateY(46px); */
      display: block;
      margin: 0 auto;
    }
    /* li标签过渡的样式 */
    .list-item {
      display: inline-block;
      margin-right: 10px;
    }
    .list-enter-active,
    .list-leave-active {
      transition: all 1s;
    }
    .list-enter,
    .list-leave-to{
      opacity: 0;
      transform: translateX(100px);
    }
    /* 设置专辑图片样式 */
    .cover{
      width: 260px;
      height: 260px;
      border-radius: 50%;
      display: block;
      margin: 10px auto;
      /* transform: translateX(-50%) translateY(10px); */
    }
    /* 动画 */
    @keyframes autoRotate{
      to{
        transform: rotateZ(360deg);
      }
    }
    /* 动画播放样式 */
    .autoRotate{
      /* 动画名,一直播放iteration(一直重复),匀速(timing),时间2s(duration),状态(running) */
      animation-name:autoRotate;
      animation-iteration-count:infinite;
      animation-timing-function: linear;
      animation-duration:2s;
      animation-play-state:running;
    }
    /* 动画状态 */
    .pause{
      animation-play-state:paused;
    }
    /* 评论 */
    .comment{
      height: 150px;
      /* background-color: skyblue; */
    }
    .comment li{
      display: flex;
      padding: 5px;
    }
    .comment li .left{
      width: 120px;
      height: 120px;
    }
    .comment li .left img{
      width: 100px;
    }
    .comment li a{
      text-decoration: none;
      font-weight: bold;
      color: crimson;
    }
  </style>
</head>
<body>
  <div id="app">
    <!-- 左边 -->
    <div id="left">
      <input type="text" value="请输入你要搜索的歌名" v-model="inputValue" @keyup.enter="search">
        <!-- 给li添加过渡 ;v-on:after-enter="afterEnter":钩子函数-->
        <transition-group name="list" tag="ul" v-on:after-enter="afterEnter">
        <!-- play(item.id):把id传过去 -->
        <li v-for="(item, index) in musicList" :key="item.id" @dblclick="playMusic(item.id,item.album.id)" :style="{'transition-delay':index*100+'ms'}" >
          {{item.name}}-----演唱者:{{item.artists[0].name}}
        </li>
        </transition-group>
    </div>
    <!-- 右边,播放 -->
    <div id="right">
      <!-- 专辑页面 -->
      <img :src="picUrl" alt="" class="cover autoRotate" :class="{pause:isPause}">
      <!-- autoplay:自动播放,controls显示控件 ;@play="play"是自定义方法-->
      <audio :src="songUrl" autoplay controls @play="play" @pause="pause" ></audio>
      <h3>精彩评论</h3>
      <div class="comment">
          <ul>
            <!-- 遍历数组时,需要动画时才用到key -->
              <li v-for="(item, index) in comments" >
                <div class="left">
                  <img :src="item.user.avatarUrl" alt="">
                </div>
                <div class="right">
                  <a href="#" rel="external nofollow" >{{item.user.nickname}}</a>
                  <p>{{item.content}}</p>
                </div>
              </li>
          </ul>
      </div>
    </div>
  </div>
  rightv>
</body>
<!-- 导入vue -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- 导入vue插件 -->
<script src="https://cdn.jsdelivr.net/npm/vue-resource@1.5.1"></script>
<script>
  //代码
  /*
    音乐播放器
    需求1:
      搜索歌曲
      发送网络请求
      回调函数函数中渲染数据
      有动画
    需求2:
      双击播放歌曲
      根据id调用接口
      查询数据
      通过audio播放歌曲
      获取专辑的信息 进而获取封面 展示给用户
    需求3
      播放歌曲时
        封面转动
      暂停播放时
        封面停止转动
    步骤:
      1.先写动画样式,动画命名为autoRotate,因为是一直运动,所以使用animation;
      2.同时写一个暂停的样式,命名为pause,给data添加一个isPause来存值,默认给一个false
      3.添加运动和暂停的步骤是添加上面的两个类,但是pause要使用v-bind指令来设置属性;
      4.在audio音频里添加播放和暂停的点击方法,在对应的方法里设置对应的布尔值;
    需求4
      点击播放歌曲
      同时获取这首歌的评论
    步骤:1.在数据中声明一个comments的空数组,用来存评论内容
      2.在播放方法中写获取评论的接口
      3.在响应体里将内容赋值给声明的数组
  */
  let app = new Vue({
    el: "#app",
    data: {
      inputValue: '',//输入的值
      musicList: [], //存储歌列表
      songUrl: '',//播放歌曲的url
      picUrl:'',//获取专辑信息
      isPause:false,//专辑是否暂停
      comments:[]//评论内容
    },
    methods: {
      // li标签过渡的事件
      randomIndex: function () {
        return Math.floor(Math.random() * this.items.length)
      },
      add: function () {
        this.items.splice(this.randomIndex(), 0, this.nextNum++)
      },
      remove: function () {
        this.items.splice(this.randomIndex(), 1)
      },
      //搜索歌曲事件
      search() {
        //调用接口
        this.$http.get(`https://autumnfish.cn/search?keywords=${this.inputValue}`).then(response => {
          // console.log(response);
          //将结果添加到musicList中
          this.musicList = response.body.result.songs;
        }, response => {
          // error callback
          alert("出错了")
        });
      },
      // 双击播放歌曲事件,接收传过来的id
      playMusic(id,albumId) {
        //获取歌曲的url
        this.$http.get(`https://autumnfish.cn/song/url?id=${id}`).then(response => {
          // console.log(response);
          //将结果添加到musicList中
          this.songUrl = response.body.data[0].url;
        }, response => {
          // error callback
          alert("出错了")
        });
        // 获取专辑信息
        this.$http.get(`https://autumnfish.cn/album?id=${albumId}`).then(res=>{
          this.picUrl=res.body.album.blurPicUrl;
        }),err=>{}
        //获取评论内容接口
        this.$http.get(`https://autumnfish.cn/comment/music?id=${id}&limit=1`).then(res=>{
          console.log(res);
          this.comments=res.body.hotComments;
        }),err=>{
          alert('信息错误')
        }
      },
      //钩子函数:动画执行完后去除了style属性,不去掉会卡顿
      afterEnter(el){
        el.style='';
      },
      // 专辑图片旋转事件
      play(){
        console.log('播放');
        this.isPause=false;
      },
      pause(){
        console.log('暂停');
        this.isPause=true;
      }
    },
  })
</script>
</html>

如果接口不能使用:请登录https://github.com/huanggengzhong/NeteaseCloudMusicApi,重新下载开启服务器即可

希望本文所述对大家vue.js程序设计有所帮助。

Javascript 相关文章推荐
ExtJS 简介 让你知道extjs是什么
Dec 29 Javascript
event.keyCode键码值表 附只能输入特定的字符串代码
May 15 Javascript
JavaScript 读取元素的CSS信息的代码
Feb 07 Javascript
js随机颜色代码的多种实现方式
Apr 23 Javascript
js实现弹窗插件功能实例代码分享
Dec 12 Javascript
2014年50个程序员最适用的免费JQuery插件
Dec 15 Javascript
Javascript中的方法和匿名方法实例详解
Jun 13 Javascript
JavaScript资源预加载组件和滑屏组件的使用推荐
Mar 10 Javascript
微信小程序 网络API Websocket详解
Nov 09 Javascript
Vue学习笔记进阶篇之函数化组件解析
Jul 21 Javascript
vue+webpack实现异步加载三种用法示例详解
Apr 24 Javascript
Vue+scss白天和夜间模式切换功能的实现方法
Jan 05 Vue.js
vue实现的微信机器人聊天功能案例【附源码下载】
Feb 18 #Javascript
vue-cli3环境变量与分环境打包的方法示例
Feb 18 #Javascript
JS实现集合的交集、补集、差集、去重运算示例【ES5与ES6写法】
Feb 18 #Javascript
JS基于开关思想实现的数组去重功能【案例】
Feb 18 #Javascript
JS实现点击li标签弹出对应的索引功能【案例】
Feb 18 #Javascript
Vue框架TypeScript装饰器使用指南小结
Feb 18 #Javascript
深入理解vue-class-component源码阅读
Feb 18 #Javascript
You might like
输出控制类
2006/10/09 PHP
一个php作的文本留言本的例子(三)
2006/10/09 PHP
php中拷贝构造函数、赋值运算符重载
2012/07/25 PHP
PHP中file_exists与is_file,is_dir的区别介绍
2012/09/12 PHP
laravel 去掉index.php伪静态的操作方法
2019/10/12 PHP
php设计模式之状态模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
imagettftext() 失效,不起作用
2021/03/09 PHP
XML+XSL 与 HTML 两种方案的结合
2007/04/22 Javascript
网站页面自动跳转实现方法PHP、JSP(下)
2010/08/01 Javascript
在Ajax中使用Flash实现跨域数据读取的实现方法
2010/12/02 Javascript
使用js写的一个简易的投票
2013/11/27 Javascript
angularJS 中$attrs方法使用指南
2015/02/09 Javascript
jQuery简单注册和禁用全局事件的方法
2016/07/25 Javascript
JS函数修改html的元素内容,及修改属性内容的方法
2016/10/28 Javascript
vue如何集成raphael.js中国地图的方法示例
2017/08/15 Javascript
React Native 自定义下拉刷新上拉加载的列表的示例
2018/03/01 Javascript
vue使用rem实现 移动端屏幕适配
2018/09/26 Javascript
解决iview多表头动态更改列元素发生的错误的方法
2018/11/02 Javascript
layDate插件设置开始和结束时间
2018/11/15 Javascript
angular中如何绑定iframe中src的方法
2019/02/01 Javascript
关于JavaScript 数组你应该知道的事情(推荐)
2019/04/10 Javascript
详解Python程序与服务器连接的WSGI接口
2015/04/29 Python
Django框架多表查询实例分析
2018/07/04 Python
python实现逐个读取txt字符并修改
2018/12/24 Python
Django中如何使用sass的方法步骤
2019/07/09 Python
python开发实例之Python的Twisted框架中Deferred对象的详细用法与实例
2020/03/19 Python
简单了解python列表和元组的区别
2020/05/14 Python
Python 实现一个简单的web服务器
2021/01/03 Python
Free People中国官网:波西米亚风格女装服饰
2016/08/30 全球购物
玩具反斗城天猫官方旗舰店:享誉全球的玩具店
2017/10/10 全球购物
《小动物过冬》教学反思
2014/04/17 职场文书
村抢险救灾方案
2014/05/09 职场文书
个人查摆问题整改措施
2014/10/04 职场文书
数据库连接池
2021/04/06 MySQL
解决Golang中ResponseWriter的一个坑
2021/04/27 Golang
Python Pygame实战在打砖块游戏的实现
2022/03/17 Python