用vue写一个日历


Posted in Javascript onNovember 02, 2020

之前在上家公司做过一个公司人员考勤的东西,里面需要用到日历,当时自己用vue随便写了一个,比较简单,删掉了其他功能的代码,只留下日历,直接看代码

<template>
 <div class="lookForMonth_wrapper">
  <div class="lookForMonth_top">
   <div class="selectDate">
    <div>{{year}} 年 {{month}} 月</div>
    <div class="upDownSelect">
     <div class="upDownSelect_item" @click="dateUp"></div>
     <div class="upDownSelect_item" @click="dateDown"></div>
    </div>
   </div>
  </div>
  <div class="calendar" :style="calendarStyle">
   <div v-for="(item,index) in calendarData" class="calendar_item" :key='index' :class="{ash:item.color==='ash',date:index>6&&item.color!=='ash'}">
    <p class="dateEdit">{{item.label}}<i class="el-icon-edit-outline" v-if="item.color!=='ash'&&index>=7"></i></p>
    <p v-if='index>6'>上班</p> // 打工人
   </div>
  </div>
 </div>
</template>
<script>
export default {
 name: "lookForMonth",
 data: () => {
 return {
  calendarData: [{label:"日"},{label: "一"}, {label:"二"},{label: "三"},{label: "四"},{label: "五"},{label: "六"}], //日历循环渲染数据
  year: 0, //当前日期年
  month: 0, //当前日期月数
  date: 0, //当前日期号数
  day: -1, //当前星期几
 };
 },
 filters:{
 },
 computed: {
 // 根据当月日期详情更改日历表格高度
 calendarStyle() {
  if (this.calendarData.length > 42) {
  return "height: 701px;";
  } else {
  return "height: 601px;";
  }
 }
 },
 async created(){
 // 获取当前日期数据
 this.getNow();
 // 获取当前月份一号的时间戳
 let firstTime = +new Date(this.year,this.month-1,1,0,0,0)
 this.getCalendarDate(); // 给calendarData添加当月数据
 },
 mounted() {
 },
 methods: {
 // 获取当前时间
 getNow() {
  let now = new Date()
  this.year = +now.getFullYear()
  this.month = +now.getMonth() + 1
  this.date = +now.getDate()
  this.day = +now.getDay()
 },
 // 获取每个月的天数
 monthDay(month) {
  if ([1,3,5,7,8,10,12].includes(month)) {
  return 31
  } else if ([4,6,9,11].includes(month)) {
  return 30
  } else if (month === 2) {
  // 判断当年是否为闰年
  if (
   (this.year % 4 === 0 && this.year % 100 !== 0) ||
   this.year % 400 === 0
  ) {
   return 29
  } else {
   return 28
  }
  }
 },
 // 给calendarData添加当月数据
 getCalendarDate() {
  // 获取当前月份一号星期几
  let firstDay = new Date(this.year + "-" + this.month + "-" + "01").getDay();
  this.calendarData = [{label:"日"},{label: "一"}, {label:"二"},{label: "三"},{label: "四"},{label: "五"},{label: "六"}];
  let num = parseInt(firstDay);
  let nowDays = this.monthDay(this.month);
  let lastMonth = this.month - 1>0?this.month - 1:12;
  let lastDays = this.monthDay(lastMonth);
  // 循环添加上一个月数据
  for (let i = 0; i < num; i++) {
  this.calendarData.push({label:lastDays - num + i + 1,color:'ash'});
  }
  // 循环添加当月数据
  for (let i = 0; i < nowDays; i++) {
  this.calendarData.push({label:i + 1});
  }
  // 循环添加下一个月数据
  if (this.calendarData.length % 7 !== 0) {
  let surplusDay = 7 - (this.calendarData.length % 7);
  for (let i = 0; i < surplusDay; i++) {
   this.calendarData.push({label:i + 1,color:'ash'});
  }
  }
  this.loading = false
 },
 // 将日期调上
 dateUp() {
  this.month--;
  if (this.month <= 0) {
  this.year--;
  this.month = 12;
  }
  this.getCalendarDate(); // 给calendarData添加当月数据
 },
 // 将日期调下
 dateDown() {
  this.month++;
  if (this.month > 12) {
  this.year++;
  this.month = 1;
  }
  this.getCalendarDate(); // 给calendarData添加当月数据
 },
 }
};
</script>
<style lang="scss" scoped>
.lookForMonth_wrapper {
 padding: 20px;
 width: 701px;
 margin: auto;
}
.lookForMonth_top {
 margin-bottom: 20px;
 overflow: hidden;
 .selectTeacher {
 float: left;
 }
 .selectDate {
 height: 30px;
 line-height: 30px;
 float: right;
 display: flex;
 .upDownSelect {
  display: flex;
  flex-direction: column;
  margin-top: -2px;
  margin-left: 5px;
  .upDownSelect_item {
  width: 0;
  height: 0;
  border: 7px solid transparent;
  cursor: pointer;
  }
  .upDownSelect_item:nth-child(1) {
  border-bottom: 7px solid #666;
  margin-bottom: 5px;
  &:hover {
   border-bottom: 7px solid skyblue;
  }
  }
  .upDownSelect_item:nth-child(2) {
  border-top: 7px solid #666;
  &:hover {
   border-top: 7px solid skyblue;
  }
  }
 }
 }
}
/* 日历表样式=======================================↓ */
.calendar {
 width: 701px;
 border-top: 1px solid #ccc;
 border-left: 1px solid #ccc;
 display: flex;
 flex-wrap: wrap;
 box-sizing: border-box;
 .calendar_item {
 box-sizing: border-box;
 width: 100px;
 height: 100px;
 border-right: 1px solid #ccc;
 border-bottom: 1px solid #ccc;
 text-align: center;
 display: flex;
 flex-direction: column;
 justify-content: center;
 align-items: center;
 &.date:hover{
  background: #eee;
 }
 .status{
  margin-top: 10px;
  &.textBlue{
  color: blue;
  }
  &.textRed{
  color: brown;
  }
 }
 .el-icon-edit-outline{
  cursor: pointer;
  margin-left: 7px;
 }
 }
 .ash{
 color: gainsboro;
 }
 .dateEdit{
 margin-bottom: 10px;
 }
}
</style>

效果如下:

用vue写一个日历

以上就是利用vue写一个日历的详细内容,更多关于vue 日历的资料请关注三水点靠木其它相关文章!

Javascript 相关文章推荐
js中的window.open返回object的错误的解决方法
Aug 15 Javascript
IE6下focus与blur错乱的解决方案
Jul 31 Javascript
javascript对象的使用和属性操作示例详解
Mar 02 Javascript
jQuery使用hide方法隐藏指定元素class样式用法实例
Mar 30 Javascript
分享我对JS插件开发的一些感想和心得
Feb 04 Javascript
在 Angular2 中实现自定义校验指令(确认密码)的方法
Jan 23 Javascript
jQuery实现的简单无刷新评论功能示例
Nov 08 jQuery
浅谈React中组件间抽象
Jan 27 Javascript
jQuery实现左右滑动的toggle方法
Mar 03 jQuery
js捆绑TypeScript声明文件的方法教程
Apr 13 Javascript
新年快乐! javascript实现超级炫酷的3D烟花特效
Jan 30 Javascript
VSCode搭建Vue项目的方法
Apr 30 Javascript
在vue中使用vant TreeSelect分类选择组件操作
Nov 02 #Javascript
vant自定义二级菜单操作
Nov 02 #Javascript
JavaScript动态生成表格的示例
Nov 02 #Javascript
JavaScript实现图片放大预览效果
Nov 02 #Javascript
解决antd 表单设置默认值initialValue后验证失效的问题
Nov 02 #Javascript
在antd4.0中Form使用initialValue操作
Nov 02 #Javascript
vue+iview使用树形控件的具体使用
Nov 02 #Javascript
You might like
iframe的onload在Chrome/Opera中执行两次Bug的解决方法
2011/03/17 Javascript
利用百度地图JSAPI生成h7n9禽流感分布图实现代码
2013/04/15 Javascript
event对象获取方法总结在google浏览器下测试
2013/11/03 Javascript
让IE8浏览器支持function.bind()方法
2014/10/16 Javascript
JavaScript 学习笔记之操作符
2015/01/14 Javascript
jquery模拟进度条实现方法
2015/08/03 Javascript
使用命令行工具npm新创建一个vue项目的方法
2017/12/27 Javascript
详解如何在vue项目中使用layui框架及采坑
2019/05/05 Javascript
使vue实现jQuery调用的两种方法
2019/05/12 jQuery
微信小程序自定义tabBar的踩坑实践记录
2020/11/06 Javascript
解决iview table组件里的 固定列 表格不自适应的问题
2020/11/13 Javascript
[16:56]教你分分钟做大人:司夜刺客
2014/10/30 DOTA
python自动化测试之从命令行运行测试用例with verbosity
2014/09/28 Python
使用python实现http及ftp服务进行数据传输的方法
2018/10/26 Python
Python3实现的简单三级菜单功能示例
2019/03/12 Python
浅谈python常用程序算法
2019/03/22 Python
使用Tkinter制作信息提示框
2020/02/18 Python
Django 解决阿里云部署同步数据库报错的问题
2020/05/14 Python
python 自定义异常和主动抛出异常(raise)的操作
2020/12/11 Python
CSS3不透明度实例讲解
2016/04/26 HTML / CSS
canvas烟花特效锦集
2018/01/17 HTML / CSS
HTML5 Web Workers之网站也能多线程的实现
2013/04/24 HTML / CSS
美国女鞋品牌:naturalizer(娜然)
2016/08/01 全球购物
自考生自我评价分享
2014/01/18 职场文书
环境工程专业自荐信
2014/03/03 职场文书
危爆物品安全大检查大整治工作方案
2014/05/03 职场文书
优秀乡村医生先进事迹材料
2014/08/23 职场文书
家庭困难证明
2014/10/12 职场文书
医学专业大学生职业生涯规划书
2014/10/25 职场文书
企业与个人合作经营协议书
2014/11/01 职场文书
先进集体申报材料
2014/12/25 职场文书
迎新生欢迎词
2015/01/23 职场文书
应届生简历自我评价
2015/03/11 职场文书
Vue实现导入Excel功能步骤详解
2021/07/03 Vue.js
小程序实现侧滑删除功能
2022/06/25 Javascript
Java获取字符串编码格式实现思路
2022/09/23 Java/Android