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

核心逻辑实现

日期计算逻辑:

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);
  }
}

样式优化

基础日历样式:

.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>

响应式设计

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

vue日历实现

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

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

相关文章

jq 实现 vue

jq 实现 vue

jq 实现 Vue 的核心功能 jq 是一个轻量级的 JavaScript 库,主要用于 DOM 操作和事件处理。虽然它不能完全替代 Vue,但可以通过一些技巧模拟 Vue 的核心功能,如数据绑定、事…

vue实现删除

vue实现删除

Vue 实现删除功能的方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 数据绑定与列表渲染 使用 v-for 指令渲染列表数据,并为每个项绑定唯一标识符(如 id)。例如: <ul&g…

vue页面实现

vue页面实现

Vue 页面实现方法 创建 Vue 组件 使用 Vue 单文件组件(SFC)方式创建页面,文件扩展名为 .vue。一个基本的 Vue 组件包含三个部分:模板(template)、脚本(script)和…

vue 实现筛选

vue 实现筛选

实现筛选功能的基本方法 在Vue中实现筛选功能通常涉及以下几个核心步骤: 数据绑定与筛选逻辑 使用v-model绑定筛选条件到Vue实例的数据属性,结合计算属性实现动态筛选: data() {…

vue 实现后退

vue 实现后退

实现后退功能的方法 在Vue中实现后退功能通常需要结合浏览器历史记录API或Vue Router的导航方法。以下是几种常见的实现方式: 使用Vue Router的go方法 this.$router…

vue实现增删

vue实现增删

Vue 实现增删功能 在 Vue 中实现增删功能通常涉及数据绑定、事件处理和列表渲染。以下是基于 Vue 2 或 Vue 3 的实现方法: 数据定义 初始化一个数组用于存储列表数据,并在 Vue 实…