当前位置:首页 > VUE

vue日历实现

2026-01-13 23:54:39VUE

Vue 日历组件实现方法

基础日历实现

使用第三方库如v-calendar快速搭建日历功能:

npm install v-calendar

引入并注册组件:

import VCalendar from 'v-calendar';
Vue.use(VCalendar);

模板中使用:

<template>
  <v-calendar :attributes="attributes"/>
</template>

自定义日历组件

创建基础日历结构:

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">上个月</button>
      <h3>{{ currentMonth }}</h3>
      <button @click="nextMonth">下个月</button>
    </div>
    <div class="days">
      <div v-for="day in days" :key="day">{{ day }}</div>
    </div>
    <div class="dates">
      <div 
        v-for="date in visibleDates"
        :key="date.getTime()"
        @click="selectDate(date)"
      >
        {{ date.getDate() }}
      </div>
    </div>
  </div>
</template>

核心逻辑实现

日期计算逻辑:

vue日历实现

data() {
  return {
    currentDate: new Date(),
    days: ['日', '一', '二', '三', '四', '五', '六']
  }
},
computed: {
  currentMonth() {
    return this.currentDate.toLocaleString('zh-CN', { 
      year: 'numeric', 
      month: 'long' 
    });
  },
  visibleDates() {
    const dates = [];
    const year = this.currentDate.getFullYear();
    const month = this.currentDate.getMonth();

    // 获取当月第一天
    const firstDay = new Date(year, month, 1);
    // 获取当月最后一天
    const lastDay = new Date(year, month + 1, 0);

    // 填充上月日期
    const prevMonthDays = firstDay.getDay();
    for (let i = prevMonthDays - 1; i >= 0; i--) {
      dates.push(new Date(year, month, -i));
    }

    // 填充当月日期
    for (let i = 1; i <= lastDay.getDate(); i++) {
      dates.push(new Date(year, month, i));
    }

    // 填充下月日期
    const nextMonthDays = 6 - lastDay.getDay();
    for (let i = 1; i <= nextMonthDays; i++) {
      dates.push(new Date(year, month + 1, i));
    }

    return dates;
  }
}

事件处理

添加日期选择功能:

methods: {
  prevMonth() {
    this.currentDate = new Date(
      this.currentDate.getFullYear(),
      this.currentDate.getMonth() - 1,
      1
    );
  },
  nextMonth() {
    this.currentDate = new Date(
      this.currentDate.getFullYear(),
      this.currentDate.getMonth() + 1,
      1
    );
  },
  selectDate(date) {
    this.$emit('date-selected', date);
  }
}

样式优化

基础日历样式:

vue日历实现

.calendar {
  width: 350px;
  font-family: Arial;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.days, .dates {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
}
.dates div {
  padding: 10px;
  cursor: pointer;
}
.dates div:hover {
  background: #eee;
}

高级功能扩展

添加事件标记功能:

data() {
  return {
    events: [
      { date: new Date(2023, 5, 15), title: '会议' },
      { date: new Date(2023, 5, 20), title: '生日' }
    ]
  }
},
methods: {
  hasEvent(date) {
    return this.events.some(event => 
      event.date.toDateString() === date.toDateString()
    );
  }
}

模板中添加事件标记:

<div 
  v-for="date in visibleDates"
  :key="date.getTime()"
  :class="{ 'has-event': hasEvent(date) }"
>
  {{ date.getDate() }}
</div>

响应式设计

添加媒体查询适应移动端:

@media (max-width: 600px) {
  .calendar {
    width: 100%;
  }
  .dates div {
    padding: 5px;
  }
}

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

相关文章

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { -…

vue广告实现

vue广告实现

Vue 广告实现方法 在 Vue 项目中实现广告功能可以通过多种方式完成,具体取决于广告类型和需求。以下是几种常见的实现方法: 使用第三方广告平台 许多广告平台如 Google AdSense、百…

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: &l…

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用…

实现.vue文件

实现.vue文件

创建.vue文件的基本结构 一个标准的.vue文件包含三个部分:模板(template)、脚本(script)和样式(style)。这种结构允许开发者在一个文件中管理组件的HTML、JavaScrip…

vue实现水印

vue实现水印

Vue 实现水印的方法 使用 CSS 背景图 通过 CSS 的 background-image 和 background-repeat 属性实现水印效果。创建一个包含水印文本的 Canvas,将其转…