当前位置:首页 > VUE

vue页面实现日历

2026-01-14 04:59:22VUE

实现基础日历布局

使用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中定义日历所需的状态,并通过计算属性生成当前月份的日期数组。需要考虑上个月和下个月的残留日期。

<script>
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 days = []
      // 上个月残留日期
      const prevMonthDays = firstDay.getDay()
      for (let i = prevMonthDays - 1; i >= 0; i--) {
        days.push({
          date: new Date(year, month, -i).getDate(),
          isCurrentMonth: false
        })
      }

      // 当前月日期
      for (let i = 1; i <= lastDay.getDate(); i++) {
        days.push({
          date: i,
          isCurrentMonth: true
        })
      }

      // 下个月残留日期
      const nextMonthDays = 6 - lastDay.getDay()
      for (let i = 1; i <= nextMonthDays; i++) {
        days.push({
          date: i,
          isCurrentMonth: false
        })
      }

      return days
    }
  },
  methods: {
    prevMonth() {
      this.currentDate = new Date(this.currentYear, this.currentMonth - 1, 1)
    },
    nextMonth() {
      this.currentDate = new Date(this.currentYear, this.currentMonth + 1, 1)
    }
  }
}
</script>

添加样式美化日历

使用CSS为日历添加基本样式,包括格子布局、当前月和其他月日期的不同样式等。

vue页面实现日历

<style scoped>
.calendar {
  width: 350px;
  margin: 0 auto;
  font-family: Arial, sans-serif;
}

.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 div {
  text-align: center;
  font-weight: bold;
  padding: 5px;
}

.days div {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
}

.days div.other-month {
  color: #ccc;
}

.days div:hover {
  background-color: #f0f0f0;
  cursor: pointer;
}
</style>

添加日期选择功能

扩展基础日历,添加日期选择功能。通过v-model或自定义事件实现与父组件的通信。

// 在data中添加
selectedDate: null,

// 在methods中添加
selectDate(day) {
  if (day.isCurrentMonth) {
    this.selectedDate = new Date(this.currentYear, this.currentMonth, day.date)
    this.$emit('date-selected', this.selectedDate)
  }
}

// 在模板中修改days部分
<div 
  v-for="(day, index) in days" 
  :key="index"
  :class="{ 
    'other-month': !day.isCurrentMonth,
    'selected': selectedDate && selectedDate.getDate() === day.date 
      && selectedDate.getMonth() === currentMonth
      && selectedDate.getFullYear() === currentYear
  }"
  @click="selectDate(day)"
>
  {{ day.date }}
</div>

使用第三方库简化开发

对于更复杂的需求,可以考虑使用专门的Vue日历组件库,如v-calendarvue-datepicker

vue页面实现日历

安装v-calendar:

npm install v-calendar

基本使用示例:

import { Calendar } from 'v-calendar'

export default {
  components: {
    Calendar
  },
  data() {
    return {
      attributes: [
        {
          key: 'today',
          highlight: {
            color: 'blue',
            fillMode: 'light'
          },
          dates: new Date()
        }
      ]
    }
  }
}

模板部分:

<Calendar 
  :attributes="attributes" 
  @dayclick="dayClicked"
/>

这种方法可以快速实现高级功能如范围选择、多日期选择、自定义样式等,减少自行开发的复杂度。

标签: 日历页面
分享给朋友:

相关文章

vue单页面实现登录

vue单页面实现登录

实现登录功能的基本流程 在Vue单页面应用中实现登录功能,通常需要以下几个关键步骤: 创建登录表单组件 使用Vue的模板语法创建包含用户名和密码输入框的表单,并绑定数据到组件的data属性。 <…

vue日历表格实现

vue日历表格实现

Vue 日历表格实现方法 基础日历表格结构 使用 Vue 的模板语法构建日历表格框架,通过 v-for 循环生成日期单元格。核心是计算当前月份的天数及起始星期位置。 <template>…

vue文件实现页面跳转

vue文件实现页面跳转

Vue文件实现页面跳转的方法 在Vue项目中,页面跳转可以通过Vue Router实现。以下是几种常见的实现方式: 声明式导航 使用<router-link>组件实现跳转,适合模板中使…

vue实现页面

vue实现页面

Vue 实现页面的基本方法 创建 Vue 项目 使用 Vue CLI 快速初始化项目,运行以下命令安装并创建项目: npm install -g @vue/cli vue create my-pro…

vue实现页面定位

vue实现页面定位

Vue 实现页面定位的方法 在 Vue 中实现页面定位通常可以通过以下几种方式完成,包括使用原生 JavaScript 的 scrollIntoView 方法、Vue Router 的滚动行为配置,以…

vue实现页面分页

vue实现页面分页

Vue实现页面分页的方法 使用Element UI的分页组件 Element UI提供了现成的分页组件el-pagination,可以快速实现分页功能。需要先安装Element UI库。 <…