当前位置:首页 > VUE

vue 实现日历

2026-03-07 07:06:04VUE

实现基础日历结构

使用 Vue 的模板和计算属性构建日历骨架。通过 v-for 循环渲染月份和日期格子,动态绑定样式类控制选中状态。

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">上一月</button>
      <span>{{ currentYear }}年{{ currentMonth + 1 }}月</span>
      <button @click="nextMonth">下一月</button>
    </div>
    <div class="days">
      <div v-for="day in ['日', '一', '二', '三', '四', '五', '六']" :key="day">{{ day }}</div>
    </div>
    <div class="dates">
      <div 
        v-for="date in visibleDates" 
        :key="date.date"
        :class="{ 'other-month': !date.isCurrentMonth, 'selected': date.isSelected }"
        @click="selectDate(date)"
      >
        {{ date.day }}
      </div>
    </div>
  </div>
</template>

处理日期逻辑

通过 JavaScript 的 Date 对象计算当前月份的天数、上月残留天数及下月补全天数,生成完整的日历数组。

<script>
export default {
  data() {
    return {
      currentDate: new Date(),
      selectedDate: null
    };
  },
  computed: {
    currentYear() {
      return this.currentDate.getFullYear();
    },
    currentMonth() {
      return this.currentDate.getMonth();
    },
    visibleDates() {
      const year = this.currentYear;
      const month = this.currentMonth;
      const firstDay = new Date(year, month, 1).getDay();
      const daysInMonth = new Date(year, month + 1, 0).getDate();
      const daysFromPrevMonth = firstDay;
      const daysFromNextMonth = 6 - new Date(year, month, daysInMonth).getDay();

      const dates = [];

      // 上月日期
      const prevMonthDays = new Date(year, month, 0).getDate();
      for (let i = daysFromPrevMonth - 1; i >= 0; i--) {
        dates.push({
          day: prevMonthDays - i,
          isCurrentMonth: false,
          date: new Date(year, month - 1, prevMonthDays - i)
        });
      }

      // 当月日期
      for (let i = 1; i <= daysInMonth; i++) {
        dates.push({
          day: i,
          isCurrentMonth: true,
          date: new Date(year, month, i)
        });
      }

      // 下月日期
      for (let i = 1; i <= daysFromNextMonth; i++) {
        dates.push({
          day: i,
          isCurrentMonth: false,
          date: new Date(year, month + 1, i)
        });
      }

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

添加样式优化

通过 CSS 美化日历布局,高亮当前选中日期和非当月日期。

vue 实现日历

<style scoped>
.calendar {
  width: 300px;
  border: 1px solid #eee;
  font-family: Arial, sans-serif;
}
.header {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  background: #f5f5f5;
}
.days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
  background: #f0f0f0;
}
.dates {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
}
.dates div {
  padding: 10px;
  cursor: pointer;
}
.other-month {
  color: #ccc;
}
.selected {
  background: #42b983;
  color: white;
}
</style>

扩展功能建议

  • 事件标记:在日期格子中显示自定义事件图标或文字。
  • 范围选择:通过 mouseentermouseleave 事件实现日期区间选择。
  • 国际化:支持多语言星期显示,使用 Intl.DateTimeFormat 处理。

完整代码可结合 Vue 3 的 Composition API 重构,逻辑更清晰。

标签: 日历vue
分享给朋友:

相关文章

vue实现分支

vue实现分支

Vue 实现分支的方法 在 Vue 项目中实现分支功能通常涉及条件渲染、动态组件或路由控制。以下是几种常见的实现方式: 条件渲染(v-if/v-show) 使用 Vue 的指令根据条件显示不同内容:…

vue 实现pc

vue 实现pc

Vue 实现 PC 端应用的核心要点 Vue.js 是一个渐进式框架,适合构建 PC 端单页应用(SPA)或复杂的前端界面。以下是实现 PC 端应用的关键方法和注意事项: 项目初始化与工程化配置 使…

vue tag实现

vue tag实现

Vue 标签实现方法 在 Vue 中实现标签功能可以通过多种方式完成,常见场景包括动态标签生成、标签输入框、标签管理等。以下是几种典型实现方案: 动态标签列表渲染 使用 v-for 指令渲染标签数组…

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式数…

vue实现剪辑

vue实现剪辑

Vue 实现视频剪辑功能 在Vue中实现视频剪辑功能通常需要结合HTML5的<video>元素和第三方库(如FFmpeg.js或Video.js)。以下是几种常见的实现方法: 使用FFm…

vue实现token

vue实现token

Vue 中实现 Token 认证的方法 在 Vue 中实现 Token 认证通常涉及前端与后端的交互,包括 Token 的获取、存储、发送和刷新等操作。以下是具体实现步骤: 获取 Token 用户登…