简单的音乐盒子

利用计算属性(监听数据),解决上一首,下一首,点击切换

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<style>
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
} /* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
} body {
line-height: 1;
} ol, ul {
list-style: none;
} blockquote, q {
quotes: none;
} blockquote:before, blockquote:after,
/* 清除浮动*/
q:before, q:after {
content: '';
content: none;
} table {
border-collapse: collapse;
border-spacing: 0;
}
li{
border: 2px solid deepskyblue;
padding: 5px;
}
</style>
</head>
<body>
<!-- autoplay 自动播放 调整音量 自动循环 @ended 自动下一首 -->
<div id="music">
<!-- :scr 要链接下面的数据 要通过指令系统-->
<audio :src="currentSong" autoplay controls autoloop @ended = 'nextSong()'></audio>
<ul>
<!-- 点击换歌 -->
<li v-for = '(item, i) in songs' @click = "currentHandler(i)">
<h3>作者:{{item.author}}</h3>
<p>{{item.name}}</p>
</li>
</ul>
<button @click = 'upSong'>上一首</button> <!-- 方法名,不加参数可以不写() -->
<button @click = 'nextSong'>下一首</button>
<button @click = 'addSong'>添加</button>
<!-- @click 方法-->
</div> <!--MVVM Model==》数组 数据库 View == document 标签 上面的div VM--> <script type="text/javascript" src="vue.js"></script>
<script type="text/javascript">
// 数据的双向绑定 m<=>v
var songs = [
{id:1,src:'./audios/audios/1.mp3',name:"水瓶座",author:'陈晓东'},
{id:2,src:'./audios/audios/2.mp3',name:"倾城",author:'陈奕迅'},
{id:3,src:'./audios/audios/3.mp3',name:"海阔天空",author:'Beyond'},
{id:4,src:'./audios/audios/4.mp3',name:"我很快乐",author:'刘惜君'} ];
var music = new Vue({
el:'#music',
data:{
// 数据属性Model
songs : songs,
// currentSong : "./audios/audios/1.mp3",
index:0
},
methods:{
// 事件名方法要在这里声明
currentHandler(i){ // 点击歌名换歌
this.index = i; // 这里绑定了数据属性,一改就调动下面的计算属性
// this.currentSong = this.songs[i].src;
},
nextSong(){
this.index++;
// this.currentSong = this.songs[this.index].src;
},
upSong(){
this.index = this.index-1;
// this.currentSong = this.songs[this.index].src;
},
addSong(){
this.songs.push( {id:5,src:'./audios/audios/4.mp3',name:"我很快乐",author:'刘惜君'})
}
},
computed:{ // 计算属性,实行监听
currentSong(){
return this.songs[this.index].src
}
}
})
</script>
</body>
</html>

vue-one_demo_music的更多相关文章

  1. Vue.js 和 MVVM 小细节

    MVVM 是Model-View-ViewModel 的缩写,它是一种基于前端开发的架构模式,其核心是提供对View 和 ViewModel 的双向数据绑定,这使得ViewModel 的状态改变可以自 ...

  2. wepack+sass+vue 入门教程(三)

    十一.安装sass文件转换为css需要的相关依赖包 npm install --save-dev sass-loader style-loader css-loader loader的作用是辅助web ...

  3. wepack+sass+vue 入门教程(二)

    六.新建webpack配置文件 webpack.config.js 文件整体框架内容如下,后续会详细说明每个配置项的配置 webpack.config.js直接放在项目demo目录下 module.e ...

  4. wepack+sass+vue 入门教程(一)

    一.安装node.js node.js是基础,必须先安装.而且最新版的node.js,已经集成了npm. 下载地址 node安装,一路按默认即可. 二.全局安装webpack npm install ...

  5. Vue + Webpack + Vue-loader 系列教程(2)相关配置篇

    原文地址:https://lvyongbo.gitbooks.io/vue-loader/content/ 使用预处理器 在 Webpack 中,所有的预处理器需要和一个相应的加载器一同使用.vue- ...

  6. Vue + Webpack + Vue-loader 系列教程(1)功能介绍篇

    原文地址:https://lvyongbo.gitbooks.io/vue-loader/content/ Vue-loader 是什么? vue-loader 是一个加载器,能把如下格式的 Vue ...

  7. 关于Vue.js 2.0 的 Vuex 2.0,你需要更新的知识库

    应用结构 实际上,Vuex 在怎么组织你的代码结构上面没有任何限制,相反,它强制规定了一系列高级的原则: 应用级的状态集中放在 store 中. 改变状态的唯一方式是提交mutations,这是个同步 ...

  8. Vue.js 2.0 和 React、Augular等其他框架的全方位对比

    引言 这个页面无疑是最难编写的,但也是非常重要的.或许你遇到了一些问题并且先前用其他的框架解决了.来这里的目的是看看Vue是否有更好的解决方案.那么你就来对了. 客观来说,作为核心团队成员,显然我们会 ...

  9. 窥探Vue.js 2.0 - Virtual DOM到底是个什么鬼?

    引言 你可能听说在Vue.js 2.0已经发布,并且在其中新添加如了一些新功能.其中一个功能就是"Virtual DOM". Virtual DOM是什么 在之前,React和Em ...

  10. 初探Vue

    Vue.js(读音/vju:/,类似于view),是近来比较火的前端框架,但一直没有怎么具体了解.实现过,就知道个啥的MVVM啦,数据驱动啦,等这些关于Vue的虚概念. 由于最近,小生在公司中,负责开 ...

随机推荐

  1. ubuntu16.04下安装mysql详细步骤

    1.如果要搭建服务器先去购买一个云主机,比如阿里云.京东云.新网等等都有卖.这里推荐使用京东云服务器,因为最近在搞活动.一元可体验两个月(可能现在活动已经过了,但在京东云里有免费领一个月的,学生机也有 ...

  2. codeforces Round#429 (Div2)

    2017-08-20 10:00:37 writer:pprp 用头文件#include <bits/stdc++.h>很方便 A. Generous Kefa codeforces 84 ...

  3. 你不知道的东西! c# == 等于运算符 和 Object.Equals()

    最近在看 高级点的程序员必看的     CLR via C#    书中说解释了 Object.Equals()  方法的实现, 其中具体的实现用的是 == 运算符 ! 以前就对 == 运算符 的具体 ...

  4. Android数据库框架-----ORMLite关联表的使用

    上一篇已经对ORMLite框架做了简单的介绍:Android数据库框架-----ORMLite 的基本用法~~本篇将介绍项目可能会使用到的一些用法,也为我们的使用ORMLite框架总结出一个较合理的用 ...

  5. nyoj998——欧拉+折半查找

    Sum 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 给你一个数N,使得在1~N之间能够找到x使得x满足gcd( x ,  N  ) >= M, 求解gcd( ...

  6. Date类型

    1.创建日期对象 var now = new Date(); var someDate = new Date(Date.parse("May 25, 2004")); var so ...

  7. H3C WA2610i-GN FitAP telnet打开

    在ac上面:sys_hidecmd 隐藏模式wlan ap ap1 exec-control enable ##目前为disable状态wlan ap ap1 telnet enable ##目前为e ...

  8. IOS-实战分享:实时美颜滤镜是怎样炼成的

    作者:琨君 原文链接:http://www.jianshu.com/p/945fc806a9b4 本文获作者授权转载 背景 前段时间由于项目需求,做了一个基于GPUImage的实时美颜滤镜.现在各种各 ...

  9. IOS-涂鸦

    // // PaintView.m // IOS_0224_涂鸦 // // Created by ma c on 16/2/24. // Copyright © 2016年 博文科技. All ri ...

  10. day5-subprocess模块

    一.概述 实际应用中,有些时候我们不得不跟操作系统进行指令级别的交互,如Linux中的shell.Python中早期通过os模块和commands模块来实现操作系统级别的交互,但从2.4版本开始,官方 ...