当前位置:首页 > VUE

vue实现日历功能

2026-02-19 14:36:15VUE

基础日历实现

使用Vue实现基础日历功能需要结合日期计算和组件化思想。创建日历组件时,需处理年、月切换并渲染日期格子。

安装依赖(如需要):

npm install date-fns  # 推荐日期处理库

日历组件模板部分:

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">上个月</button>
      <span>{{ currentYear }}年{{ currentMonth }}月</span>
      <button @click="nextMonth">下个月</button>
    </div>
    <div class="weekdays">
      <div v-for="day in weekdays" :key="day">{{ day }}</div>
    </div>
    <div class="days">
      <div 
        v-for="(day, index) in days" 
        :key="index"
        :class="{ 'other-month': !day.isCurrentMonth, 'today': day.isToday }"
      >
        {{ day.date }}
      </div>
    </div>
  </div>
</template>

日期数据处理

在Vue组件的script部分处理日期逻辑:

import { format, startOfMonth, endOfMonth, eachDayOfInterval, isSameMonth, isToday } from 'date-fns'

export default {
  data() {
    return {
      currentDate: new Date(),
      weekdays: ['日', '一', '二', '三', '四', '五', '六']
    }
  },
  computed: {
    currentYear() {
      return format(this.currentDate, 'yyyy')
    },
    currentMonth() {
      return format(this.currentDate, 'MM')
    },
    days() {
      const start = startOfMonth(this.currentDate)
      const end = endOfMonth(this.currentDate)
      const daysArray = eachDayOfInterval({ start, end })

      return daysArray.map(day => ({
        date: format(day, 'd'),
        isCurrentMonth: isSameMonth(day, this.currentDate),
        isToday: isToday(day),
        fullDate: day
      }))
    }
  },
  methods: {
    prevMonth() {
      this.currentDate = new Date(this.currentDate.getFullYear(), this.currentDate.getMonth() - 1, 1)
    },
    nextMonth() {
      this.currentDate = new Date(this.currentDate.getFullYear(), this.currentDate.getMonth() + 1, 1)
    }
  }
}

样式设计

基础日历样式可参考以下CSS:

.calendar {
  width: 350px;
  font-family: Arial;
}

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

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

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

.days div {
  padding: 10px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #eee;
}

.other-month {
  color: #ccc;
}

.today {
  background-color: #4285f4;
  color: white;
  border-radius: 50%;
}

进阶功能实现

事件标记功能

// 在data中添加
events: [
  { date: '2023-06-15', title: '会议' },
  { date: '2023-06-20', title: '生日' }
]

// 修改days计算属性
days() {
  // ...原有代码
  return daysArray.map(day => {
    const formattedDate = format(day, 'yyyy-MM-dd')
    return {
      // ...原有属性
      hasEvent: this.events.some(event => event.date === formattedDate)
    }
  })
}

模板中添加事件标记

<div class="day-content">
  {{ day.date }}
  <span v-if="day.hasEvent" class="event-dot"></span>
</div>

添加点击事件

<div @click="selectDay(day)">...</div>

methods: {
  selectDay(day) {
    this.selectedDate = day.fullDate
    // 可以触发自定义事件或显示模态框
  }
}

第三方库方案

使用现成的Vue日历组件库可以快速实现复杂功能:

  1. 安装vue-cal:

    npm install vue-cal
  2. 基本使用:

    
    import VueCal from 'vue-cal'
    import 'vue-cal/dist/vuecal.css'

export default { components: { VueCal }, data() { return { events: [ { start: '2023-06-15 10:00', end: '2023-06-15 12:00', title: '会议' } ] } } }

vue实现日历功能


```html
<vue-cal
  :time="false"
  :events="events"
  active-view="month"
/>

以上方案提供了从零实现和快速集成两种路径,可根据项目复杂度选择适合的方式。自定义实现更适合需要高度定制化的场景,而第三方库适合快速开发标准日历功能。

标签: 日历功能
分享给朋友:

相关文章

vue实现日历

vue实现日历

Vue 实现日历组件 使用 Vue 实现日历组件可以通过自定义开发或借助第三方库完成。以下是两种常见方法: 方法一:自定义日历组件 创建基础日历结构,利用 Vue 的响应式特性动态渲染日期。核心逻…

vue实现插件功能

vue实现插件功能

Vue 插件实现方法 安装与注册插件 在 Vue 项目中,插件通常以独立模块形式存在。通过 Vue.use() 方法注册插件,该方法会自动调用插件的 install 函数。 // 引入插件 imp…

vue实现修改功能

vue实现修改功能

Vue 实现修改功能的方法 数据绑定与表单处理 使用v-model实现双向数据绑定,快速获取表单输入值。适用于表单类修改场景: <template> <input v-mode…

js 实现日历

js 实现日历

实现日历的基本思路 日历的核心功能是展示日期,并允许用户进行日期选择或导航。JavaScript 可以动态生成日历的 HTML 结构,并处理用户交互逻辑。 基础日历结构 日历通常包含头部(显示月份和…

vue分类功能实现

vue分类功能实现

Vue分类功能实现方法 使用v-for指令渲染分类列表 在Vue模板中使用v-for循环渲染分类数据,结合v-bind动态绑定分类ID或其他属性 <div v-for="category in…

vue拖拽功能实现

vue拖拽功能实现

Vue 拖拽功能实现方法 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,通过 draggable 属性和相关事件实现基础拖拽功能。 <template>…