当前位置:首页 > VUE

vue实现日历效果

2026-02-18 09:17:54VUE

实现基础日历布局

使用Vue的模板语法构建日历的基本HTML结构,通常包括星期标题栏和日期格子。通过v-for循环生成日期单元格,动态绑定样式类以标记当前月、非当前月或选中日期。

<template>
  <div class="calendar">
    <div class="weekdays">
      <span v-for="day in weekdays" :key="day">{{ day }}</span>
    </div>
    <div class="days">
      <span 
        v-for="(date, index) in visibleDates" 
        :key="index"
        :class="{ 'other-month': !date.isCurrentMonth, 'today': date.isToday }"
        @click="selectDate(date)"
      >
        {{ date.day }}
      </span>
    </div>
  </div>
</template>

处理日期数据逻辑

在Vue组件的datacomputed中定义日期生成逻辑。计算当前月份的首日位置和总天数,生成包含日期对象(含月份标记)的数组。

export default {
  data() {
    return {
      currentDate: new Date(),
      weekdays: ['日', '一', '二', '三', '四', '五', '六']
    }
  },
  computed: {
    visibleDates() {
      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);

      // 生成当前月日期
      let dates = [];
      for (let day = 1; day <= lastDay.getDate(); day++) {
        dates.push({
          day,
          date: new Date(year, month, day),
          isCurrentMonth: true
        });
      }

      // 补全上月和下月日期
      const startDay = firstDay.getDay();
      const endDay = lastDay.getDay();
      const prevMonthDays = new Date(year, month, 0).getDate();

      for (let i = 0; i < startDay; i++) {
        dates.unshift({
          day: prevMonthDays - i,
          date: new Date(year, month - 1, prevMonthDays - i),
          isCurrentMonth: false
        });
      }

      for (let i = 1; i <= 6 - endDay; i++) {
        dates.push({
          day: i,
          date: new Date(year, month + 1, i),
          isCurrentMonth: false
        });
      }

      return dates;
    }
  }
}

添加交互功能

实现月份切换和日期选择功能,通过方法更新currentDate触发响应式重新计算。添加事件处理函数处理用户点击日期时的逻辑。

vue实现日历效果

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.selectedDate = date;
    // 触发自定义事件或处理业务逻辑
    this.$emit('date-selected', date);
  }
}

样式优化

使用CSS Grid布局实现日历的整齐排列,添加悬停和选中状态样式提升用户体验。

.calendar {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
}
.weekdays, .days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 8px;
}
.days span {
  padding: 8px;
  text-align: center;
  cursor: pointer;
  border-radius: 4px;
}
.days span:hover {
  background-color: #f0f0f0;
}
.other-month {
  color: #ccc;
}
.today {
  background-color: #e3f2fd;
}
.selected {
  background-color: #2196f3;
  color: white;
}

进阶功能扩展

添加事件标记 在日期对象中增加事件数据,通过模板条件渲染显示事件标记点。

vue实现日历效果

dates = dates.map(date => ({
  ...date,
  hasEvent: checkIfHasEvent(date.date) // 自定义事件检查函数
}));
<span v-if="date.hasEvent" class="event-dot"></span>

支持范围选择 通过记录开始和结束日期,在计算属性中判断每个日期是否处于选中范围内,动态添加样式类。

isInRange(date) {
  if (!this.startDate || !this.endDate) return false;
  return date >= this.startDate && date <= this.endDate;
}

国际化支持 动态切换星期显示和月份名称,使用Intl.DateTimeFormatAPI根据语言环境格式化日期。

const formatter = new Intl.DateTimeFormat(locale, { weekday: 'short' });
this.weekdays = Array.from({ length: 7 }, (_, i) => 
  formatter.format(new Date(2023, 0, i + 1))
);

标签: 日历效果
分享给朋友:

相关文章

vue实现效果展示

vue实现效果展示

Vue 实现效果展示的方法 动态数据绑定展示 通过 Vue 的响应式特性,实时展示数据变化。例如,表单输入与预览同步: <template> <div> <…

jquery效果

jquery效果

jQuery 动画效果 jQuery 提供了多种内置动画效果,可用于实现平滑的页面交互。常用的方法包括 show()、hide()、toggle()、fadeIn()、fadeOut()、slideU…

vue实现翻页效果

vue实现翻页效果

使用Vue实现翻页效果 在Vue中实现翻页效果通常需要结合分页组件和数据处理逻辑。以下是几种常见方法: 基础分页组件实现 <template> <div> <…

vue实现检索效果

vue实现检索效果

Vue实现检索功能的方法 在Vue中实现检索功能通常需要结合数据绑定、计算属性和方法。以下是几种常见的实现方式: 基础实现(基于计算属性) <template> <div>…

vue 实现toggle效果

vue 实现toggle效果

使用 v-show 实现 Toggle 通过 v-show 指令可以控制元素的显示与隐藏,适合频繁切换的场景。v-show 仅切换 CSS 的 display 属性,不会销毁和重建 DOM。 <…

vue 实现过渡效果

vue 实现过渡效果

Vue 过渡效果实现方法 Vue 提供了 <transition> 和 <transition-group> 组件,用于在元素插入、更新或移除时应用动画效果。以下是具体实现方式…