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

响应式设计

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

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

vue日历实现

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

相关文章

vue 实现长列表

vue 实现长列表

Vue 实现长列表的优化方案 虚拟滚动技术 虚拟滚动是处理长列表的高效方式,仅渲染可视区域内的元素。通过计算滚动位置动态加载和卸载DOM节点,大幅减少内存占用和渲染压力。 <template&…

vue实现sql操作

vue实现sql操作

在Vue中实现SQL操作通常需要结合后端服务或本地数据库技术,因为前端JavaScript无法直接连接SQL数据库。以下是几种常见的实现方式: 通过API与后端交互 Vue前端通过HTTP请求(如…

vue实现网页切换

vue实现网页切换

Vue 实现网页切换的方法 在 Vue 中实现网页切换通常可以通过以下几种方式完成,具体取决于项目需求和路由管理方式。 使用 Vue Router Vue Router 是 Vue.js 官方提供的…

vue实现条件判断

vue实现条件判断

Vue 条件判断实现方法 Vue 提供了多种方式实现条件判断,可以根据不同场景选择合适的方式。 v-if / v-else-if / v-else 这是 Vue 中最常用的条件渲染指令,根据表达式的…

vue实现录播播放

vue实现录播播放

Vue 实现录播播放 使用 video.js 实现 安装 video.js 和相关的 Vue 适配器: npm install video.js @videojs-player/vue 在 Vue…

vue实现多层评论回复

vue实现多层评论回复

Vue 实现多层评论回复 数据结构设计 多层评论通常采用嵌套结构,每个评论对象包含子评论数组。示例数据结构如下: comments: [ { id: 1, content: '父…