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

html部分,將page作為一個單獨(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" >上一頁</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" >下一頁</a>
</li>
</ul>
</script>
<div id="app">
<page></page>
</div>
js部分:
<script>
Vue.component("page", {
template: "#page",
data: function () {
return {
current: 1, // 當(dāng)前頁碼
showItem: 5, // 最少顯示5個頁碼
allpage: 13 // 總共的
}
},
computed: {
pages: function () {
var pag = [];
if (this.current < this.showItem) { //如果當(dāng)前的激活的項(xiàng) 小于要顯示的條數(shù)
//總頁數(shù)和要顯示的條數(shù)那個大就顯示多少條
var i = Math.min(this.showItem, this.allpage);
while (i) {
pag.unshift(i--);
}
} else { //當(dāng)前頁數(shù)大于顯示頁數(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請求
}
}
})
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)的分頁,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
使用D3.js+Vue實(shí)現(xiàn)一個簡單的柱形圖
這篇文章主要介紹了使用D3.js+Vue實(shí)現(xiàn)一個簡單的柱形圖,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08
vue?el-input設(shè)置必填提示功能(單個與多個)
有的功能需要設(shè)置必填項(xiàng),當(dāng)然也需要判斷是不是添上了,下面這篇文章主要給大家介紹了關(guān)于vue?el-input設(shè)置必填提示功能(單個與多個)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
vue-cli如何修改打包項(xiàng)目結(jié)構(gòu)及前綴
這篇文章主要介紹了vue-cli如何修改打包項(xiàng)目結(jié)構(gòu)及前綴問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
vue3.x項(xiàng)目中,出現(xiàn)紅色波浪線問題及解決
這篇文章主要介紹了vue3.x項(xiàng)目中,出現(xiàn)紅色波浪線問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
從vue基礎(chǔ)開始創(chuàng)建一個簡單的增刪改查的實(shí)例代碼(推薦)
這篇文章主要介紹了從vue基礎(chǔ)開始創(chuàng)建一個簡單的增刪改查的實(shí)例代碼,需要的朋友參考下2018-02-02
Vue實(shí)現(xiàn)星級評價效果實(shí)例詳解
這篇文章主要介紹了Vue實(shí)現(xiàn)星級評價效果的實(shí)例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12
Vue3中使用ref標(biāo)簽對組件進(jìn)行操作方法
這篇文章主要介紹了Vue3中使用ref標(biāo)簽對組件進(jìn)行操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
vue前端項(xiàng)目打包成Docker鏡像并運(yùn)行的實(shí)現(xiàn)
這篇文章主要介紹了vue前端項(xiàng)目打包成Docker鏡像并運(yùn)行的實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08

