当前位置:首页 > VUE

vue VUE实现日历组件功能

2026-01-14 00:46:16VUE

Vue 日历组件实现方法

基础日历结构

使用Vue的模板语法构建日历的HTML结构,通常包含头部(年月显示、切换按钮)和日历表格(星期标题和日期格子)。

<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 }"
        @click="selectDay(day)"
      >
        {{ day.date }}
      </div>
    </div>
  </div>
</template>

日期数据处理

计算当前月份的天数和前后月份的补全日期,确保日历表格完整显示6行。

data() {
  return {
    currentDate: new Date(),
    weekdays: ['日', '一', '二', '三', '四', '五', '六']
  }
},
computed: {
  days() {
    const year = this.currentDate.getFullYear()
    const month = this.currentDate.getMonth()
    const firstDay = new Date(year, month, 1)
    const lastDay = new Date(year, month + 1, 0)
    const days = []

    // 上个月补全天数
    const prevMonthDays = firstDay.getDay()
    for (let i = prevMonthDays - 1; i >= 0; i--) {
      days.push({
        date: new Date(year, month, -i).getDate(),
        isCurrentMonth: false
      })
    }

    // 当月天数
    for (let i = 1; i <= lastDay.getDate(); i++) {
      days.push({
        date: i,
        isCurrentMonth: true,
        fullDate: new Date(year, month, i)
      })
    }

    // 下个月补全天数
    const nextMonthDays = 42 - days.length
    for (let i = 1; i <= nextMonthDays; i++) {
      days.push({
        date: i,
        isCurrentMonth: false
      })
    }

    return days
  },
  currentYear() {
    return this.currentDate.getFullYear()
  },
  currentMonth() {
    return this.currentDate.getMonth() + 1
  }
}

月份切换功能

实现上个月和下个月的切换逻辑,更新currentDate数据。

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
    )
  },
  selectDay(day) {
    if (day.isCurrentMonth) {
      console.log('选择的日期:', day.fullDate)
    }
  }
}

样式设计

添加基础CSS样式,使日历显示美观。

.calendar {
  width: 350px;
  font-family: Arial;
}
.header {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  background: #f0f0f0;
}
.weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  background: #e0e0e0;
}
.days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-gap: 1px;
  background: #ccc;
}
.days > div {
  padding: 10px;
  background: white;
  height: 40px;
}
.other-month {
  color: #aaa;
}

进阶功能扩展

可根据需求添加以下功能:

  • 日期范围选择
  • 标记特殊日期
  • 多语言支持
  • 事件显示
  • 移动端适配
// 示例:标记今天
computed: {
  days() {
    // ...原有代码
    const today = new Date()
    days.forEach(day => {
      if (day.fullDate && 
          day.fullDate.getDate() === today.getDate() &&
          day.fullDate.getMonth() === today.getMonth() &&
          day.fullDate.getFullYear() === today.getFullYear()) {
        day.isToday = true
      }
    })
    return days
  }
}
/* 添加今天样式 */
.today {
  background: #ffeb3b !important;
  font-weight: bold;
}

vue VUE实现日历组件功能

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

相关文章

vue 组件实现

vue 组件实现

Vue 组件实现方法 单文件组件 (SFC) 使用 .vue 文件格式,包含模板、脚本和样式三部分: <template> <div class="example">{{…

vue页面实现日历

vue页面实现日历

Vue 页面实现日历的方法 使用第三方组件库 推荐使用成熟的日历组件库,如 v-calendar 或 fullcalendar-vue,快速实现功能丰富的日历。 安装 v-calendar:…

vue轮播组件实现

vue轮播组件实现

Vue 轮播组件实现方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template&g…

vue实现折叠组件

vue实现折叠组件

Vue 折叠组件实现方法 基于 v-show 和事件触发 通过 v-show 控制内容的显示与隐藏,结合点击事件切换状态。 <template> <div>…

vue实现组件封装

vue实现组件封装

Vue 组件封装方法 封装组件的基本步骤 创建独立的 .vue 文件,包含 template、script 和 style 三个部分。通过 props 接收父组件传递的数据,使用 $emit 触发自…

vue radio组件实现

vue radio组件实现

Vue Radio 组件实现 在 Vue 中实现 Radio 组件可以通过原生 HTML 的 <input type="radio"> 结合 Vue 的响应式特性,或者使用第三方 UI 库…