首页 > 其他分享 >Vue日历插件

Vue日历插件

时间:2023-10-25 16:59:21浏览次数:37  
标签:flex 插件 Vue 日历 currentYear font Date new currentMonth

<template>
  <div class="page">
    <div class="calendar">
      <div
        style="
          display: flex;
          justify-content: space-between;
          align-items: center;
          border: 1px solid rgb(246, 247, 256);
        "
      >
        <!-- <span
          style="font-size: 30px; margin-left: 50px; cursor: pointer"
          @click="prevDayclick(1)"
        >
        </span> -->
        <li class="header" style="justify-content: center">
          {{ currentYear + "年" + (currentMonth + 1) + "月" }}
        </li>
        <!-- <span
          style="font-size: 30px; margin-right: 50px; cursor: pointer"
          @click="nextDayclick(1)"
          >></span
        > -->
      </div>
      <li class="week">
        <div>一</div>
        <div>二</div>
        <div>三</div>
        <div>四</div>
        <div>五</div>
        <div style="color: red">六</div>
        <div style="color: red">日</div>
      </li>
      <li class="row day">
        <span
          @click="prevDayclick(item)"
          class="date prevDay"
          v-for="item in prevDays"
          :key="'A' + item"
          >{{ item }}</span
        >
        <span
          @click="clickdate(item)"
          v-for="item in currentDays"
          :key="'B' + item"
          class="date nowm"
          :class="{
            jintian: currentDay == item,
          }"
          >{{ item }}</span
        >
        <span
          @click="nextDayclick(item)"
          class="date prevDay"
          v-for="item in nextDays"
          :key="'A' + item"
          >{{ item }}</span
        >
      </li>
    </div>
  </div>
</template>

<script>
export default {
  name: "Calender",
  props: {
    month: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      week: ["日", "一", "二", "三", "四", "五", "六"],
      currentDay: new Date().getDate(),
      currentDay2: new Date().getDate(),
      currentMonth: this.month - 1,
      currentYear: new Date().getFullYear(),
    };
  },
  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, 0).getDay();
      const 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();
      const days = [];
      let number = 0;
      let totalLenght = this.currentDays + this.prevDays.length;
      if (totalLenght > 28 && totalLenght <= 33) {
        while (num < 15) {
          number++;
          days.push(number);
          num++;
        }
      } else {
        while (num < 8) {
          number++;
          days.push(number);
          num++;
        }
      }
      return days.sort();
    },
    zongdate() {
      return (
        this.currentYear + "-" + (this.currentMonth + 1) + "-" + this.currentDay
      );
    },
  },
  methods: {
    clickdate(val) {
      this.currentDay = val;
      if (
        this.currentMonth === new Date().getMonth() &&
        this.currentDay !== new Date().getDate()
      ) {
        document.getElementsByClassName("nowm")[
          this.currentDay2 - 1
        ].style.color = "#3F62AE";
      }
    },
    prevDayclick(val) {
      this.currentDay = val;
      this.currentMonth = Number(this.currentMonth) - 1;
      document.getElementsByClassName("nowm")[
        this.currentDay2 - 1
      ].style.color = "black";
      if (this.currentMonth < 0) {
        this.currentMonth = 11;
        this.currentYear = Number(this.currentYear - 1);
      }
    },
    nextDayclick(val) {
      this.currentDay = val;
      this.currentMonth = Number(this.currentMonth) + 1;
      document.getElementsByClassName("nowm")[
        this.currentDay2 - 1
      ].style.color = "black";
      if (this.currentMonth > 11) {
        this.currentMonth = 0;
        this.currentYear = Number(this.currentYear + 1);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.calendar {
  width: 320px;
  position: relative;

  .header {
    width: 100%;
    display: flex;
    align-items: center;
    height: 40px;
    font-size: 15px;
    padding-top: 10px;
  }
  .week {
    display: flex;
    height: 30px;
    background: #fefefe;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #404040;
    div {
      width: calc(100% / 7);
      text-align: center;
      height: 30px;
      border: 1px solid rgb(246, 247, 256);
    }
  }
  .row {
    width: 100%;
  }
  .day {
    -webkit-box-pack: start;
    -ms-flex-pack: start;
    justify-content: flex-start;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    font-size: 12px;
    display: flex;

    span {
      height: 40px;
      line-height: 40px;
      text-align: center;
      border: 1px solid rgb(246, 247, 256);
    }
    .date {
      display: flex;
      width: calc(100% / 7);
      justify-content: center;
      align-items: center;
      height: 40px;
      line-height: 40px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: black;
    }
    .nowDay {
      background: #404040;
    }
  }
}
.prevDay {
  color: #ccc !important;
}
</style>

标签:flex,插件,Vue,日历,currentYear,font,Date,new,currentMonth
From: https://www.cnblogs.com/guozhiqiang/p/17787585.html

相关文章

  • 关于在vue 中翻译select 下拉数据的操作
    可以使用object.keys()import*asfiltersfrom"./filters";//globalfiltersObject.keys(filters).forEach((key)=>{Vue.filter(key,filters[key]);//eslint-disable-lineno-undef});<el-select:value="detailRow.prpLpayeeD......
  • Vue 中 axios 的使用和跨域问题的解决
    一、内容:1.Axios是一个基于promise的HTTP库,类似于jQuery的ajax,用于http请求。axios并不是vue插件,所以不能使用Vue.use()。2.它既可以应用于浏览器端,也可以应用于node.js编写的服务端。3.Axios具有以下特性: (1)支持PromiseAPI。 (2)拦截请求与响应,比如:在请求前......
  • Vue中 使用 Scss 实现配置、切换主题
    1.样式文件目录介绍本项目中的公共样式文件均位于src/assets/css目录下,其中index.scss是总的样式文件的汇总入口,common.scss是供全局使用的一些基本样式(常量),_theme.scss、_handle.scss两个文件是进行主题颜色配置的文件。如下图,将index.scss在main.js文件中引入即可全......
  • vue实现动态展开与折叠内联块元素
    功能需求当多个内联块元素(比如div)在一个固定宽度的父元素内自动排列换行时,如果这些元素的行数超过四行,那么默认折叠超出的部分,并在第四行末尾显示一个按钮供用户切换展开或折叠状态。在折叠状态下,为了确保展开/折叠按钮能够显示在第四行末尾,会隐藏第四行的最后一个元素。在展开状......
  • 29-Vue脚手架-mixin 混入
    mixin混入功能:可以把多个组件共用的配置提取成一个混入对象使用混合:1)第一步,定义混入新建一个JS文件,比如mixin.jssrc/mixin.js//分别暴露exportconsthunhe1={methods:{showName(){alert(this.name)}},mounted(){......
  • vue3 watch 用法
    <scriptsetup>import{ref,computed,watch}from'vue'constnum=ref(1)constname=ref('ming')constobj=ref({name:'小明',age:30})//watch简单类型//watch(num,(newValue,oldVal......
  • [Vue]computed和watch的区别
    computed和watch之间的区别: 1.computed能完成的功能,watch都可以完成。 2.watch能完成的功能,computed不一定能完成,例如:watch可以进行异步操作。两个重要的小原则: 1.所有被Vue管理的函数,最好写成普通函数,这样this的指向才是vm或组件实例对象。 2.所有不......
  • vue中如何使用svg,以及碰到的相应问题
    安装cnpminstallsvg-sprite-loader--save-dev创建svg文件夹存放svg图标创建icons文件夹,在icons文件夹下创建svg文件夹存放本地svg图标。vue.config.js中配置svg图片config.module.rule("svg").exclude.add(resolve("src/icons")).end();config.module.rule......
  • vue3 elementplus table表格内添加checkbox和行号
    1.仅添加复选框<el-table-columntype="selection"width="55"></el-table-column>2.添加复选框和文字行号在一列<el-table-column><template#header><el-checkboxv-model="selectAll"@change="han......
  • vue 首次加载项目,控制台报错: Redirected when going from "/" to "/login"
    第一次加载加载页面时报错如下:Redirectedwhengoingfrom"/"to"/login"viaanavigationguard. ![image](https://img2023.cnblogs.com/blog/1880163/202310/1880163-20231025113840444-1010075971.png)后续在地址栏直接添加/login,index,错误页面等均正常无报错.路由......