时间轴前后翻页

描述:日期每隔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>
相关推荐
阿正的梦工坊1 分钟前
JavaScript 函数作用域详解——为什么函数外面访问不到里面的变量?
开发语言·javascript
黑臂麒麟3 分钟前
React Hooks 闭包陷阱:状态“丢失“的经典坑
javascript·react native·react.js·ecmascript
1314lay_10075 分钟前
Vue+C#根据配置文件实现动态构建查询条件和动态表格
javascript·vue.js·elementui·c#
SuperEugene10 分钟前
Vue3 前端配置驱动避坑:配置冗余、渲染性能、扩展性问题解决|配置驱动开发实战篇
前端·javascript·vue.js·驱动开发·前端框架
JianZhen✓13 分钟前
从零到一:基于声网Agora的医疗视频问诊前端实战指南
前端·音视频
DS数模13 分钟前
2026年Mathorcup数学建模竞赛A题思路解析+代码+论文
开发语言·数学建模·matlab·mathorcup·妈妈杯·2026妈妈杯
GISer_Jing15 分钟前
LangChain浏览器Agent开发全攻略
前端·ai·langchain
叶子野格17 分钟前
《C语言学习:编程例题》8
c语言·开发语言·c++·学习·算法·visual studio
Java面试题总结18 分钟前
Python 入门(四)- Openpyxl 操作 Excel 教程
开发语言·python·excel
小李子呢021124 分钟前
前端八股---脚手架工具Vue CLI(Webpack) vs Vite
前端·vue.js·webpack