当前位置:首页 > 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>

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

相关文章

vue实现穿梭框树组件

vue实现穿梭框树组件

Vue 穿梭框树组件实现 穿梭框树组件通常结合了树形结构和穿梭框功能,允许用户在左右两栏之间移动树节点数据。以下是基于 Vue 的实现方案: 核心功能设计 数据结构 树形数据通常采用嵌套结构,例如:…

实现vue组件

实现vue组件

创建 Vue 组件 Vue 组件可以通过单文件组件(.vue 文件)或直接在 JavaScript 中定义。以下是两种常见实现方式。 单文件组件方式 单文件组件包含模板、脚本和样式三部分,适合复杂项…

elementui组件

elementui组件

ElementUI 组件概述 ElementUI 是一套基于 Vue.js 的桌面端组件库,提供丰富的 UI 组件,适用于快速开发中后台系统。以下是其核心组件分类及典型用法。 基础组件 Button…

vue实现组件跟随

vue实现组件跟随

Vue 实现组件跟随效果 实现组件跟随效果通常需要监听鼠标或触摸事件,动态更新组件位置。以下是几种常见实现方式: 使用鼠标事件绑定 <template> <div class=…

vue 实现toast组件

vue 实现toast组件

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

vue实现盖章组件

vue实现盖章组件

实现Vue盖章组件的步骤 组件结构设计 创建一个Vue组件,包含画布元素和必要的控制按钮。画布用于显示盖章效果,按钮用于触发盖章操作。组件需要接收图片和盖章位置等参数。 <template&g…