用uniapp写一个demo,能实现分页器
时间: 2024-03-26 09:36:22 浏览: 56
好的,以下是一个使用uniapp实现的分页器demo。
HTML部分:
```html
<template>
<view>
<view class="paginator">
<button :disabled="currentPage === 1" @click="prevPage"><</button>
<button v-for="pageNumber in pageNumbers" :key="pageNumber"
:class="{ 'active': pageNumber === currentPage }"
@click="changePage(pageNumber)">{{ pageNumber }}</button>
<button :disabled="currentPage === totalPages" @click="nextPage">></button>
</view>
</view>
</template>
```
CSS部分:
```css
.paginator {
display: flex;
align-items: center;
}
button {
margin: 0 5px;
padding: 2px 8px;
border: none;
border-radius: 4px;
background-color: #f8f8f8;
color: #333;
font-size: 14px;
cursor: pointer;
}
button.active {
background-color: #333;
color: #fff;
}
```
JavaScript部分:
```javascript
export default {
data() {
return {
currentPage: 1,
totalPages: 10,
pageNumbers: 5
};
},
computed: {
// 计算页码按钮数组
pageButtonList() {
let startPage = Math.max(1, this.currentPage - Math.floor(this.pageNumbers / 2));
let endPage = Math.min(this.totalPages, startPage + this.pageNumbers - 1);
let list = [];
for (let i = startPage; i <= endPage; i++) {
list.push(i);
}
return list;
}
},
methods: {
// 上一页
prevPage() {
if (this.currentPage > 1) {
this.currentPage--;
}
},
// 下一页
nextPage() {
if (this.currentPage < this.totalPages) {
this.currentPage++;
}
},
// 改变页码
changePage(pageNumber) {
this.currentPage = pageNumber;
}
}
};
```
这个demo中使用了uniapp的单文件组件,分页器的样式和交互都在组件内部实现。你可以在组件中修改页码数、样式等。
阅读全文
相关推荐

















