vue日历且内容自定义

这直接上代码

<template>
  <!-- 月份选择器,支持左右箭头修改月份 -->
  <div class="date-body">
    <div class="month-con">
      <i class="el-icon-arrow-left" @click="reduceMonth()"></i>
      <span class="month"></span>
        {{ time.year }}年{{
        time.month + 1 > 9 ? time.month + 1 : '0' + (time.month + 1)
        }}月
      </span>
      <i class="el-icon-arrow-right" @click="addMonth()"></i>
    </div>
    <div class="date-content">
      <el-row>
        <el-col :span="24">
          <!-- 第一行表头,周一到周日 -->
          <div class="top-con">
            <div class="top" v-for="item in top" :key="item">周{{ item }}</div>
          </div>
          <!-- 日历号 -->
          <div class="date-con">
            <div
              class="date"
              :class="[item.thisMonth, item.isToday, item.afterToday]"
              v-for="(item, index) in visibleCalendar"
              :key="index"
            >
              <div class="date-detials">
                <div class="number">{{ item.day }}</div>
                <div class="morning">
                  <span></span>张三,李四
                </div>
                <div class="evening">
                  <span></span>王五,赵六
                </div>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
  
<script>
export default {
  data() {
    return {
      top: ["一", "二", "三", "四", "五", "六", "日"],
      time: {
        year: new Date().getFullYear(),
        month: new Date().getMonth()
      }
    };
  },
  created() {},
  methods: {
    reduceMonth() {
      if (this.time.month > 0) {
        this.time.month = this.time.month - 1;
      } else {
        this.time.month = 11;
        this.time.year = this.time.year - 1;
      }
      this.$emit("changeMonth", this.time);
    },
    addMonth() {
      if (this.time.month < 11) {
        this.time.month = this.time.month + 1;
      } else {
        this.time.month = 0;
        this.time.year = this.time.year + 1;
      }
      this.$emit("changeMonth", this.time);
    }
  },
  computed: {
    // 获取当前月份显示日历,共42天
    visibleCalendar: function() {
      // 获取今天的年月日
      const today = new Date();
      today.setHours(0);
      today.setMinutes(0);
      today.setSeconds(0);
      today.setMilliseconds(0);

      const calendarArr = [];
      // 获取当前月份第一天
      const currentFirstDay = new Date(this.time.year, this.time.month, 1);
      // 获取第一天是周几,注意周日的时候getDay()返回的是0,要做特殊处理
      const weekDay =
        currentFirstDay.getDay() === 0 ? 7 : currentFirstDay.getDay();
      // 用当前月份第一天减去周几前面几天,就是看见的日历的第一天
      const startDay = currentFirstDay - (weekDay - 1) * 24 * 3600 * 1000;
      // 我们统一用42天来显示当前显示日历
      for (let i = 0; i < 35; i++) {
        const date = new Date(startDay + i * 24 * 3600 * 1000);
        calendarArr.push({
          date: new Date(startDay + i * 24 * 3600 * 1000),
          year: date.getFullYear(),
          month: date.getMonth(),
          day: date.getDate(),
          // 是否在当月
          thisMonth:
            date.getFullYear() === today.getFullYear() &&
            date.getMonth() === today.getMonth()
              ? "thisMonth"
              : "",
          // 是否是今天
          isToday:
            date.getFullYear() === today.getFullYear() &&
            date.getMonth() === today.getMonth() &&
            date.getDate() === today.getDate()
              ? "isToday"
              : "",
          // 是否在今天之后
          afterToday: date.getTime() >= today.getTime() ? "afterToday" : ""
        });
      }
      return calendarArr;
    }
  }
};
</script>
<style lang="scss" scoped>
.date-content {
  height: 260px;
  overflow-y: auto;
  overflow-x: hidden;
}
.month-con {
  height: 40px;
  line-height: 40px;
  padding: 0px 22px;
  text-align: center;
  background: #f0f1f6;
  font-size: 22px;
  color: #333333;
  .month {
    margin: 0 10px;
  }
  .el-icon-arrow-left {
    float: left;
    margin-top: 8px;
    cursor: pointer;
  }
  .el-icon-arrow-right {
    float: right;
    margin-top: 8px;
    cursor: pointer;
  }
}
//
.top-con {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #c0c4cc;
  .top {
    width: 14.285%;
    // background-color: rgb(242, 242, 242);
    padding: 10px 0;
    margin: 5px;
    text-align: center;
  }
}
.date-con {
  display: flex;
  flex-wrap: wrap;
  margin-right: -6px;
  .date {
    width: 14.285%;
    text-align: center;
    margin-bottom: 6px;
    padding-right: 6px;
    .date-detials {
      font-size: 12px;
      font-weight: 400;
      border: 1px solid #4a79ee1c;
      cursor: pointer;
      .number {
        height: 32px;
        line-height: 32px;
      }
      .morning {
        height: 32px;
        line-height: 32px;
        background-color: rgba(74, 121, 238, 0.11);
        color: #4a79ee;
        span {
          display: inline-block;
          width: 5px;
          height: 5px;
          margin-right: 4px;
          border-radius: 3px;
          background: #4a79ee;
        }
      }
      .evening {
        height: 34px;
        line-height: 34px;
        background-color: rgba(248, 154, 108, 0.11);
        color: #f89a6c;
        span {
          display: inline-block;
          width: 5px;
          height: 5px;
          margin-right: 4px;
          border-radius: 3px;
          background: #f89a6c;
        }
      }
    }
  }
  .thisMonth {
    .morning {
      background-color: rgb(220, 245, 253);
    }
    .evening {
      background-color: rgb(220, 244, 209);
    }
  }
  .isToday {
    font-weight: 700;
    .morning {
      background-color: rgb(169, 225, 243);
    }
    .evening {
      background-color: rgb(193, 233, 175);
    }
  }
}
.tip-con {
  margin-top: 51px;
  .tip {
    margin-top: 21px;
    width: 100%;
  }
}
</style>
  
  

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue日历带假期及自定义内容的实现可以通过以下几个步骤来完成。 首先,需要创建一个新的Vue组件来承载日历。在组件的数据中定义一个日历的起始日期和结束日期。同时,还需要创建一个holidayList数组来保存假期的日期。假设我们的日历是以周为单位展示的,可以使用moment.js库来处理日期的计算和格式化。 接下来,在组件的模板中,使用v-for指令遍历所有的周,然后再嵌套一个循环来遍历该周的每一天。通过计算得到的日期,可以判断当前是否为假期日,并添加相应的类名,以便样式展示。 为了实现自定义内容,在模板中,可以在每一天的单元格中使用插槽slot,将自定义内容传递进来。可以在具体的业务场景中,根据需求传递不同的自定义内容,比如可以在某个特定日期显示庆祝活动的标记,或者在特殊假期设置自定义的假期文案等。 最后,在组件的方法中,可以定义一些事件处理函数,如点击某一天触发的事件等,来实现更多的交互功能。 总结起来,实现Vue日历带假期及自定义内容可通过以下几个步骤来完成:定义起始日期和结束日期,保存假期列表,使用v-for和插槽slot循环遍历日期并展示相应的内容和样式,通过事件处理函数实现交互功能。整个实现过程需要结合具体的业务需求来进行,可以根据情况进行个性化定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值