当前位置:首页 > VUE

vue实现日历效果

2026-03-30 10:12:29VUE

Vue实现日历效果的方法

使用第三方库

Vue-Cal是一个专为Vue设计的日历组件库,支持月视图、周视图和日视图,具备事件拖拽、自定义样式等功能。安装方式:

npm install vue-cal

在组件中使用:

vue实现日历效果

<template>
  <vue-cal :events="events" />
</template>

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

export default {
  components: { VueCal },
  data() {
    return {
      events: [
        { start: '2023-10-01 10:00', end: '2023-10-01 12:00', title: '会议' }
      ]
    }
  }
}
</script>

手动实现基础日历

创建一个显示当月日期的网格布局,计算当月天数及起始星期:

vue实现日历效果

<template>
  <div class="calendar">
    <div class="header">{{ currentMonth }}</div>
    <div class="weekdays">
      <div v-for="day in weekdays" :key="day">{{ day }}</div>
    </div>
    <div class="days">
      <div 
        v-for="(date, index) in visibleDates" 
        :key="index"
        :class="{ 'other-month': !date.isCurrentMonth }">
        {{ date.day }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentDate: new Date(),
      weekdays: ['日', '一', '二', '三', '四', '五', '六']
    }
  },
  computed: {
    currentMonth() {
      return this.currentDate.toLocaleString('default', { 
        month: 'long', 
        year: 'numeric' 
      })
    },
    visibleDates() {
      const year = this.currentDate.getFullYear()
      const month = this.currentDate.getMonth()
      const firstDay = new Date(year, month, 1)
      const lastDay = new Date(year, month + 1, 0)

      const startDay = firstDay.getDay()
      const daysInMonth = lastDay.getDate()

      const prevMonthDays = new Date(year, month, 0).getDate()
      const dates = []

      for (let i = startDay - 1; i >= 0; i--) {
        dates.push({ 
          day: prevMonthDays - i, 
          isCurrentMonth: false 
        })
      }

      for (let i = 1; i <= daysInMonth; i++) {
        dates.push({ 
          day: i, 
          isCurrentMonth: true 
        })
      }

      const remaining = 42 - dates.length
      for (let i = 1; i <= remaining; i++) {
        dates.push({ 
          day: i, 
          isCurrentMonth: false 
        })
      }

      return dates
    }
  }
}
</script>

<style>
.calendar {
  display: grid;
  grid-template-rows: auto auto 1fr;
}
.weekdays, .days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
}
.other-month {
  opacity: 0.5;
}
</style>

增强功能实现

添加月份切换和日期选择功能:

<script>
export default {
  methods: {
    prevMonth() {
      this.currentDate = new Date(
        this.currentDate.getFullYear(),
        this.currentDate.getMonth() - 1,
        1
      )
    },
    nextMonth() {
      this.currentDate = new Date(
        this.currentDate.getFullYear(),
        this.currentDate.getMonth() + 1,
        1
      )
    },
    selectDate(date) {
      if (date.isCurrentMonth) {
        this.selectedDate = new Date(
          this.currentDate.getFullYear(),
          this.currentDate.getMonth(),
          date.day
        )
      }
    }
  }
}
</script>

事件管理

为日历添加事件显示功能:

<template>
  <div 
    v-for="(date, index) in visibleDates" 
    :key="index"
    @click="selectDate(date)">
    {{ date.day }}
    <div 
      v-for="event in getEvents(date)" 
      :key="event.id"
      class="event">
      {{ event.title }}
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    getEvents(date) {
      return this.events.filter(event => {
        const eventDate = new Date(event.date)
        return eventDate.getDate() === date.day && 
               eventDate.getMonth() === this.currentDate.getMonth()
      })
    }
  }
}
</script>

以上方法提供了从简单到复杂的日历实现方案,可根据项目需求选择适合的方式。第三方库适合快速集成,手动实现则更灵活可控。

标签: 日历效果
分享给朋友:

相关文章

vue页面实现日历

vue页面实现日历

Vue 页面实现日历的方法 使用第三方组件库 推荐使用成熟的日历组件库,如 v-calendar 或 fullcalendar-vue,快速实现功能丰富的日历。 安装 v-calendar: np…

vue怎么实现日历

vue怎么实现日历

实现基础日历布局 使用Vue的模板语法构建日历的HTML结构,通常采用表格形式展示日期。月份切换通过计算属性动态生成日期数组。 <template> <div class=…

h5页面滑动效果实现

h5页面滑动效果实现

实现H5页面滑动效果的方法 H5页面滑动效果可以通过多种方式实现,包括原生JavaScript、CSS动画或第三方库。以下是几种常见的方法: 使用CSS Scroll Snap CSS Scroll…

vue实现tab效果

vue实现tab效果

Vue 实现 Tab 效果的方法 使用动态组件和 v-if 通过 v-if 或 v-show 控制不同标签内容的显示与隐藏,结合点击事件切换当前激活的标签页。 <template>…

vue实现多选效果

vue实现多选效果

Vue 实现多选效果的方法 使用 v-model 绑定数组 在 Vue 中,可以通过 v-model 绑定一个数组来实现多选效果。适用于复选框组(checkbox)或下拉多选(select multi…

vue实现multiselect效果

vue实现multiselect效果

Vue 实现多选(Multiselect)效果 使用第三方库 vue-multiselect 安装 vue-multiselect 库: npm install vue-multiselect 在…