Code Monkey home page Code Monkey logo

music163's Introduction

网易云音乐(移动端)

使用 jQuery 实现移动端网易云音乐,包含首页、推荐歌单、播放器三个页面。

预览

电脑请使用Chrome打开,并右键->检查打开开发者模式,使用移动端模拟器查看。或使用手机扫描二维码打开链接。

预览链接:预览
二维码:
1514712938.png

技术

jQuery/动态REM/LeanCloud

后端数据存储使用了LeanCloud,具体可查询 LeanCloud数据存储入门教程

实现的功能

  • 音乐播放、暂停
  • 搜索单曲、歌手、专辑
  • 热门搜索
  • 搜索历史记录
  • 排行榜
  • 推荐歌单
  • 歌词(&歌词翻译)

运行

  1. clone 项目至本地 git clone https://github.com/xmxshr/music163.git
  2. 进入项目文件夹 cd music163
  3. 安装http服务器http-servernpm i http-server -g
  4. 启动http服务器 http-server -c-1
  5. 使用浏览器打开 http://127.0.0.1:8080

难点

播放页面的旋转光盘

光盘分为三层,上层亮光投影、光盘内部图片、黑色底盘。
将黑色底盘固定不动,上层亮光投影、光盘内部图片添加『360°无限旋转』的动画。

歌词的截取、滚动

根据所下载的歌词格式,写一个正则表达式,将歌词变为一个如下数组。

const lyrics = [{
  time: '00:00',
  lyric: '作词/作曲',
},{
  ...
}];

其中time表示当前句歌词显示时间,lyric表示当前句歌词。
遍历数组lyrics 创建标签<p data-time=${array[i].time}>${array[i].lyric}</p>
使用HTML5新标签<audio>, 根据audio.currentTime可得到当前音乐播放时间。设置一个定时器,定时判断『当前句歌词』。

获取当前句歌词:

const $lyrics = $('.song-description .lyric .words p');
  let $whichLine;
  for (let i = 0; i < array.length; i++) {
    // 最后一句歌词
    if (i === array.length - 1) {
      $whichLine = $lyrics.eq(i);
    // 上一句歌词时间 <= 当前时间 <= 下一句歌词时间
    } else if ($lyrics.eq(i).attr('data-time') <= currentTime && currentTime < $lyrics.eq(i + 1).attr('data-time')) {
      $whichLine = $lyrics.eq(i);
      break;
    }
  }

滚动歌词并变色:

if ($whichLine) {
  $whichLine.addClass('active').prev().removeClass('active');
  const top = $whichLine.offset().top;
  const ctTop = $('.song-description .lyric .words').offset().top;
  const delta = top - ctTop - $('.song-description .lyric').innerHeight()*lineHeight / 3;
  $('.song-description .lyric .words').css({
    transform: `translateY(-${delta/fontSize}rem)`;
  });
}

细节

为推荐搜索加定时器

搜索时监听搜索框的input事件,以提供『推荐搜索』。即只要输入就会发送请求。 为了阻止不必要的请求,可在监听函数里加一个定时器。在用户停止输入之后,再提供『推荐搜索』选项。

let timer;
$('input#search').on('input', (e) => {
  if (timer) {
    window.clearTimeout(timer);
  }
  timer = setTimeout(() => {
    const $input = $(e.currentTarget);
    const inputValue = $input.val().trim();
    // 显示input清空按钮
    setClearInput(true);
    if (inputValue === '') return;
    // 发送『推荐搜索』请求
    query.find().then((results) => {
      ...
    }).catch();
  }, 300);
}

music163's People

Contributors

xmxshr avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.