当前位置:首页 > VUE

vue实现年历

2026-01-07 00:42:32VUE

Vue 实现年历的方法

使用 Vue 实现年历可以通过组件化开发方式,结合日期计算和动态渲染完成。以下是两种常见的实现方式:

基于原生 Vue 的日历组件

创建 Calendar.vue 组件文件,核心逻辑包括日期计算和模板渲染:

<template>
  <div class="calendar">
    <div class="header">{{ currentYear }}年</div>
    <div class="months">
      <div v-for="month in 12" :key="month" class="month">
        <div class="month-title">{{ month }}月</div>
        <div class="days">
          <div v-for="day in getDaysInMonth(currentYear, month)" 
               :key="day" 
               class="day">
            {{ day }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentYear: new Date().getFullYear()
    }
  },
  methods: {
    getDaysInMonth(year, month) {
      return new Date(year, month, 0).getDate()
    }
  }
}
</script>

<style>
.calendar {
  font-family: Arial;
  width: 100%;
}
.months {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}
.month {
  border: 1px solid #ddd;
  padding: 10px;
}
.days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}
.day {
  text-align: center;
  padding: 5px;
}
</style>

使用第三方库 vue-cal

安装 vue-cal 库可以快速实现高级日历功能:

vue实现年历

npm install vue-cal @vueuse/core

组件实现示例:

<template>
  <vue-cal
    :time="false"
    :disable-views="['week', 'day']"
    active-view="year"
    :events="events"
    @ready="onCalendarReady"
  />
</template>

<script>
import VueCal from 'vue-cal'
import 'vue-cal/dist/vuecal.css'

export default {
  components: { VueCal },
  data() {
    return {
      events: [
        { start: '2023-01-15', end: '2023-01-15', title: '假期' },
        { start: '2023-07-01', end: '2023-07-01', title: '纪念日' }
      ]
    }
  },
  methods: {
    onCalendarReady() {
      console.log('Calendar ready')
    }
  }
}
</script>

实现功能扩展

添加月份切换功能

在原生实现中增加年份切换按钮:

vue实现年历

<template>
  <div>
    <button @click="prevYear">上一年</button>
    <span>{{ currentYear }}</span>
    <button @click="nextYear">下一年</button>
    <!-- 日历内容 -->
  </div>
</template>

<script>
export default {
  methods: {
    prevYear() {
      this.currentYear--
    },
    nextYear() {
      this.currentYear++
    }
  }
}
</script>

添加事件标记功能

为特定日期添加样式标记:

<template>
  <div class="day" 
       :class="{ 'has-event': hasEvent(currentYear, month, day) }">
    {{ day }}
  </div>
</template>

<script>
export default {
  data() {
    return {
      markedDates: ['2023-01-01', '2023-10-01']
    }
  },
  methods: {
    hasEvent(year, month, day) {
      const dateStr = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`
      return this.markedDates.includes(dateStr)
    }
  }
}
</script>

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

响应式设计考虑

针对移动端优化布局:

@media (max-width: 768px) {
  .months {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 480px) {
  .months {
    grid-template-columns: 1fr;
  }
}

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

相关文章

vue实现文档分享

vue实现文档分享

Vue 实现文档分享功能 文档分享功能通常涉及文件上传、存储、生成分享链接以及权限控制等模块。以下是基于 Vue 的实现方案: 文件上传组件 使用 element-ui 或 ant-design-v…

vue实现弹窗

vue实现弹窗

Vue 实现弹窗的常见方法 使用组件化方式封装弹窗 创建一个独立的弹窗组件(如 Modal.vue),通过 v-if 或 v-show 控制显示隐藏: <template> <…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json):…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue实现打印

vue实现打印

使用Vue实现打印功能 在Vue项目中实现打印功能可以通过多种方式完成,以下是几种常见的方法: 方法一:使用window.print() 这种方法适用于打印整个页面或特定区域的内容。 // 在Vu…

vue框架实现CRM

vue框架实现CRM

Vue框架实现CRM系统 使用Vue框架构建CRM(客户关系管理)系统需要结合前端技术栈和后端API。以下为关键实现方法: 技术选型 核心框架:Vue 3(Composition API)…