Vue小案例
轮播图的制作
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<img :src="images[currentIndex].imgSrc" alt="" @click="imgHandler">
<br />
<button @click="pervHandler">上一张</button>
<button @click="nextHandler">下一张</button>
</div>
<script src="./vue.js"></script>
<script>
new Vue({
el:"#app",
data(){
return {
images: [
{id:1, imgSrc: "./images/1.jpeg"},
{id:2, imgSrc: "./images/2.jpeg"},
{id:3, imgSrc: "./images/3.jpeg"},
{id:4, imgSrc: "./images/4.jpeg"}
],
currentIndex:0
}
},
methods:{
pervHandler(){
this.currentIndex--;
if (this.currentIndex < 0){
this.currentIndex = 3;
}
},
nextHandler(){
this.currentIndex++;
if (this.currentIndex == 4){
this.currentIndex = 0;
}
},
imgHandler(e){
console.log(e.target);
console.log(this);
}
}
created(){
setInterval( () => {
this.currentIndex++;
if (this.currentIndex == 4){
this.currentIndex = 0;
}
}, 1000)
}
})
</script>
</body>
</html>
Vue中使用ajax一般vue不用ajax用axios
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
span.active{
color: red;
}
</style>
</head>
<body>
<div id="app">
<div>
<span v-for="(category, index) in categoryList" :key="category.id" :class="{active:currentIndex==index}" @click="handlerClick(index, category.id)">{{ category.name }} </span>
</div>
</div>
<script src="./vue.js"></script>
<script src="./jquery.min.js"></script>
<script>
new Vue({
el:"#app",
data(){
return {
categoryList:[],
currentIndex:0
}
},
methods:{
handlerClick(index, id) {
this.currentIndex = index;
$.ajax({
url: `https://www.luffycity.com/api/v1/courses/?sub_category=${id}`,
type:"GET",
success:(data)=>{
var data = data.data;
console.log(data);
}
})
}
},
created(){
$.ajax({
url: "https://www.luffycity.com/api/v1/course_sub/category/list/",
type: "GET",
success:(data) => {
if (data.error_no == 0){
var data = data.data;
console.log(this);
let obj = {
id: 0,
name: "全部",
category: 0
};
this.categoryList = data;
this.categoryList.unshift(obj);
}
},
error:function (err) {
console.log(err);
}
})
}
})
</script>
</body>
</html>
音乐播放器
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
ul li.active{
color: red;
}
</style>
</head>
<body>
<div id="music">
<audio :src="musicData[currentIndex].songSrc" controls autoplay @ended="nextHandler"></audio>
<ul>
<li v-for="(item, index) in musicData" :key="item.id" :class="{active:currentIndex===index}" @click="handlerClick(index)">
<h2>歌手: {{ item.author }}</h2>
<p>歌名: {{ item.name }}</p>
</li>
</ul>
</div>
<script src="./vue.js"></script>
<script>
var musicData = [{
id: 1,
name: '于荣光 - 少林英雄',
author: '于荣光',
songSrc: './static/于荣光 - 少林英雄.mp3'
},
{
id: 2,
name: 'Joel Adams - Please Dont Go',
author: 'Joel Adams',
songSrc: './static/Joel Adams - Please Dont Go.mp3'
},
{
id: 3,
name: 'MKJ - Time',
author: 'MKJ',
songSrc: './static/MKJ - Time.mp3'
},
{
id: 4,
name: 'Russ - Psycho (Pt. 2)',
author: 'Russ',
songSrc: './static/Russ - Psycho (Pt. 2).mp3'
}
];
new Vue({
el:"#music",
data(){
return {
musicData: [],
currentIndex: 0
}
},
methods:{
handlerClick(index){
this.currentIndex = index;
},
nextHandler(){
this.currentIndex++;
if (this.currentIndex == 4){
this.currentIndex = 0;
}
}
},
created(){
this.musicData = musicData;
}
})
</script>
</body>
</html>