美文网首页
12、vue2写一个日历

12、vue2写一个日历

作者: 蜗牛的学习方法 | 来源:发表于2023-04-03 16:28 被阅读0次

工作中需要用到日历组件,但是antd不满足于业务需求,所以自己写了一个日历组件

<template>
  <div class="canlendar-right">
    <div>
      <span @click="handlePrev"><</span>{{ selectMonth }}
      <span @click="handleNext">></span>
    </div>
    <div class="week-header">
      <div v-for="item in weekData" :key="item">
        {{ item }}
      </div>
    </div>
    <div class="week-day">
      <div
        class="day"
        v-for="item in calendarArr"
        :key="item.value"
        @click="handleChangeCalendar(item)"
      >
        <span
          :class="[
            item.isNow ? 'currentDay' : 'otherDay',
            { active: item.value === selectDate },
          ]"
        >
          {{ item.label }}
        </span>
      </div>
    </div>
  </div>
</template>

<script>
import moment from 'moment';
const weekData = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
export default {
  data() {
    return {
      weekData,
      calendarArr: [],
      selectDate: moment().format('YYYY-MM-DD'),
    };
  },
  computed: {
    selectMonth() {
      return moment(this.selectDate).format('YYYY-MM');
    },
    //获取当月第一天是星期几
    beginDay() {
      // return new Date(this.year, this.month - 1, 1).getDay();
      let date = new Date(this.selectMonth);
      date.setMonth(date.getMonth(), 1);
      return date.getDay();
    },
    //获取当月最后一天
    curDays() {
      let date = new Date(this.selectMonth);
      date.setMonth(date.getMonth() + 1, 0);
      return date.getDate();
    },
    //获取上月最后一天
    prevDays() {
      let date = new Date(this.selectDate);
      date.setDate(0);
      return date.getDate();
    },
  },
  mounted() {
    this.getInitDate();
  },
  methods: {
    handlePrev() {
      this.selectDate = moment(this.selectDate)
        .subtract(1, 'month')
        .format('YYYY-MM-DD');
      this.getInitDate();
    },
    handleNext() {
      this.selectDate = moment(this.selectDate)
        .add(1, 'month')
        .format('YYYY-MM-DD');
      this.getInitDate();
    },
    getInitDate() {
      let calendarArr = new Array(42);
      this.calendarArr = [];
      const date = new Date(this.selectDate);
      this.year = date.getFullYear();
      this.month = date.getUTCMonth() + 1;
      this.day = date.getDate();
      console.log(this.year, this.month, this.day, this.beginDay);
      for (let i = 1; i <= 42; i++) {
        if (i - this.beginDay > 0 && i - this.beginDay <= this.curDays) {
          calendarArr[i] = {
            label: i - this.beginDay,
            value: moment(
              `${this.year}-${this.month}-${i - this.beginDay}`
            ).format('YYYY-MM-DD'),
            isNow: true,
          };
        } else if (i - this.beginDay <= 0) {
          let year = this.year;
          let month = this.month - 1;
          if (this.month === 1) {
            year = year - 1;
            month = 12;
          }
          calendarArr[i] = {
            label: i - this.beginDay + this.prevDays,
            value: moment(
              `${year}-${month}-${i - this.beginDay + this.prevDays}`
            ).format('YYYY-MM-DD'),
          };
        } else {
          let year = this.year;
          let month = this.month + 1;
          if (this.month === 12) {
            year = year + 1;
            month = 1;
          }
          calendarArr[i] = {
            label: i - this.beginDay - this.curDays,
            value: moment(
              `${year}-${month}-${i - this.beginDay - this.curDays}`
            ).format('YYYY-MM-DD'),
          };
        }
      }
      this.calendarArr = calendarArr.filter((v) => v.label);
      console.log('11', this.calendarArr);
    },
    handleChangeCalendar(item) {
      this.selectDate = item.value;
    },
  },
};
</script>

<style scoped>
.canlendar-right {
  width: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.week-header {
  color: #4e5969;
  font-size: 14px;
  text-align: center;
  width: 100%;
  display: flex;
  margin: 20px 0 9px 0;
}
.week-header div {
  width: 100px;
}
.week-day {
  width: 400px;
  border-right: 1px solid #e0e4ed;
  border-bottom: 1px solid #e0e4ed;
  display: flex;
  flex-wrap: wrap;
}
.day {
  height: 55px;
  width: 55px;
  text-align: center;
  line-height: 50px;
  border-left: 1px solid #e0e4ed;
  border-top: 1px solid #e0e4ed;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
}
.currentDay {
  color: #1d2129;
  font-size: 16px;
  display: inline-block;
  width: 28px;
  height: 28px;
  line-height: 28px;
  margin-top: 10px;
}
.otherDay {
  color: #c9cdd4;
  font-size: 16px;
  display: inline-block;
  width: 28px;
  height: 28px;
  line-height: 28px;
  margin-top: 10px;
}
.active {
  border-radius: 50%;
  background: #0147eb;
  color: #fff;
}
.task-height {
  width: 100%;
  height: 20px;
  background: rgba(233, 247, 255, 0.6);
  border-top: 2px solid rgba(24, 144, 255, 1);
  position: relative;
}
.task-height span {
  position: absolute;
  top: -39px;
  transform: translateX(-50%);
  display: inline-block;
  width: 100%;
  text-align: center;
  font-weight: 400;
  font-size: 12px;
  color: #5698d6;
  font-family: MiSans-Normal;
}
.over-load {
  background: rgba(255, 242, 232, 1);
  border-top: 2px solid rgba(255, 122, 69, 1);
}
.over-load span {
  color: #fa8c16;
}
</style>

效果如图:


image.png

相关文章

  • 写日历

    昨天我写了日历,它是2020年的日历,我只写了现在的月份,也就是2月。 我就根据数学书上的日历写,...

  • 写一个签到日历

    最近项目上写了一个签到页面,其中有一个功能设计的是点击签到,可以查看签到日历,稍微麻烦点的是连续签到的日期间有横线...

  • 写小日历

  • vue 写一个日历组件

    之前自己写了一个vue的日历demo,那时直接引入cdn的vue.js完成的,功能简单,样式简单,代码还写得乱起八...

  • 我的Vue.js之旅

    初入Vue.js (Vue2) 项目后台用PHP接口形式写,前台学习着用Vue调自己接口,给公司写一个运营支撑平台...

  • Vue实例

    Vue2 实战:模仿卖座电影 一个使用 Vue2 全家桶高仿卖座电影网的项目JavaScript 日记 - 一个例...

  • 2018-08-25

    用javascript写日历 这是刚开始学习js基础时做的一个小小的日历案例,只是完成了简单的日历功能,可以输出指...

  • 4. vue3 项目实践

    1. vue2 中的 data 2. vue2 methods 3. vue2 中的 router 4. vue2...

  • vue2实现material风格的组件之button

    前言 最近学vue2的时候发现好像没有基于vue2的一个material design风格的组件库,因为自己对这种...

  • 使用组合式api——setup代替vue2的选项式api

    先说说vue2的选项式api,请看代码 这是一个常见的vue2文件,其中data,computed,watch,c...

网友评论

      本文标题:12、vue2写一个日历

      本文链接:https://www.haomeiwen.com/subject/oetjddtx.html