前端 vue 制作日历(二)

17 篇文章 1 订阅
8 篇文章 1 订阅

先上个图
在这里插入图片描述
此方法是把一个月的数据转换成二维数组
建立在之前的方式上

在这里插入图片描述

完整代码

<template>
  <div id="calendar">
    <div class="calendar_header">
      <p style="width:33%" @click="monthtoleft"></p>
      <p style="width:34%;text-align:center">
        <span>{{thisyear}}</span>-
        <span>{{thismonth&lt;=9?'0'+thismonth:thismonth}}</span>-
        <span>{{today&lt;=9?'0'+today:today}}</span> <br>
        <span>星期{{week[weekday]}}</span>
      </p>
      <p style="width:33%;text-align:right" @click="monthtoright"></p>

    </div>
    <hr>
    <div class="calendar-week">
      <div class="calendar-week-day" v-for="(item,index) in week" :key="index"><span
          :class="{weektoday:index==weekday}">{{item}}</span></div>
    </div>
    <div class="calendar-week" v-for="(item,index) in daylist" :key="index">
      <div class="calendar-week-day" v-for="(item2,index2) in item" :key="index2"
        :class="{fontcolor:item2.ismonth==0,check:thisyear==year&&thismonth==month&&item2.day==today&&item2.ismonth==1}"
        @click="checkday(item2.day,item2.ismonth)">{{item2.day}}
      </div>
    </div>
  </div>
</template>

<script>
const myDate = new Date();
export default {
  data () {
    return {
      week: ['日', '一', '二', '三', '四', '五', '六'],
      daylist: [],
      thisyear: '',
      thismonth: '',
      today: '',
      weekday: '',
      year: '',
      month: '',
    };
  },
  created () {
    //初始化年月日星期
    this.year = this.thisyear = myDate.getFullYear()
    this.month = this.thismonth = myDate.getMonth() + 1
    this.today = myDate.getDate()
    this.weekday = myDate.getDay()
    this.getcalendar()
  },
  methods: {
    getcalendar () {
      let daysnumber = new Date(this.thisyear, this.thismonth, 0).getDate();//获取当月天数
      let beginweeks = new Date(this.thisyear, this.thismonth - 1, 1).getDay();//获取当月1日是星期几,确定1日开始位置
      let list = []
      let newList = []
      let mos = ''
      // 获取上个月的天数
      if (this.thismonth == 1) {
        mos = new Date(this.thisyear, 11, 0).getDate()
      } else {
        mos = new Date(this.thisyear, this.thismonth - 1, 0).getDate()
      }
      //补全当前月之前空缺位置
      for (var i = beginweeks; i > 0; i--) {
        let arr = {
          day: mos--,
          ismonth: '0'
        }
        list.push(arr)
      }
      newList = list.reverse()

      for (var j = 1; j <= daysnumber; j++) {
        let arr = {
          day: j,
          ismonth: '1'
        }
        newList.push(arr)
      }
      let endweeks = new Date(this.thisyear, this.thismonth - 1, daysnumber).getDay();//获取当月最后一天是星期几
      //补全每月结束之后空缺位置
      for (var m = 1; m < 7 - endweeks; m++) {
        let arr = {
          day: m,
          ismonth: '0'
        }
        newList.push(arr)
      }
      // 转变一下
      let arrr = []
      this.daylist = newList.reduce(function (pre, item, index, arr) {
        var begin = index * 7;
        var end = begin + 7;
        var res = arr.slice(begin, end);
        if (res.length != 0) {
          arrr[index] = res;
        }
        return arrr;
      }, [])
    },
    //上一月
    monthtoleft () {
      if (this.thismonth > 1) {
        this.thismonth = this.thismonth - 1
      } else {
        this.thismonth = 12
        this.thisyear = this.thisyear - 1
      }
      this.getcalendar()
    },
    //下一月
    monthtoright () {
      if (this.thismonth < 12) {
        this.thismonth = this.thismonth + 1
      } else {
        this.thismonth = 1
        this.thisyear = this.thisyear + 1
      }
      this.getcalendar()
    },
    //获取点击日期
    checkday (days, ismonth) {
      if (ismonth == 1) {
        this.year = this.thisyear
        this.month = this.thismonth
        this.today = days
        this.weekday = new Date(this.thisyear, this.thismonth - 1, days).getDay()
      }
    },
  }

};
</script>
<style scoped lang="scss">
#calendar {
  background: white;
  padding: 10px;
  border: 1px solid #eaebed;
  max-height: 503px;
  .calendar_header {
    height: 67px;
    width: 100%;
    color: #5e7a88;
    display: flex;
  }
  hr {
    background: #eaebed;
    height: 1px;
    border: none;
    margin: 0;
    margin-left: -10px;
  }
  .calendar-week {
    line-height: 58px;
    text-align: center;
    display: inline-block;
    cursor: pointer;
    margin-bottom: 1px;
    display: flex;
    .calendar-week-day {
      width: 15%;
      height: 58px;
      background-color: #eef7fe;
      margin-right: 1px;
      border-radius: 4px;
    }
    .weektoday {
      display: inline-block;
      width: 30px;
      height: 30px;
      background: #ffb400;
      line-height: 30px;
      border-radius: 15px;
      color: white;
    }
    .fontcolor {
      color: #ccc;
      cursor: not-allowed;
      display: inline-block;
    }
    .check {
      background: #95cffd;
    }
  }
}
</style>



  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值