时间轴前后翻页

描述:日期每隔3个前后翻页

效果如下:

代码实现:

javascript 复制代码
<template>
  <div class="flex">
    <button @click="prev">前一页</button>
    <div class="box flex flex-1">
      <div class="box-item" v-for="(item, index) in mockTableFormate" :key="index">
        {{ item }}
      </div>
    </div>
    <button @click="next">后一页</button>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
const mockTable = [
  '2024-12-1',
  '2024-12-2',
  '2024-12-3',
  '2024-12-4',
  '2024-12-5',
  '2024-12-6',
  '2024-12-7',
  '2024-12-8',
  '2024-12-9',
  '2024-12-10',
  '2024-12-11'
]

const total = mockTable.length
const currentIndex = ref(total)
const step = 3

const mockTableFormate = computed(() => {
  if (currentIndex.value >= step) {
    return mockTable.slice(currentIndex.value - step, currentIndex.value)
  } else {
    return mockTable.slice(0, currentIndex.value)
  }
})

function prev() {
  if (currentIndex.value - step > 0) {
    currentIndex.value = Math.max(currentIndex.value - step, 0)
  }
}

function next() {
  currentIndex.value = Math.min(currentIndex.value + step, total)
}
</script>

<style lang="scss" scoped>
.box {
  margin: 0 10px;
  width: 300px;
  .box-item {
    border: 1px solid red;
    border-radius: 4px;
  }
}

.flex {
  display: flex;
  justify-content: space-around;
}
</style>
相关推荐
Main. 2417 小时前
从0到1学习Qt -- Qt3D入门
开发语言·qt·学习
接着奏乐接着舞。17 小时前
Go 一小时上手指南:从零到运行第一个程序
开发语言·后端·golang
飞机和胖和黄17 小时前
王道C语言第一周作业
c语言·开发语言
lly20240617 小时前
SQLite 安装指南
开发语言
星火开发设计17 小时前
C++ deque 全面解析与实战指南
java·开发语言·数据结构·c++·学习·知识
cnxy18817 小时前
Python Web开发新时代:FastAPI vs Django性能对比
前端·python·fastapi
神仙姐姐QAQ17 小时前
vue3更改.el-dialog__header样式不生效
前端·javascript·vue.js
脾气有点小暴17 小时前
uniapp真机调试无法连接
前端·uni-app
AI_567817 小时前
Vue.js 深度开发指南:从数据绑定到状态管理的最佳实践
前端·javascript·vue.js
Irene199117 小时前
Sass常用语法总结
前端·sass