vue实现日历样式

最近,项目组要求仿照企业微信的打卡功能,实现报工的日历展示功能,看了很多资料,现在此自己总结一下思路,以做记录。

首先看界面效果:
在这里插入图片描述
我先描述一下思路:首先判断当前月份一共有几天total,然后找到当月的1号是周几,找到在日历中显示的位置,然后依次往数组里添加对象,有几天就添加几条数据。

一、页面样式布局
<template>
  <div class="date-page">
    <div class="calenders">
      <div class="date-header">
        <div class="pre-month" @click="preMonthClick()">
          <i class="iconfont icon-zuobianjiantou font20"></i>
        </div>
        <div class="show-date">{{year}}{{month}}</div>
        <div class="pre-month" @click="nextMonthClick()" v-if="show">
          <i class="iconfont icon-youbianjiantou font20"></i>
        </div>
      </div>
      <div class="date-content">
        <div class="week-header">
          <div v-for="item in ['日','一','二','三','四','五','六']" :key="item">{{ item }}</div>
        </div>
      
        <div class="week-day">
          <div
            class="every-day"
            :class="{actived:index == isActive}"
            v-for="(showDay,index) in showData"
            :key="index"
            @click="detailClick(showDay,index)"
          >
            <div :class="['day',isWeekDay?'circle-color-blue':'']">{{showDay}}</div>
            <div v-if="showDay!==''" class="cicle" :class="colorClass(1)"></div>
          </div>
        </div>
      </div>
    </div>
    <div class="detail">
      <div>
        <div class="detail-title">当天报工</div>
        <div class="line"></div>
      </div>

      <!-- <div>
        <div class="detail-bottom" v-for="(item,index) in showDayDeatile.datePeriods" :key="index">
          <div class="left" style="display:inline-block">{{item.jobTime}}</div>
          <div class="right" style="float:right;display:inline-block">{{item.state}}</div>
        </div>
        <div class="line"></div>
        <div class="detail-bottom">
          <div
            class="right"
            style="float:right;font-size:14px;font-weight:bold;right:0.2rem"
          >审批人:{{showDayDeatile.appRover}}</div>
        </div>
      </div>-->
      <div style="height:60px">
        <div class="detail-bottom">
          <div class="left" style="display:inline-block">上午 • 已报工</div>
          <div class="right" style="float:right;display:inline-block">待审核</div>
        </div>
        <div class="right" style="float:right;font-size:14px;right:0.2rem">审批人 : 李四</div>
      </div>
      <div class="line"></div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      year: "",
      month: "",
      today: "",
      calendarDateList: [],
      showData: [],
      isActive: -1,
      show: false,
      showDayDeatile: {},
      circleColor: 0, //圆点的颜色,0-白色,1-红色,2-橙色
      isWeekDay: false
    };
  },

二、获的当前的日期,初始化数据

 getToday() {
      let date = new Date();
      this.year = date.getFullYear();
      this.month = date.getMonth() + 1;
      this.today = date.getDate();//获取这是周几
      //初始化数据
      this.getMonthDays(this.year, this.month - 1);
      //得到当月今天的位置index
      let targetDay = new Date(this.year, this.month - 1, 1).getDay();
      let index = this.today + targetDay - 1;
      this.isActive = index;
    },
getMonthDays(year, month) {
      // 定义每个月的天数,如果是闰年第二月改为29天
      let daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
      if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
        daysInMonth[1] = 29;
      }
      // 当月第一天为周几
      let targetDay = new Date(year, month, 1).getDay();
      // console.log("targetDay:" + targetDay);
      let calendarDateList = [];
      let preNum = targetDay;
      let nextNum = 0;
      if (targetDay > 0) {
        // 当前月份1号前的自然周剩余日期,置空
        for (let i = 0; i < preNum; i++) {
          calendarDateList.push("");
        }
      }
      for (let i = 0; i < daysInMonth[month]; i++) {
        // 正常显示的日期
        calendarDateList.push(i + 1);
      }
      // 当前月份最后一天的自然周剩余日期,置空
      nextNum = 6 - new Date(year, month + 1, 0).getDay(); 
      for (let i = 0; i < nextNum; i++) {
        calendarDateList.push("");
      }
      this.showData = calendarDateList;
      // console.log("showDate:" + JSON.stringify(this.showData));
      return calendarDateList;
    }

这样,就可以显示出来基本的日历样式。

三、点击前一个月按钮

 preMonthClick() {
      this.isActive = -1;
      this.show = true;
      if (this.month === 1) {
        this.month = 12;
        this.year = this.year - 1;
      } else {
        this.month = this.month - 1;
      }
      this.getMonthDays(this.year, this.month - 1);
    },

四、点击下一个月按钮

nextMonthClick() {
      this.isActive = -1;
      let date = new Date();
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      if (this.year == year) {
        this.month = this.month + 1;
        if (this.month < month) {
          this.show = true;
        } else {
          this.show = false;
          this.getMonthDays(this.year, this.month - 1);
          let targetDay = new Date(this.year, this.month - 1, 1).getDay();
          let index = this.today + targetDay - 1;
          this.isActive = index;
        }
      } else if (this.year < year) {
        this.show = true;
        if (this.month === 12) {
          this.month = 1;
          this.year = this.year + 1;
        } else {
          this.month = this.month + 1;
        }
      }
      this.getMonthDays(this.year, this.month - 1);
    } 

因为主要是思路,所以没有上传样式代码。
如有需要,可自行下载。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Vue.js和一些CSS样式实现可折叠日历。 首先,在Vue项目中创建一个组件,用于显示日历。可以使用v-for指令来循环渲染日历的每一天,然后根据需要添加样式和交互行为。 在组件的模板中,你可以使用表格来布局日历,并使用v-for指令循环渲染表格的行和列。每个单元格可以用一个对象来表示,包括日期、事件等信息。 在组件的数据中,你需要定义一个数组来存储日历的数据。你可以使用JavaScript的Date对象来生成日历的日期数据,并将它们添加到数组中。 接下来,你可以为日历添加折叠功能。你可以使用Vue的条件渲染,通过控制一个变量来展开或折叠日历的内容。 最后,你可以为日历的每个日期添加点击事件,以实现一些交互行为,例如显示日期的详细信息或执行相关操作。 以下是一个简单的示例代码: ```vue <template> <div> <button @click="toggleCalendar">{{ isOpen ? '折叠日历' : '展开日历' }}</button> <table v-if="isOpen"> <thead> <tr> <th v-for="day in daysOfWeek" :key="day">{{ day }}</th> </tr> </thead> <tbody> <tr v-for="(week, index) in weeks" :key="index"> <td v-for="day in week" :key="day.date" @click="showDetails(day)"> {{ day.date }} </td> </tr> </tbody> </table> </div> </template> <script> export default { data() { return { isOpen: false, daysOfWeek: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'], weeks: [] } }, methods: { toggleCalendar() { this.isOpen = !this.isOpen; }, showDetails(day) { // 处理点击日期的逻辑 } }, mounted() { // 生成日历数据的逻辑 } } </script> <style scoped> /* 添加样式以美化日历 */ table { width: 100%; } th, td { padding: 10px; text-align: center; } td:hover { cursor: pointer; background-color: #e9e9e9; } </style> ``` 以上代码只是一个简单的示例,你可以根据你的需求进行修改和扩展。希望能对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值