当前位置:首页 > VUE

vue实现年历标记

2026-01-07 04:10:30VUE

实现年历标记的基本思路

在Vue中实现年历标记功能,通常需要结合日期处理库(如dayjsdate-fns)和动态渲染逻辑。核心步骤包括生成全年日期数据、标记特定日期、以及渲染交互式日历界面。

安装依赖库

推荐使用dayjs处理日期:

npm install dayjs

生成全年日期数据

通过dayjs生成当前年份的月份和日期数据:

vue实现年历标记

import dayjs from 'dayjs';

export default {
  data() {
    return {
      currentYear: dayjs().year(),
      markedDates: ['2023-05-20', '2023-11-15'] // 示例标记日期
    }
  },
  computed: {
    yearCalendar() {
      const months = [];
      for (let month = 0; month < 12; month++) {
        const daysInMonth = dayjs().year(this.currentYear).month(month).daysInMonth();
        const monthDays = [];

        for (let day = 1; day <= daysInMonth; day++) {
          const date = dayjs().year(this.currentYear).month(month).date(day);
          monthDays.push({
            date: date.format('YYYY-MM-DD'),
            isMarked: this.markedDates.includes(date.format('YYYY-MM-DD'))
          });
        }

        months.push({
          monthName: dayjs().month(month).format('MMMM'),
          days: monthDays
        });
      }
      return months;
    }
  }
}

渲染日历界面

使用v-for渲染月份和日期格子,并通过CSS标记特殊日期:

<template>
  <div class="year-calendar">
    <div v-for="(month, index) in yearCalendar" :key="index" class="month">
      <h3>{{ month.monthName }}</h3>
      <div class="days-grid">
        <div 
          v-for="(day, dayIndex) in month.days" 
          :key="dayIndex"
          :class="['day', { 'marked': day.isMarked }]"
        >
          {{ dayjs(day.date).date() }}
        </div>
      </div>
    </div>
  </div>
</template>

<style>
.year-calendar {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

.month {
  border: 1px solid #eee;
  padding: 10px;
}

.days-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}

.day {
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.marked {
  background-color: #ffeb3b;
  border-radius: 50%;
}
</style>

添加交互功能

实现日期点击标记/取消标记:

vue实现年历标记

methods: {
  toggleMark(date) {
    const index = this.markedDates.indexOf(date);
    if (index === -1) {
      this.markedDates.push(date);
    } else {
      this.markedDates.splice(index, 1);
    }
  }
}

在模板中添加点击事件:

<div 
  @click="toggleMark(day.date)"
  :class="['day', { 'marked': day.isMarked }]"
>
  {{ dayjs(day.date).date() }}
</div>

高级功能扩展

支持不同类型的标记(如节假日、纪念日):

data() {
  return {
    markTypes: {
      holiday: { color: '#f44336', text: '休' },
      event: { color: '#4caf50', text: '事' }
    },
    customMarks: {
      '2023-10-01': { type: 'holiday', note: '国庆节' },
      '2023-08-08': { type: 'event', note: '项目截止' }
    }
  }
}

渲染时根据类型显示不同样式:

<div 
  v-for="(day, dayIndex) in month.days" 
  :key="dayIndex"
  :class="['day', customMarks[day.date]?.type]"
  :style="{ backgroundColor: customMarks[day.date] ? markTypes[customMarks[day.date].type].color : '' }"
>
  {{ dayjs(day.date).date() }}
  <span v-if="customMarks[day.date]">
    {{ markTypes[customMarks[day.date].type].text }}
  </span>
</div>

标签: 年历标记
分享给朋友:

相关文章

vue实现年历

vue实现年历

实现年历的Vue组件 使用Vue实现年历功能可以通过以下方式完成,主要利用Vue的响应式特性和计算属性动态生成日历数据。 核心实现思路 创建Vue组件管理当前年份和月份数据,通过计算属性生成日历矩阵…

vue实现检索内容标记

vue实现检索内容标记

Vue 实现检索内容标记的方法 在 Vue 中实现检索内容标记功能,可以通过以下步骤完成。核心思路是利用正则表达式匹配检索关键词,并使用 v-html 或自定义指令动态渲染高亮内容。 方法一:使用…

vue实现年历标记

vue实现年历标记

实现年历标记的方法 在Vue中实现年历标记可以通过以下步骤完成。这里以使用第三方库v-calendar为例,该库提供了丰富的日历功能,支持日期标记、范围选择等。 安装v-calendar 通过npm…

vue实现文本标记

vue实现文本标记

Vue 实现文本标记的方法 使用 v-html 指令 在 Vue 中可以通过 v-html 指令动态渲染 HTML 内容。例如,将需要标记的文本包裹在 <mark> 标签中: <t…

vue实现检索内容标记

vue实现检索内容标记

实现检索内容高亮标记的方法 在Vue中实现检索内容的高亮标记,可以通过以下方法实现: 使用v-html指令和正则表达式替换 创建一个方法,用于将匹配的检索词替换为带有高亮样式的HTML标签。在模板…