当前位置:首页 > VUE

vue 日历组件实现

2026-01-19 01:50:18VUE

Vue 日历组件实现

基本结构搭建

使用Vue的单文件组件(SFC)结构,创建一个基础的日历组件框架。需要定义月份、年份的状态管理,以及日期渲染逻辑。

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">上一月</button>
      <h2>{{ currentYear }}年{{ currentMonth }}月</h2>
      <button @click="nextMonth">下一月</button>
    </div>
    <div class="days-grid">
      <div v-for="day in days" :key="day" class="day-header">{{ day }}</div>
      <div 
        v-for="date in visibleDates" 
        :key="date.getTime()"
        class="date-cell"
        :class="{ 'other-month': !isCurrentMonth(date) }"
      >
        {{ date.getDate() }}
      </div>
    </div>
  </div>
</template>

核心逻辑实现

需要计算当前月份的所有日期,包括上个月和下个月的部分日期以确保日历网格完整。

vue 日历组件实现

<script>
export default {
  data() {
    return {
      currentDate: new Date(),
      days: ['日', '一', '二', '三', '四', '五', '六']
    }
  },
  computed: {
    currentYear() {
      return this.currentDate.getFullYear()
    },
    currentMonth() {
      return this.currentDate.getMonth() + 1
    },
    visibleDates() {
      const dates = []
      const firstDay = new Date(this.currentYear, this.currentDate.getMonth(), 1)
      const lastDay = new Date(this.currentYear, this.currentDate.getMonth() + 1, 0)

      // 上个月的最后几天
      const prevMonthDays = firstDay.getDay()
      for (let i = prevMonthDays - 1; i >= 0; i--) {
        const date = new Date(firstDay)
        date.setDate(date.getDate() - (i + 1))
        dates.push(date)
      }

      // 当前月的所有日期
      for (let i = 1; i <= lastDay.getDate(); i++) {
        dates.push(new Date(this.currentYear, this.currentDate.getMonth(), i))
      }

      // 下个月的前几天
      const nextMonthDays = 6 - lastDay.getDay()
      for (let i = 1; i <= nextMonthDays; i++) {
        const date = new Date(lastDay)
        date.setDate(date.getDate() + i)
        dates.push(date)
      }

      return dates
    }
  },
  methods: {
    isCurrentMonth(date) {
      return date.getMonth() === this.currentDate.getMonth()
    },
    prevMonth() {
      this.currentDate = new Date(this.currentYear, this.currentDate.getMonth() - 1, 1)
    },
    nextMonth() {
      this.currentDate = new Date(this.currentYear, this.currentDate.getMonth() + 1, 1)
    }
  }
}
</script>

样式设计

为日历添加基本样式,确保布局合理美观。

vue 日历组件实现

<style scoped>
.calendar {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
  font-family: Arial, sans-serif;
}

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

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

.day-header {
  text-align: center;
  font-weight: bold;
  padding: 5px;
}

.date-cell {
  text-align: center;
  padding: 10px;
  border: 1px solid #eee;
  cursor: pointer;
}

.date-cell:hover {
  background-color: #f5f5f5;
}

.other-month {
  color: #ccc;
}
</style>

功能扩展

可以添加日期选择功能,标记特定日期等高级功能。

// 在methods中添加
selectDate(date) {
  this.$emit('date-selected', date)
}

// 在模板中修改date-cell
<div 
  v-for="date in visibleDates" 
  :key="date.getTime()"
  class="date-cell"
  :class="{ 
    'other-month': !isCurrentMonth(date),
    'selected': isSelected(date)
  }"
  @click="selectDate(date)"
>
  {{ date.getDate() }}
</div>

// 添加isSelected方法
isSelected(date) {
  return this.selectedDate && 
         date.getDate() === this.selectedDate.getDate() &&
         date.getMonth() === this.selectedDate.getMonth() &&
         date.getFullYear() === this.selectedDate.getFullYear()
}

第三方库集成

对于更复杂的需求,可以考虑使用成熟的日历库如v-calendar或fullcalendar-vue。

npm install v-calendar
import VCalendar from 'v-calendar'

// 在main.js中
Vue.use(VCalendar)

// 组件中使用
<template>
  <v-calendar />
</template>

标签: 组件日历
分享给朋友:

相关文章

css制作日历

css制作日历

使用CSS Grid布局制作日历 日历的布局适合使用CSS Grid实现,因其天然的行列结构。以下是一个基础日历的HTML和CSS代码: <div class="calendar">…

组件elementui

组件elementui

ElementUI 简介 ElementUI 是一套基于 Vue.js 的桌面端组件库,由饿了么前端团队开发并开源。它提供了丰富的 UI 组件(如按钮、表单、表格等),适合快速构建企业级中后台产品。…

vue实现tooltips组件

vue实现tooltips组件

实现基础 Tooltip 组件 创建一个名为 Tooltip.vue 的单文件组件,包含模板、样式和逻辑: <template> <div class="tooltip-co…

vue 实现toast组件

vue 实现toast组件

实现 Vue Toast 组件的方法 创建基础 Toast 组件 在 Vue 项目中创建一个基础 Toast 组件,通常包含消息内容和显示控制逻辑。 <template> <…

vue实现日历插件

vue实现日历插件

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

vue 滑杆组件实现

vue 滑杆组件实现

实现基础滑杆组件 使用Vue的v-model和原生<input type="range">实现基础功能: <template> <div> <…