当前位置:首页 > VUE

vue3实现日历

2026-01-20 17:23:13VUE

Vue3 日历组件实现方法

基础日历结构

使用Vue3的Composition API可以快速构建日历核心逻辑。以下代码展示如何生成当月日历网格:

<script setup>
import { ref, computed } from 'vue'

const currentDate = ref(new Date())
const currentMonth = computed(() => currentDate.value.getMonth())
const currentYear = computed(() => currentDate.value.getFullYear())

const daysInMonth = computed(() => {
  return new Date(currentYear.value, currentMonth.value + 1, 0).getDate()
})

const firstDayOfMonth = computed(() => {
  return new Date(currentYear.value, currentMonth.value, 1).getDay()
})

const calendarDays = computed(() => {
  const days = []
  const prevMonthDays = new Date(currentYear.value, currentMonth.value, 0).getDate()

  // 上个月末尾几天
  for (let i = firstDayOfMonth.value - 1; i >= 0; i--) {
    days.push({ day: prevMonthDays - i, isCurrentMonth: false })
  }

  // 当月所有天
  for (let i = 1; i <= daysInMonth.value; i++) {
    days.push({ day: i, isCurrentMonth: true })
  }

  // 下个月开头几天
  const remainingCells = 42 - days.length
  for (let i = 1; i <= remainingCells; i++) {
    days.push({ day: i, isCurrentMonth: false })
  }

  return days
})
</script>

模板渲染

在模板中使用grid布局展示日历:

vue3实现日历

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">←</button>
      <h2>{{ currentDate.toLocaleString('default', { month: 'long', year: 'numeric' }) }}</h2>
      <button @click="nextMonth">→</button>
    </div>

    <div class="weekdays">
      <div v-for="day in ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']" :key="day">
        {{ day }}
      </div>
    </div>

    <div class="days">
      <div 
        v-for="(dayObj, index) in calendarDays" 
        :key="index"
        :class="{ 'other-month': !dayObj.isCurrentMonth }"
      >
        {{ dayObj.day }}
      </div>
    </div>
  </div>
</template>

样式设计

添加基础样式使日历更美观:

<style scoped>
.calendar {
  width: 350px;
  font-family: Arial;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.weekdays, .days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}

.weekdays div {
  text-align: center;
  font-weight: bold;
}

.days div {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
}

.days div.other-month {
  opacity: 0.3;
}

.days div:hover {
  background-color: #f0f0f0;
  cursor: pointer;
}
</style>

功能扩展

实现月份切换和日期选择功能:

vue3实现日历

const prevMonth = () => {
  currentDate.value = new Date(currentYear.value, currentMonth.value - 1, 1)
}

const nextMonth = () => {
  currentDate.value = new Date(currentYear.value, currentMonth.value + 1, 1)
}

const selectedDate = ref(null)

const selectDate = (dayObj) => {
  if (!dayObj.isCurrentMonth) return
  selectedDate.value = new Date(currentYear.value, currentMonth.value, dayObj.day)
}

高级功能实现

添加事件标记和范围选择:

const events = ref([
  { date: '2023-07-15', title: '会议' },
  { date: '2023-07-20', title: '生日' }
])

const hasEvent = (day) => {
  const dateStr = `${currentYear.value}-${currentMonth.value + 1}-${day}`
  return events.value.some(event => event.date === dateStr)
}

// 模板中修改日期单元格
<div 
  v-for="(dayObj, index) in calendarDays" 
  :key="index"
  :class="{ 
    'other-month': !dayObj.isCurrentMonth,
    'has-event': dayObj.isCurrentMonth && hasEvent(dayObj.day)
  }"
  @click="selectDate(dayObj)"
>
  {{ dayObj.day }}
  <span v-if="dayObj.isCurrentMonth && hasEvent(dayObj.day)" class="event-dot"></span>
</div>

响应式改进

使日历适应不同屏幕尺寸:

@media (max-width: 600px) {
  .calendar {
    width: 100%;
  }

  .days div {
    height: 30px;
  }
}

这个实现包含了日历的核心功能,可以根据需要进一步扩展如周视图、年视图、拖拽添加事件等高级功能。组件化的设计使其可以轻松集成到任何Vue3项目中。

标签: 日历
分享给朋友:

相关文章

vue表格实现日历

vue表格实现日历

Vue 表格实现日历的方法 基础表格结构 使用 el-table 或原生 HTML 表格,通过 v-for 循环生成日历格子。月份天数通过 new Date() 计算,动态渲染表格内容。 &…

vue实现多选日历

vue实现多选日历

Vue 实现多选日历 使用 Vue 实现多选日历功能可以借助现有的日历库或手动构建。以下是两种常见方法: 使用第三方库(如 V-Calendar) 安装 V-Calendar 库: npm ins…

vue如何实现日历

vue如何实现日历

使用第三方库(如 FullCalendar) FullCalendar 是一个功能强大的日历库,支持 Vue 集成。安装依赖后,通过组件的方式引入日历功能。配置事件、日期范围和交互逻辑可以通过 pro…

vue实现值班日历

vue实现值班日历

Vue 实现值班日历的方法 使用 FullCalendar 插件 FullCalendar 是一个功能强大的日历插件,支持 Vue 集成。安装依赖: npm install @fullcalenda…

vue实现钉钉日历

vue实现钉钉日历

使用 Vue 实现钉钉日历功能 安装依赖 需要安装 v-calendar 或 fullcalendar-vue 等日历库。以 v-calendar 为例: npm install v-calenda…