返回

Vue.js实现可自定义日历, 助力日程管理

前端

在软件开发中,日历是一个不可或缺的元素,它可以帮助用户轻松地管理日程和安排任务。在Vue.js框架中,我们可以利用其强大的组件系统和丰富的功能,轻松地实现一个可自定义日历,满足不同用户的个性化需求。

1. 搭建日历组件的基本框架

首先,我们需要搭建日历组件的基本框架。这包括创建必要的HTML结构、CSS样式和Vue组件代码。

<template>
  <div class="calendar">
    <div class="calendar-header">
      <button @click="prevMonth"></button>
      <div>{{ year }}{{ month }}</div>
      <button @click="nextMonth"></button>
    </div>
    <div class="calendar-body">
      <div class="calendar-week">
        <div v-for="day in weekDays" :key="day">{{ day }}</div>
      </div>
      <div class="calendar-days">
        <div v-for="day in days" :key="day" :class="{ 'calendar-day': true, 'calendar-day--today': day === today }" @click="selectDay(day)">{{ day }}</div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue';

export default {
  setup() {
    const year = ref(new Date().getFullYear());
    const month = ref(new Date().getMonth() + 1);
    const today = ref(new Date().getDate());
    const weekDays = ['日', '一', '二', '三', '四', '五', '六'];
    const days = computed(() => {
      const firstDayOfMonth = new Date(year.value, month.value - 1, 1).getDay();
      const lastDayOfMonth = new Date(year.value, month.value, 0).getDate();
      const days = [];
      for (let i = 1; i <= lastDayOfMonth; i++) {
        days.push(i);
      }
      return days;
    });

    const prevMonth = () => {
      month.value--;
      if (month.value < 1) {
        month.value = 12;
        year.value--;
      }
    };

    const nextMonth = () => {
      month.value++;
      if (month.value > 12) {
        month.value = 1;
        year.value++;
      }
    };

    const selectDay = (day) => {
      console.log(`你选择了${year.value}${month.value}${day}日`);
    };

    return {
      year,
      month,
      today,
      weekDays,
      days,
      prevMonth,
      nextMonth,
      selectDay
    };
  }
};
</script>

<style>
.calendar {
  width: 350px;
  margin: 0 auto;
  border: 1px solid #ccc;
  border-radius: 5px;
}

.calendar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  background-color: #f5f5f5;
}

.calendar-header button {
  border: none;
  background-color: #f5f5f5;
  cursor: pointer;
}

.calendar-header div {
  font-size: 18px;
  font-weight: bold;
}

.calendar-body {
  padding: 10px;
}

.calendar-week {
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: #f5f5f5;
}

.calendar-week div {
  width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  font-size: 12px;
  font-weight: bold;
}

.calendar-days {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.calendar-day {
  width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  font-size: 12px;
  border: 1px solid #ccc;
  border-radius: 3px;
  cursor: pointer;
}

.calendar-day--today {
  background-color: #ccc;
}
</style>

2. 实现日历的交互功能

接下来,我们需要实现日历的交互功能,包括切换月份、选择日期等。

<script>
import { ref, computed } from 'vue';

export default {
  setup() {
    // 省略其他代码

    const selectDay = (day) => {
      console.log(`你选择了${year.value}${month.value}${day}日`);
    };

    return {
      // 省略其他代码

      selectDay
    };
  }
};
</script>

3. 添加日历自定义功能

最后,我们可以根据需要添加日历的自定义功能,例如修改显示时间、点击时间、点击动画、打卡显示、奖品显示等。

<script>
import { ref, computed } from 'vue';

export default {
  setup() {
    // 省略其他代码

    const showTime = ref(false);
    const clickTime = ref('');
    const clickAnimation = ref(false);
    const打卡 = ref(false);
    const奖品 = ref(false);

    const toggleShowTime = () => {
      showTime.value = !showTime.value;
    };

    const onDayClick = (day) => {
      clickTime.value = `${year.value}${month.value}${day}日`;
      clickAnimation.value = true;
      setTimeout(() => {
        clickAnimation.value = false;
      }, 500);
    };

    const toggle打卡 = () => {
      打卡.value = !打卡.value;
    };

    const toggle奖品 = () => {
      奖品.value = !奖品.value;
    };

    return {
      // 省略其他代码

      showTime,
      clickTime,
      clickAnimation,
      打卡,
      奖品,
      toggleShowTime,
      onDayClick,
      toggle打卡,
      toggle奖品
    };
  }
};
</script>

4. 样式美化

最后,我们可以根据自己的喜好对日历组件进行样式美化,让它看起来更加美观。

<style>
// 省略其他样式代码

.calendar {
  width: 500px;
  margin: 0 auto;
  border: 1px solid #333;
  border-radius: 5px;
  background-color: #fff;
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.1);
}

.calendar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  background-color: #f5f5f5;
}

.calendar-header button {
  border: none;
  background-color: #f5f5f5;
  cursor: pointer;
}

.calendar-header div {
  font-size: 20px;
  font-weight: bold;
  color: #333;
}

.calendar-body {
  padding: 10px;
}

.calendar-week {
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: #f5f5f5;
}

.calendar