当前位置:首页 > 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>

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

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

相关文章

css3制作立体效果

css3制作立体效果

使用 transform 属性实现立体旋转 通过 transform-style: preserve-3d 和 rotateX/Y/Z 实现 3D 空间变换: .container { tr…

h5所能实现的效果

h5所能实现的效果

H5能实现的效果 H5(HTML5)作为最新的HTML标准,具备强大的功能和丰富的API支持,能够实现多种现代Web应用效果。以下是H5的主要能力分类: 多媒体支持 H5原生支持音频和视频播放,无需…

vue实现滑动效果

vue实现滑动效果

Vue 实现滑动效果的方法 在 Vue 中实现滑动效果可以通过多种方式,包括使用 CSS 过渡、第三方动画库或直接操作 DOM。以下是几种常见的方法: 使用 CSS 过渡和 Vue 的 <tr…

vue实现的效果

vue实现的效果

Vue.js 是一个流行的前端框架,用于构建用户界面和单页应用。以下是一些常见的 Vue 实现效果及其实现方式: 动态数据绑定 Vue 的核心特性之一是响应式数据绑定。通过将数据与 DOM 元素绑定…

Vue实现日历日程

Vue实现日历日程

Vue 实现日历日程 使用第三方库(如 FullCalendar) FullCalendar 是一个功能强大的日历库,支持 Vue 集成。安装依赖: npm install @fullcal…

vue实现移动效果

vue实现移动效果

使用 CSS 过渡实现移动效果 在 Vue 中,可以通过 v-bind:class 或 v-bind:style 结合 CSS 过渡实现移动效果。定义一个 CSS 类,包含 transition 属性…