vue手写日历

11 篇文章 3 订阅

在这里插入图片描述

<template>
  <div class="page">
    输入月份数字<input v-model="inputVal" type="text"><button @click="change">点击</button>
    <ul class="calendar">
      <li class="header">
        {{
                new Date().getFullYear() + '年' + (currentMonth+1) + '月'
              }}
      </li>
      <li class="week">
        <p></p>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
      </li>
      <li class="row day">
        <span class="prevDay date" v-for="item in prevDays" :key="'A' + item">{{ item }}</span>
        <span v-for="item in currentDays" :key="'B' + item" class="nowDay date">{{ item }}</span>
        <span class="nextDay date" v-for="item in nextDays" :key="'A' + item">{{ item }}</span>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      week: ["日", "一", "二", "三", "四", "五", "六"],
      currentDay: new Date().getDate(),
      currentMonth: new Date().getMonth(),
      currentYear: new Date().getFullYear(),
      inputVal: ''
    };
  },
  computed: {
    currentMonthChinese() {
      return new Date(this.currentYear, this.currentMonth).toLocaleString(
        'default',
        { month: 'short' }
      )
    },
    currentDays() {
      return new Date(this.currentYear, this.currentMonth + 1, 0).getDate()
    },
    prevDays() {
      let data = new Date(this.currentYear, this.currentMonth, 0).getDate()
      let num = new Date(this.currentYear, this.currentMonth, 1).getDay()
      var days = []
      while (num > 0) {
        days.push(data--)
        num--
      }
      return days.sort()
    },
    nextDays() {
      const m = this.currentMonth + 1
      let num = new Date(this.currentYear, m, 1).getDay()
      var days = []
      let number = 0
      while (num < 7) {
        number++
        days.push(number)
        num++
      }
      return days.sort()
    }
  },
  methods: {
    change(){
      this.currentMonth = Number(this.inputVal) -1
    }
  },
};
</script>

<style lang="scss">
.calendar {
  width: 730px;
  .header {
    display: flex;
    align-items: center;
    height: 64px;
    background: #f9f9f9;
    border-radius: 4px 4px 0px 0px;
    border: 2px solid #f4f4f4;
    font-size: 28px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #404040;
    padding-left: 20px;
  }
  .week {
    display: flex;
    height: 64px;
    background: #fefefe;
    border: 2px solid #f4f4f4;
    font-size: 24px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #404040;
    p {
      width: 100px;
      height: 64px;
    }
  }
  .row {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }
  .day {
    -webkit-box-pack: start;
    -ms-flex-pack: start;
    justify-content: flex-start;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    span {
      width: 100px;
      height: 30px;
      line-height: 30px;
      text-align: center;
    }
    .date {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100px;
      height: 94px;
      background: #fefefe;
      border: 2px solid #f4f4f4;
      font-size: 28px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #9b9b9b;
    }
    .nowDay {
      background: #404040;
    }
  }
}
</style>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

David凉宸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值