当前位置:首页 > VUE

基于vue实现日历

2026-01-19 06:23:58VUE

实现基础日历结构

使用Vue的模板语法构建日历的HTML结构,通常包含星期标题和日期格子。月份切换按钮通过v-on绑定事件。

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">上一月</button>
      <h2>{{ currentYear }}年{{ currentMonth + 1 }}月</h2>
      <button @click="nextMonth">下一月</button>
    </div>
    <div class="weekdays">
      <div v-for="day in weekdays" :key="day">{{ day }}</div>
    </div>
    <div class="days">
      <div 
        v-for="(day, index) in days" 
        :key="index"
        :class="{ 'other-month': !day.isCurrentMonth }"
      >
        {{ day.date }}
      </div>
    </div>
  </div>
</template>

处理日历数据逻辑

在Vue的datasetup中定义响应式变量,使用JavaScript的Date对象计算当月天数、上月剩余天数及下月补全天数。

export default {
  data() {
    return {
      currentDate: new Date(),
      weekdays: ['日', '一', '二', '三', '四', '五', '六']
    }
  },
  computed: {
    currentYear() {
      return this.currentDate.getFullYear()
    },
    currentMonth() {
      return this.currentDate.getMonth()
    },
    days() {
      const year = this.currentYear
      const month = this.currentMonth
      const firstDay = new Date(year, month, 1)
      const lastDay = new Date(year, month + 1, 0)
      const daysInMonth = lastDay.getDate()
      const firstDayOfWeek = firstDay.getDay()

      // 生成当月天数数组
      const days = []
      for (let i = 1; i <= daysInMonth; i++) {
        days.push({ date: i, isCurrentMonth: true })
      }

      // 补全上月剩余天数
      const prevMonthDays = firstDayOfWeek
      const prevMonthLastDay = new Date(year, month, 0).getDate()
      for (let i = 0; i < prevMonthDays; i++) {
        days.unshift({ date: prevMonthLastDay - i, isCurrentMonth: false })
      }

      // 补全下月天数(保持6行)
      const totalCells = Math.ceil(days.length / 7) * 7
      const nextMonthDays = totalCells - days.length
      for (let i = 1; i <= nextMonthDays; i++) {
        days.push({ date: i, isCurrentMonth: false })
      }

      return days
    }
  }
}

实现月份切换功能

添加月份切换方法,注意处理跨年情况。使用setMonth会自动处理月份溢出(如month=12会变成下一年1月)。

methods: {
  prevMonth() {
    this.currentDate = new Date(
      this.currentYear,
      this.currentMonth - 1,
      1
    )
  },
  nextMonth() {
    this.currentDate = new Date(
      this.currentYear,
      this.currentMonth + 1,
      1
    )
  }
}

添加样式美化日历

使用CSS Grid布局使日历排列整齐,为不同状态的日期添加视觉区分。

.calendar {
  width: 350px;
  font-family: Arial;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.weekdays, .days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}
.weekdays {
  font-weight: bold;
  text-align: center;
}
.days div {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
}
.days div.other-month {
  opacity: 0.3;
}

扩展功能:日期选择

添加日期选择功能,通过v-model绑定选中的日期,高亮显示当前选中日期。

基于vue实现日历

<div 
  v-for="(day, index) in days" 
  :key="index"
  :class="{ 
    'other-month': !day.isCurrentMonth,
    'selected': isSelected(day)
  }"
  @click="selectDate(day)"
>
  {{ day.date }}
</div>
data() {
  return {
    selectedDate: null
  }
},
methods: {
  isSelected(day) {
    if (!this.selectedDate || !day.isCurrentMonth) return false
    return (
      this.selectedDate.getDate() === day.date &&
      this.selectedDate.getMonth() === this.currentMonth &&
      this.selectedDate.getFullYear() === this.currentYear
    )
  },
  selectDate(day) {
    if (!day.isCurrentMonth) return
    this.selectedDate = new Date(
      this.currentYear,
      this.currentMonth,
      day.date
    )
  }
}
.days div.selected {
  background-color: #42b983;
  color: white;
}

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

相关文章

vue实现下拉框删除

vue实现下拉框删除

Vue 实现下拉框删除功能 在Vue中实现下拉框的删除功能通常涉及以下步骤: 数据绑定与渲染 在Vue组件中定义数据数组用于存储下拉框选项: data() { return { op…

vue首页实现

vue首页实现

实现Vue首页的基本步骤 创建一个Vue首页通常涉及项目初始化、页面结构设计、路由配置和组件开发。以下是具体实现方法: 初始化Vue项目 使用Vue CLI或Vite快速搭建项目结构: npm i…

vue实现双向

vue实现双向

Vue 实现双向绑定的方法 Vue 的双向绑定主要通过 v-model 指令实现,它结合了数据绑定和事件监听,适用于表单元素(如 input、select、textarea 等)。以下是几种常见的实现…

vue实现答题

vue实现答题

Vue 实现答题功能 数据结构设计 使用 Vue 的响应式特性管理题目和答案数据。通常需要以下数据结构: data() { return { questions: [ {…

vue toast实现

vue toast实现

Vue Toast 实现方法 使用第三方库(推荐) 安装 vue-toastification 库,这是一个功能丰富且轻量级的 Vue Toast 插件。 npm install vue-toast…

vue实现抽屉

vue实现抽屉

Vue 实现抽屉组件 抽屉组件是一种常见的 UI 模式,通常用于从屏幕边缘滑出内容。以下是几种实现抽屉的方法: 使用 Vue 原生实现 创建一个基本的抽屉组件,利用 Vue 的过渡和条件渲染功能。…