基于vue.js實(shí)現(xiàn)的分頁(yè)
本文主要介紹基于vue的分頁(yè)原生寫法。
先po上效果圖:

html部分,將page作為一個(gè)單獨(dú)的組件
<script type="text/x-template" id="page">
<ul class="pagination">
<li v-show="current != 1" @click="current-- && goto(current)">
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一頁(yè)</a>
</li>
<li v-for="index in pages" @click="goto(index)" :class="{'active':current == index}" :key="index">
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{index}}</a>
</li>
<li v-show="allpage != current && allpage != 0 " @click="current++ && goto(current++)">
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁(yè)</a>
</li>
</ul>
</script>
<div id="app">
<page></page>
</div>
js部分:
<script>
Vue.component("page", {
template: "#page",
data: function () {
return {
current: 1, // 當(dāng)前頁(yè)碼
showItem: 5, // 最少顯示5個(gè)頁(yè)碼
allpage: 13 // 總共的
}
},
computed: {
pages: function () {
var pag = [];
if (this.current < this.showItem) { //如果當(dāng)前的激活的項(xiàng) 小于要顯示的條數(shù)
//總頁(yè)數(shù)和要顯示的條數(shù)那個(gè)大就顯示多少條
var i = Math.min(this.showItem, this.allpage);
while (i) {
pag.unshift(i--);
}
} else { //當(dāng)前頁(yè)數(shù)大于顯示頁(yè)數(shù)了
var middle = this.current - Math.floor(this.showItem / 2), //從哪里開始
i = this.showItem;
if (middle > (this.allpage - this.showItem)) {
middle = (this.allpage - this.showItem) + 1
}
while (i--) {
pag.push(middle++);
}
}
return pag
}
},
methods: {
goto: function (index) {
if (index == this.current) return;
this.current = index;
//這里可以發(fā)送ajax請(qǐng)求
}
}
})
var vm = new Vue({
el: '#app',
})
</script>
css部分:
body {
font-family: "Segoe UI";
}
li {
list-style: none;
}
a {
text-decoration: none;
}
.pagination {
position: relative;
}
.pagination li {
display: inline-block;
margin: 0 5px;
}
.pagination li a {
padding: .5rem 1rem;
display: inline-block;
border: 1px solid #ddd;
background: #fff;
color: #0E90D2;
}
.pagination li a:hover {
background: #eee;
}
.pagination li.active a {
background: #0E90D2;
color: #fff;
}
最后附上github地址:https://github.com/AmberWuWu/vue-page
總結(jié)
以上所述是小編給大家介紹的基于vue.js實(shí)現(xiàn)的分頁(yè),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
一文詳解Vue3組件通信輕松玩轉(zhuǎn)復(fù)雜數(shù)據(jù)流
在大型Vue項(xiàng)目中,組件通信如同神經(jīng)網(wǎng)絡(luò)般貫穿整個(gè)應(yīng)用,這篇文章將為大家詳細(xì)介紹一下Vue3中的組件通信方式,有需要的小伙伴可以了解下2025-02-02
vue使用富文本編輯器vue-quill-editor的操作指南和注意事項(xiàng)
vue中很多項(xiàng)目都需要用到富文本編輯器,在使用了ueditor和tinymce后,發(fā)現(xiàn)并不理想,所以果斷使用vue-quill-editor來(lái)實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于vue使用富文本編輯器vue-quill-editor的操作指南和注意事項(xiàng),需要的朋友可以參考下2023-05-05
Vue計(jì)算屬性實(shí)現(xiàn)成績(jī)單
這篇文章主要為大家詳細(xì)介紹了Vue計(jì)算屬性實(shí)現(xiàn)成績(jī)單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
ElementUI中兩個(gè)Select選擇聯(lián)動(dòng)效果實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于ElementUI中兩個(gè)Select選擇聯(lián)動(dòng)效果實(shí)現(xiàn)的相關(guān)資料,在前端項(xiàng)目開發(fā)中,經(jīng)常會(huì)遇到省市縣三級(jí)聯(lián)動(dòng)的下拉列表框組的問(wèn)題,需要的朋友可以參考下2023-08-08
在Vue?3中使用OpenLayers加載GPX數(shù)據(jù)并顯示圖形效果
本文介紹了如何在Vue 3中使用OpenLayers加載GPX格式的數(shù)據(jù)并在地圖上顯示圖形,通過(guò)使用OpenLayers的GPX解析器,我們能夠輕松地處理和展示來(lái)自GPS設(shè)備的地理數(shù)據(jù),需要的朋友可以參考下2024-12-12
Vue中自動(dòng)生成路由配置文件覆蓋路由配置的思路詳解
這篇文章主要介紹了Vue中自動(dòng)生成路由配置文件覆蓋路由配置的思路詳解,大概思路是讀取@/views下所有index.vue如果當(dāng)前文件下有包含相同路徑則認(rèn)為是它的子路由,需要的朋友可以參考下2024-05-05

