Vue实现简易翻页效果源码分享
程序员文章站
2022-06-05 14:34:55
源码如下:
sl...
源码如下:
<html> <head> <meta charset="utf-8"> <title>slidepage</title> <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script> <style type="text/css"> *{ margin: 0; padding: 0; } .container { width: 100%; margin: 0 auto; text-align: center; } .content{ font-size: 400px } .leftbtn{ width: 45px; height: 45px; margin-right: 15px; } .rightbtn{ width: 45px; height: 45px; margin-left: 15px; } </style> </head> <body> <div id='root'> <div v-if="numberarr.length == 0">{{showmessage}}</div> <div class="container" v-for="(item, index) in getcurpagecontent(numberarr, curpage, itemnumperpage)" :key="index"> <div class="content">{{item}}</div> <div class="pagebuttonlist"> <button class="leftbtn" @click="handleclick('leftbtn')"><</button> <span class="pagination">{{curpage}}/{{totalpage}}</span> <button class="rightbtn" @click="handleclick('rightbtn')">></button> </div> </div> </div> <script> new vue({ el: "#root", data(){ return { showmessage: 'no number', content:'', numberarr: [1, 2, 3, 4], curpage: 1, totalpage: 1, itemnumperpage: 1 } }, mounted() { this.init() }, methods:{ init(){ this.totalpage = math.ceil(this.numberarr.length / this.itemnumperpage) this.totalpage = this.totalpage < 1 ? 1 : this.totalpage }, getcurpagecontent: function(numberarr, curpage, itemnumperpage){ return numberarr.filter(function(element, index){ if(index >= (curpage -1)* itemnumperpage && index < curpage *itemnumperpage){ return true }else{ return false } }) }, handleclick: function(arg){ if(arg == 'leftbtn'){ this.curpage = this.curpage > 1 ? --this.curpage : this.totalpage }else if (arg == 'rightbtn'){ this.curpage = this.curpage < this.totalpage ? ++this.curpage: 1 } } // handleleftclick: function(){ // if(this.curpage > 1){ // this.curpage -- // }else{ // this.curpage = this.totalpage // } // }, // handlerightclick: function(){ // if(this.curpage < this.totalpage){ // this.curpage ++ // }else{ // this.curpage = 1 // } // } } }) </script> </body> </html>
效果如下所示,点击左右能切换页面:
总结
以上所述是小编给大家介绍的vue实现简易翻页效果源码分享,希望对大家有所帮助