当前位置:首页 > 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布局展示日历:

<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>

功能扩展

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

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项目中。

vue3实现日历

标签: 日历
分享给朋友:

相关文章

vue VUE实现日历组件功能

vue VUE实现日历组件功能

Vue 日历组件实现方法 基础日历结构 使用Vue的模板语法构建日历的HTML结构,通常包含头部(年月显示、切换按钮)和日历表格(星期标题和日期格子)。 <template> <…

vue页面实现日历

vue页面实现日历

实现基础日历布局 使用Vue的模板语法构建日历的基本HTML结构,通常需要包含星期标题和日期格子。月份切换按钮可以通过v-on绑定事件。 <template> <div cl…

uniapp日历视图

uniapp日历视图

uniapp日历视图实现方法 在uniapp中实现日历视图可以通过多种方式完成,包括使用第三方组件库或自行开发。以下是几种常见方法: 使用uni-calendar组件 uni-calendar是u…

vue实现日历组件

vue实现日历组件

Vue 日历组件实现步骤 基础结构搭建 使用Vue 3的Composition API或Options API创建组件框架。核心结构包括年份/月份切换按钮、星期标题栏和日期网格。 <temp…

vue实现日历插件

vue实现日历插件

Vue 日历插件实现方案 使用现成组件库 推荐直接使用成熟的 Vue 日历组件,如: V-Calendar:专为 Vue 设计的轻量级日历组件 FullCalendar:功能强大的日历库,有 Vu…

vue实现日历效果

vue实现日历效果

Vue实现日历效果的方法 使用第三方库 Vue生态中有多个成熟的日历组件库,如v-calendar或vue2-calendar,可以快速实现日历功能。安装后直接引入组件即可使用。 npm inst…