当前位置:首页 > VUE

vue实现年历

2026-01-11 22:54:26VUE

实现年历的Vue组件

在Vue中实现年历功能可以通过组合使用日期处理库和组件化开发来完成。以下是两种常见实现方式:

基础实现方案

安装日期处理库(如dayjs或date-fns):

npm install dayjs

创建Vue组件基本结构:

<template>
  <div class="year-calendar">
    <div v-for="month in months" :key="month" class="month-container">
      <h3>{{ getMonthName(month) }}</h3>
      <div class="days-grid">
        <div v-for="day in getDaysInMonth(month)" 
             :key="day"
             class="day-cell">
          {{ day }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import dayjs from 'dayjs'

export default {
  data() {
    return {
      currentYear: dayjs().year()
    }
  },
  computed: {
    months() {
      return Array.from({length: 12}, (_, i) => i + 1)
    }
  },
  methods: {
    getMonthName(month) {
      return dayjs(`${this.currentYear}-${month}-01`).format('MMMM')
    },
    getDaysInMonth(month) {
      const days = dayjs(`${this.currentYear}-${month}`).daysInMonth()
      return Array.from({length: days}, (_, i) => i + 1)
    }
  }
}
</script>

<style>
.year-calendar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}
.month-container {
  border: 1px solid #eee;
  padding: 10px;
}
.days-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}
.day-cell {
  text-align: center;
  padding: 5px;
}
</style>

增强版实现方案

vue实现年历

添加日期选择和事件标记功能:

<template>
  <div>
    <select v-model="selectedYear">
      <option v-for="year in yearRange" :value="year">{{ year }}</option>
    </select>

    <div class="year-calendar">
      <div v-for="month in months" :key="month" class="month-container">
        <h3>{{ getMonthName(month) }}</h3>
        <div class="days-grid">
          <div v-for="n in getOffset(month)" class="empty-cell"></div>
          <div v-for="day in getDaysInMonth(month)" 
               :key="day"
               :class="['day-cell', { 
                 'today': isToday(month, day),
                 'selected': isSelected(month, day),
                 'has-event': hasEvent(month, day)
               }]"
               @click="selectDate(month, day)">
            {{ day }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import dayjs from 'dayjs'

export default {
  data() {
    return {
      selectedYear: dayjs().year(),
      selectedDate: null,
      events: [
        { date: '2023-05-15', title: '会议' },
        { date: '2023-08-20', title: '生日' }
      ]
    }
  },
  computed: {
    yearRange() {
      const current = dayjs().year()
      return Array.from({length: 10}, (_, i) => current - 5 + i)
    },
    months() {
      return Array.from({length: 12}, (_, i) => i + 1)
    }
  },
  methods: {
    getMonthName(month) {
      return dayjs(`${this.selectedYear}-${month}-01`).format('MMMM')
    },
    getDaysInMonth(month) {
      const days = dayjs(`${this.selectedYear}-${month}`).daysInMonth()
      return Array.from({length: days}, (_, i) => i + 1)
    },
    getOffset(month) {
      return dayjs(`${this.selectedYear}-${month}-01`).day()
    },
    isToday(month, day) {
      const date = dayjs(`${this.selectedYear}-${month}-${day}`)
      return date.isSame(dayjs(), 'day')
    },
    isSelected(month, day) {
      if (!this.selectedDate) return false
      const date = dayjs(`${this.selectedYear}-${month}-${day}`)
      return date.isSame(this.selectedDate, 'day')
    },
    hasEvent(month, day) {
      const dateStr = dayjs(`${this.selectedYear}-${month}-${day}`).format('YYYY-MM-DD')
      return this.events.some(event => event.date === dateStr)
    },
    selectDate(month, day) {
      this.selectedDate = dayjs(`${this.selectedYear}-${month}-${day}`)
    }
  }
}
</script>

关键实现要点

日期计算处理

vue实现年历

  • 使用dayjs处理日期相关计算
  • 获取每月天数:dayjs().daysInMonth()
  • 获取每月第一天是星期几:dayjs().startOf('month').day()

样式布局

  • 使用CSS Grid布局实现日历网格
  • 月份采用4列响应式布局
  • 日期采用7列布局对应星期

交互功能

  • 支持年份选择
  • 日期点击选中效果
  • 特殊日期标记(今日/事件日)

优化建议

  • 添加周数显示
  • 实现国际化支持
  • 添加月视图/周视图切换
  • 集成拖拽添加事件功能
  • 使用虚拟滚动优化大数据量渲染

以上方案可根据实际需求进行组合或扩展,核心思路是通过日期库处理日历逻辑,Vue负责数据绑定和交互实现。

标签: 年历vue
分享给朋友:

相关文章

vue实现多个tab

vue实现多个tab

Vue 实现多个 Tab 的方法 使用动态组件切换 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现多个…

vue查看更多怎么实现

vue查看更多怎么实现

实现 Vue 的“查看更多”功能 数据截取与显示控制 通过 v-if 或 v-show 控制内容的显示与隐藏。初始状态下只显示部分内容,点击“查看更多”后展开完整内容。 <template&…

vue 实现在线预览

vue 实现在线预览

Vue 实现在线预览的常见方法 使用 iframe 嵌入 通过 iframe 标签可以嵌入多种类型的文件,如 PDF、图片、网页等。需要确保文件地址可访问。 <template>…

vue自己实现下拉导航

vue自己实现下拉导航

实现下拉导航的基本思路 在Vue中实现下拉导航,可以通过结合Vue的响应式数据和事件绑定特性来完成。主要思路是利用v-show或v-if控制下拉菜单的显示与隐藏,并通过鼠标事件或点击事件触发状态变化。…

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 ECh…

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…