当前位置:首页 > 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为日历添加基本样式,包括格子布局、当前月和其他月日期的不同样式等。

<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

安装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()
        }
      ]
    }
  }
}

模板部分:

vue页面实现日历

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

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

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

相关文章

h5实现页面3d切换

h5实现页面3d切换

使用CSS 3D变换实现基础效果 在HTML5中实现3D页面切换,可以通过CSS的transform属性结合3D变换函数。创建一个包含多个页面的容器,通过调整transform-style和persp…

vue实现页面缩略

vue实现页面缩略

Vue实现页面缩略的方法 使用CSS的transform: scale() 通过CSS的transform属性可以实现页面缩放效果。在Vue组件中,可以动态绑定样式来控制缩放比例。 <temp…

js实现页面加载

js实现页面加载

监听 DOMContentLoaded 事件 使用 DOMContentLoaded 事件可以在 HTML 文档完全加载和解析后触发,无需等待样式表、图像等外部资源加载完成。 document.…

vue实现页面管理

vue实现页面管理

Vue 实现页面管理的方法 使用 Vue Router 进行路由管理 Vue Router 是 Vue.js 的官方路由管理器,适合构建单页面应用(SPA)。通过定义路由表,可以实现页面的动态加载和跳…

vue实现登了页面

vue实现登了页面

Vue 登录页面实现步骤 创建登录表单组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue,包含基本的表单元素如用户名和密码输入框。 <template> <…

vue实现html页面

vue实现html页面

Vue 实现 HTML 页面的方法 Vue.js 是一个渐进式 JavaScript 框架,常用于构建用户界面。以下是使用 Vue 实现 HTML 页面的几种常见方法: 直接引入 Vue.js 在…