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

vue实现toggle

Vue 实现 Toggle 功能 在 Vue 中实现 toggle(切换)功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 和 v-on 通过 v-model 绑定数据,结合…

vue实现画板

vue实现画板

Vue 实现画板功能 在 Vue 中实现画板功能可以通过 HTML5 的 Canvas API 实现。以下是一个完整的实现方案: 创建 Vue 组件 新建一个 Vue 单文件组件(SFC),命名为…

vue实现rgb调色

vue实现rgb调色

Vue实现RGB调色功能 实现思路 通过Vue的数据绑定和事件处理,动态调整RGB三个通道的值,实时更新显示颜色。 核心代码示例 <template> <div class=…

vue实现流程转化

vue实现流程转化

Vue 实现流程转化 在 Vue 中实现流程转化通常涉及多个步骤,包括状态管理、组件通信和动态渲染。以下是几种常见的实现方法: 使用 Vuex 进行状态管理 Vuex 是 Vue 的官方状态管理库,…

vue实现列表循环

vue实现列表循环

Vue 列表循环的实现方法 在 Vue 中,可以通过 v-for 指令实现列表循环渲染。以下是几种常见的实现方式: 基础列表渲染 <template> <ul>…

vue界面实现滚动

vue界面实现滚动

实现滚动的基本方法 在Vue中实现滚动效果可以通过多种方式完成,包括使用原生CSS、JavaScript或第三方库。以下是一些常见的实现方法: 使用CSS实现滚动 通过CSS的overflow属性可…