当前位置:首页 > VUE

vue 实现日历

2026-01-12 23:56:22VUE

实现基础日历结构

使用 Vue 的模板语法构建日历的网格布局,通常以 7 列(一周)和 5-6 行(月份天数)的表格形式展示。通过 v-for 循环渲染日期单元格,动态绑定样式和事件。

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

处理日期逻辑

通过 JavaScript 的 Date 对象计算当前月份的天数、起始星期以及上个月/下个月的残留日期。核心逻辑包括:

  1. 获取月份第一天和最后一天的星期数。
  2. 生成包含当前月、上月残留和下月补全的日期数组。
<script>
export default {
  data() {
    return {
      currentDate: new Date(),
      weekdays: ['日', '一', '二', '三', '四', '五', '六']
    };
  },
  computed: {
    currentYear() {
      return this.currentDate.getFullYear();
    },
    currentMonth() {
      return this.currentDate.getMonth();
    },
    days() {
      const year = this.currentYear;
      const month = this.currentMonth;
      const firstDay = new Date(year, month, 1).getDay();
      const lastDate = new Date(year, month + 1, 0).getDate();
      const prevLastDate = new Date(year, month, 0).getDate();

      const days = [];
      // 上月残留日期
      for (let i = firstDay - 1; i >= 0; i--) {
        days.push({
          date: prevLastDate - i,
          isCurrentMonth: false
        });
      }
      // 当前月日期
      for (let i = 1; i <= lastDate; i++) {
        const today = new Date();
        days.push({
          date: i,
          isCurrentMonth: true,
          isToday: today.getDate() === i && 
                   today.getMonth() === month && 
                   today.getFullYear() === year
        });
      }
      // 下月补全日期
      const remaining = 42 - days.length;
      for (let i = 1; i <= remaining; i++) {
        days.push({
          date: i,
          isCurrentMonth: false
        });
      }
      return days;
    }
  },
  methods: {
    prevMonth() {
      this.currentDate = new Date(this.currentYear, this.currentMonth - 1, 1);
    },
    nextMonth() {
      this.currentDate = new Date(this.currentYear, this.currentMonth + 1, 1);
    },
    selectDay(day) {
      if (day.isCurrentMonth) {
        console.log(`选中日期: ${this.currentYear}-${this.currentMonth + 1}-${day.date}`);
      }
    }
  }
};
</script>

添加样式优化

通过 CSS 美化日历布局,区分当前月、非当前月及今日的样式。

<style scoped>
.calendar {
  width: 300px;
  border: 1px solid #eee;
  font-family: Arial, sans-serif;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  background: #f5f5f5;
}
.weekdays, .days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
}
.weekdays {
  background: #ddd;
  padding: 5px 0;
}
.days span {
  padding: 10px;
  cursor: pointer;
}
.other-month {
  color: #ccc;
}
.today {
  background: #4CAF50;
  color: white;
  border-radius: 50%;
}
</style>

扩展功能建议

  1. 事件标记:在日期单元格中显示自定义事件图标或文字。
  2. 范围选择:支持通过鼠标拖动选择日期范围。
  3. 国际化:根据地区动态切换星期显示顺序(如周一为首日)。
  4. 服务端集成:结合 API 拉取节假日或日程数据。

通过上述步骤,可实现一个基础的交互式 Vue 日历组件,后续可根据需求进一步扩展功能。

vue 实现日历

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

相关文章

vue实现atm

vue实现atm

Vue实现ATM机模拟功能 使用Vue.js实现ATM机模拟功能需要结合组件化开发、状态管理以及用户交互逻辑。以下是关键实现步骤: 核心组件结构 创建以下Vue组件构建ATM界面: Car…

vue实现屏幕共享

vue实现屏幕共享

Vue 实现屏幕共享的方法 使用 WebRTC 和 getDisplayMedia API 通过 navigator.mediaDevices.getDisplayMedia 捕获屏幕内容,结合 We…

项目基于vue实现

项目基于vue实现

基于Vue实现项目的关键方法 搭建基础环境 安装Node.js和npm/yarn,确保开发环境具备运行Vue的能力。通过Vue CLI快速初始化项目结构,选择所需配置(如Babel、Router、Vu…

vue实现微博发布动态

vue实现微博发布动态

使用Vue实现微博发布动态功能 创建Vue组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="we…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组…

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…