当前位置:首页 > VUE

vue实现日历组件

2026-03-29 06:26:05VUE

实现基础日历结构

使用 Vue 的模板和逻辑分离特性,构建日历的网格布局。通过 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 ['日', '一', '二', '三', '四', '五', '六']" :key="day">{{ day }}</span>
    </div>
    <div class="days">
      <span 
        v-for="(day, index) in days" 
        :key="index"
        :class="{ 'other-month': !day.isCurrentMonth }"
      >
        {{ day.date }}
      </span>
    </div>
  </div>
</template>

处理日期逻辑

通过 JavaScript 的 Date 对象计算月份天数、起始星期,并生成日期数组。使用 computed 属性动态更新数据。

<script>
export default {
  data() {
    return {
      currentYear: new Date().getFullYear(),
      currentMonth: new Date().getMonth(),
    };
  },
  computed: {
    days() {
      const firstDay = new Date(this.currentYear, this.currentMonth, 1).getDay();
      const daysInMonth = new Date(this.currentYear, this.currentMonth + 1, 0).getDate();
      const days = [];

      // 填充上个月末尾的日期
      for (let i = 0; i < firstDay; i++) {
        days.push({ date: '', isCurrentMonth: false });
      }

      // 填充当前月日期
      for (let i = 1; i <= daysInMonth; i++) {
        days.push({ date: i, isCurrentMonth: true });
      }

      return days;
    }
  },
  methods: {
    prevMonth() {
      if (this.currentMonth === 0) {
        this.currentYear--;
        this.currentMonth = 11;
      } else {
        this.currentMonth--;
      }
    },
    nextMonth() {
      if (this.currentMonth === 11) {
        this.currentYear++;
        this.currentMonth = 0;
      } else {
        this.currentMonth++;
      }
    }
  }
};
</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;
}
.weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
  background: #eee;
}
.days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-gap: 5px;
  padding: 5px;
}
.days span {
  padding: 10px;
  text-align: center;
  cursor: pointer;
}
.days span:hover {
  background: #f0f0f0;
}
.other-month {
  color: #ccc;
}
</style>

扩展功能:日期选择与事件标记

通过 v-model 绑定选中的日期,并支持添加自定义事件。新增一个 events 数据对象存储事件信息。

data() {
  return {
    events: {},
    selectedDate: null,
  };
},
methods: {
  selectDate(day) {
    if (day.isCurrentMonth) {
      this.selectedDate = new Date(this.currentYear, this.currentMonth, day.date);
    }
  },
  addEvent(eventText) {
    if (!this.selectedDate) return;
    const dateKey = this.selectedDate.toISOString().split('T')[0];
    this.events[dateKey] = eventText;
  }
}

在模板中添加事件输入框和显示逻辑:

vue实现日历组件

<div class="events">
  <input v-model="newEvent" @keyup.enter="addEvent" placeholder="添加事件" />
  <div v-if="selectedDate">
    {{ events[selectedDate.toISOString().split('T')[0]] || '无事件' }}
  </div>
</div>

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

相关文章

vue哪个组件实现动画

vue哪个组件实现动画

在Vue中实现动画效果,可以通过以下组件和方法实现: Vue内置动画组件 Vue提供了<transition>和<transition-group>两个内置组件,用于处理进入…

vue实现穿梭框树组件

vue实现穿梭框树组件

Vue 穿梭框树组件实现 穿梭框树组件通常结合了树形结构和穿梭框功能,允许用户在左右两栏之间移动树节点数据。以下是基于 Vue 的实现方案: 核心功能设计 数据结构 树形数据通常采用嵌套结构,例如:…

css制作日历

css制作日历

使用CSS Grid布局制作日历 日历的布局适合使用CSS Grid实现,因其天然的行列结构。以下是一个基础日历的HTML和CSS代码: <div class="calendar">…

如何缓存react组件

如何缓存react组件

缓存React组件的方法 React.memo 使用React.memo对函数组件进行浅比较缓存,避免不必要的重新渲染。适用于props不变的场景。 const MemoizedComponen…

vue实现秒表组件

vue实现秒表组件

实现秒表组件的基本思路 使用Vue实现秒表组件需要管理时间状态、控制计时器的启动/暂停/重置功能,并通过计算属性动态显示格式化时间。核心逻辑包括利用setInterval更新计时数据,并通过生命周期钩…

vue 实现toast组件

vue 实现toast组件

实现 Vue Toast 组件的方法 创建基础 Toast 组件 在 Vue 项目中创建一个基础 Toast 组件,通常包含消息内容和显示控制逻辑。 <template> <d…