当前位置:首页 > 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.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

elementui组件

elementui组件

ElementUI 组件概述 ElementUI 是一套基于 Vue.js 的桌面端组件库,提供丰富的 UI 组件,适用于快速开发中后台系统。以下是其核心组件分类及典型用法。 基础组件 Button…

uniapp组件

uniapp组件

uniapp组件基础概念 uniapp的组件系统基于Vue.js,支持跨平台开发(H5、小程序、App等)。组件分为内置基础组件(如<view>、<button>)和自定义组件…

vue实现盖章组件

vue实现盖章组件

实现Vue盖章组件的步骤 组件结构设计 创建一个Vue组件,包含画布元素和必要的控制按钮。画布用于显示盖章效果,按钮用于触发盖章操作。组件需要接收图片和盖章位置等参数。 <template&g…

vue实现分页组件

vue实现分页组件

Vue 分页组件实现 基础分页组件结构 创建一个名为 Pagination.vue 的文件,包含以下基础模板和逻辑: <template> <div class="pagina…

vue实现列表组件

vue实现列表组件

Vue 列表组件实现方法 基础列表渲染 使用 v-for 指令渲染数组数据,需配合 :key 提升性能: <template> <ul> <li v-for…