vue篇 2、简单的轮播图 ajax、简单的音乐播放器、计算属性computed
手动轮播
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="d1"> <img :src="images[urrentIndex].imgSrc" alt=""> <button @click="nextP">下一张</button> <button @click="lastP">上一张</button> </div> <script src="./vue.js"></script> <script> new Vue({ el:'#d1', data(){ return{ images:[ {id:1,imgSrc:'./1.jpg'}, {id:1,imgSrc:'./2.jpg'}, {id:1,imgSrc:'./3.jpg'}, {id:1,imgSrc:'./4.jpg'}, {id:1,imgSrc:'./5.jpg'}, ], urrentIndex:0 } }, methods:{ nextP(){ if(this.urrentIndex === 4){ this.urrentIndex =0; }else {this.urrentIndex++;} }, lastP(){ if(this.urrentIndex === 0){ this.urrentIndex =4; }else { this.urrentIndex--;} } } }) </script> </body> </html>
cdn :
www.bootcdn.cn
利用vue的created() 钩子 和setInterval来实现自动轮播效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="d1"> <img :src="images[urrentIndex].imgSrc" alt=""> <button @click="nextP">下一张</button> <button @click="lastP">上一张</button> </div> <script src="./vue.js"></script> <script> new Vue({ el:'#d1', data(){ return{ images:[ {id:1,imgSrc:'./1.jpg'}, {id:1,imgSrc:'./2.jpg'}, {id:1,imgSrc:'./3.jpg'}, {id:1,imgSrc:'./4.jpg'}, {id:1,imgSrc:'./5.jpg'}, ], urrentIndex:0 } }, methods:{ nextP(){ if(this.urrentIndex === 4){ this.urrentIndex =0; }else {this.urrentIndex++;} }, lastP(){ if(this.urrentIndex === 0){ this.urrentIndex =4; }else { this.urrentIndex--;} } } , created(){ setInterval(()=>{ if(this.urrentIndex === 3){ this.urrentIndex =0; }else {this.urrentIndex++;} },3000); } }) </script> </body> </html>