Vue實現簡易翻頁效果源碼分享
更新時間:2018年11月08日 11:15:39 作者:天天要加油
本文給大家分享了vue實現簡易翻頁效果,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧
源碼如下:
<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實現簡易翻頁效果源碼分享,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
vue 3 中watch 和watchEffect 的新用法
本篇文章主要通過 Options API 和 Composition API 對比 watch 的使用方法,讓大家快速掌握 vue3 中 watch 新用法,需要的朋友可以參考一下哦,希望對大家有所幫助2021-11-11VueTreeselect?參數options的數據轉換-參數normalizer解析
這篇文章主要介紹了VueTreeselect?參數options的數據轉換-參數normalizer解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07