html
<template>
<div>
<h2 class="calenderH2" id="v-step11">
日历
</h2>
<div>
<div class="outerdata" id="dateOuter">
<div class="nowdate clearfix">
<span class="nowdate fl">
<i class="icon iconfont icon-jintian" style="font-size:25px; color:#3169fe;line-height:37px;"></i>
{{newresDate}}
</span>
<span class="addSchedule fr" style="margin-top:20px;">
<el-tooltip effect="dark" content="添加日程" placement="bottom">
<span style="cursor: pointer;height:28px; display:inline-block;outline:0;" >
<i @click="newAdd()" style="font-size: 26px; color:#3169fe; cursor: pointer" class="icon iconfont icon-tianjia"></i>
</span>
</el-tooltip>
</span>
</div>
<div class="attendtittle">
<span class="lastmonth" @click.stop.prevent="lastMonthClick()" @click="getdate(1)">上个月</span>
<span class="todata" @click.prevent="getThisDay()" @click="getdate(0)">今天</span>
<span class="nextmonth" @click.stop.prevent="nextMonthClick()" @click="getdate(1)">下个月</span>
<span style="font-size: 14px;color: #2D2D2D;">{{checkYear}}年</span>
<span style="font-size: 14px;color: #2D2D2D;">{{checkMonth}}月</span>
</div>
<!--表体-->
<div id="attendwrap" class="attendwrap">
<div id="attendwrapleft" style="width: 100%; ">
<!-- 日历头部 -->
<div class="everyWeekDay">
<span v-for="(item,index) in weekDays" :key="index">
<span v-if="item=='日' || item=='六'" class="weekday" style="color:#0089FA;">{{item}}</span>
<span class="weekday" style="color:#888;" v-else>{{item}}</span>
</span>
</div>
<!-- 日历 -->
<div class="everyDay">
<!--空白站位-->
<span v-for="item1 in ThisWeekDayCounrt" :key="item1" class="days"></span>
<!--这个月的天数-->
<span @click="getdate(item)" v-for="(item,index) in thisDayCount" :key="item.id" :class="{'leftcolorchang':leftcolorDisplay==item}" class="days">
<!-- 当天的日期 -->
<span v-if="checkYear == nowYear && checkMonth == nowMonth && index+1 == nowDay">
<span v-if="cumWeekDayCounrt(index+1) == 6 || cumWeekDayCounrt(index+1) == 0" class="days" style="width:100%; cursor:pointer;border-radius: 100%;">
<span style="display:inline-block; width:30px; line-height:30px; position: relative;">
<span :id='"redChange"+checkYear+checkMonth+(index+1)' class="newscheduleDay"></span>
<span :id='"scheduleChange"+checkYear+checkMonth+(index+1)' class="scheduleDay"></span>
<span :id='"oldScheduleChange"+checkYear+checkMonth+(index+1)' class="oldscheduleDay"></span>
<span style="background-color: rgb(78, 204, 196);color: rgb(255, 255, 255);display: block;width: 30px;height: 30px;line-height: 30px;text-align: center;margin: 0 auto;border-radius: 100%;">
{{index+1}}
</span>
</span>
</span>
<span v-else class="days" style="width:100%; cursor:pointer;border-radius: 100%;">
<span style="display:inline-block; width:30px; line-height:30px; position: relative;">
<span :id='"redChange"+checkYear+checkMonth+(index+1)' class="newscheduleDay"></span>
<span :id='"scheduleChange"+checkYear+checkMonth+(index+1)' class="scheduleDay"></span>
<span :id='"oldScheduleChange"+checkYear+checkMonth+(index+1)' class="oldscheduleDay"></span>
<span style="background-color: rgb(78, 204, 196);color: rgb(255, 255, 255);display: block;width: 30px;height: 30px;line-height: 30px;text-align: center;margin: 0 auto;border-radius: 100%;">
{{index+1}}
</span>
</span>
</span>
</span>
<!-- 点击日期 -->
<span v-else>
<span v-if="cumWeekDayCounrt(index+1) == 6 || cumWeekDayCounrt(index+1) == 0" style="color:#0089FA;cursor:pointer;">
<span class="click" style="display:inline-block; width:30px; line-height:30px; position: relative;">
<span :id='"redChange"+checkYear+checkMonth+(index+1)' class="newscheduleDay"></span>
<span :id='"scheduleChange"+checkYear+checkMonth+(index+1)' class="scheduleDay"></span>
<span :id='"oldScheduleChange"+checkYear+checkMonth+(index+1)' class="oldscheduleDay"></span>
<span>{{index+1}}</span>
</span>
</span>
<span v-else class="days" style="width: 100%; cursor: pointer; border-radius: 100%;">
<span class="click" style="display:inline-block; width:30px; line-height:30px; position: relative;">
<span :id='"redChange"+checkYear+checkMonth+(index+1)' class="newscheduleDay"></span>
<span :id='"scheduleChange"+checkYear+checkMonth+(index+1)' class="scheduleDay"></span>
<span :id='"oldScheduleChange"+checkYear+checkMonth+(index+1)' class="oldscheduleDay"></span>
<span>{{index+1}}</span>
</span>
</span>
</span>
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
js
export default {
data: {
// 日历
date: new Date(),
nowYear: new Date().getFullYear(),
nowMonth: new Date().getMonth() + 1,
nowDay: new Date().getDate(),
weekDays: new Array("日", "一", "二", "三", "四", "五", "六"),
months: new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"),
lastDays: new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
checkYear: "",
checkMonth: "",
thisDayCount: "", //这个月有几天
ThisWeekDayCounrt: "", //这个周有多少天
resData: [],
checkdate: "", //点击的日期
leftcolorDisplay: 0,
nowdate: "",
newgetDate: "",
newgetMonth: "",
newgetYear: "",
newresDate: "",
},
created() {
// 日历
var _this = this;
_this.checkYear = _this.nowYear;
_this.checkMonth = _this.nowMonth;
setTimeout(function() {
// 初始调用服务请求
_this.getThisMonthDay(_this.checkYear, _this.checkMonth);
_this.getThisWeekDay(_this.checkYear, _this.checkMonth, 1);
}, 1000);
this.requreData();
this.newnowday();
},
methods: {
// 加载日历
requreData() {
this.getThisMonthDay(this.checkYear, this.checkMonth);
this.getThisWeekDay(this.checkYear, this.checkMonth, 1);
},
// 判断闰年
isLeapYear(year) {
var isLeap = false;
if (0 == year % 4 && (year % 100 != 0 || year % 400 == 0)) {
isLeap = true;
}
return isLeap;
},
// 闰年2月有多少天
getThisMonthDay(year, month) {
var thisDayCount = this.lastDays[month - 1];
if (month == 2 && this.isLeapYear(year)) {
thisDayCount++;
}
this.thisDayCount = thisDayCount;
},
// 本月几号是星期几的索引
getThisWeekDay(year, month, date) {
var thisDate = new Date(year, month - 1, date);
this.ThisWeekDayCounrt = thisDate.getDay();
},
//每号是星期几的索引
cumWeekDayCounrt(date) {
var thisDate = new Date(this.checkYear, this.checkMonth - 1, date);
return thisDate.getDay();
},
// 日历
newnowday() {
var d = this.date;
this.newgetDate = d.getDate();
this.newgetMonth = d.getMonth() + 1;
this.newgetYear = d.getFullYear();
this.newresDate = this.newgetYear + "-" + (this.newgetMonth < 10 ? String("0" + this.newgetMonth) : this.newgetMonth) + "-" + (this.newgetDate < 10 ? String("0" + this.newgetDate) : this.newgetDate)
this.getAtten(this.newresDate);
},
// 日历日期
getAtten(checkdate) {
this.checkYear = checkdate.slice(0, 4)
var regex = /^[0]+/
this.checkMonth = checkdate.slice(5, 7).replace(regex, "");
var checkDay = checkdate.slice(8, 10).replace(regex, "");
this.getThisMonthDay(this.checkYear, this.checkMonth);
this.getThisWeekDay(this.checkYear, this.checkMonth, 1);
if (this.leftcolorDisplay != checkDay) {
this.leftcolorDisplay = checkDay
};
this.getAttenData(checkdate);
},
// 点击日期
getdate(item) {
// 控制红点位置,item是几红点就在几
this.leftcolorDisplay = item;
this.checkdate = this.checkYear + "-" + (this.checkMonth < 10 ? String("0" + this.checkMonth) : this.checkMonth) + "-" + (item < 10 ? String("0" + item) : item);
console.log('getdate', this.checkdate)
},
// 切换今天的数据
getThisDay() {
//返回今天 重新获取今天的数据渲染页面;
this.checkYear = this.nowYear;
this.checkMonth = this.nowMonth;
this.requreData();
this.checkdate = this.newgetYear + "-" + (this.newgetMonth < 10 ? String("0" + this.newgetMonth) : this.newgetMonth) + "-" + (this.newgetDate < 10 ? String("0" + this.newgetDate) : this.newgetDate)
console.log('getThisDay', this.checkdate)
this.getAttenData(this.checkdate);
},
//切换上一个月
lastMonthClick() {
if (this.checkMonth == 1) {
this.checkYear = Number(this.checkYear) - 1;
this.checkMonth = 12;
} else {
this.checkMonth = parseInt(this.checkMonth) - 1;
}
this.requreData();
this.checkdate = this.checkYear + "-" + (this.checkMonth < 10 ? String("0" + this.checkMonth) : this.checkMonth) + "-01";
console.log('lastMonthClick', this.checkdate);
this.getAttenData(this.checkdate);
},
//切换下一个月
nextMonthClick() {
if (this.checkMonth == 12) {
this.checkYear = Number(this.checkYear) + 1;
this.checkMonth = 1;
} else {
this.checkMonth = parseInt(this.checkMonth) + 1;
}
this.requreData();
//请求数据 this.http;
this.checkdate = this.checkYear + "-" + (this.checkMonth < 10 ? String("0" + this.checkMonth) : this.checkMonth) + "-01";
console.log('nextMonthClick', this.checkdate);
this.getAttenData(this.checkdate);
},
// 获取数据
getAttenData(checkdate) {
console.log('传日期参数调用接口');
}
}
}
css
<style>
/* 日历 */
.attendwrap {
padding: 10px 0;
border-bottom: 1px solid #e6e6eb;
height: 250px;
}
.outerdata {
width: 100%;
border-radius: 5px;
background-color: #fff;
}
.attendtittle {
line-height: 51px;
border-bottom: 1px solid #e6e6eb;
}
.nowdate {
font-size: 16px;
color: #333;
font-weight: bold;
margin: 15px 0;
}
.todata {
width: 44px;
height: 24px;
border: 1px solid #e6e6eb;
padding: 5px 2%;
cursor: pointer;
border-radius: 4px;
font-size: 14px;
color: #333;
margin-left: 1.5%;
}
.lastmonth {
color: #333;
cursor: pointer;
padding: 5px 2%;
border: 1px solid #e6e6eb;
border-radius: 5px;
}
.nextmonth {
margin-left: 1.5%;
margin-right: 5%;
border-radius: 5px;
color: #333;
cursor: pointer;
padding: 5px 2%;
border: 1px solid #e6e6eb;
}
.everyWeekDay .weekday {
display: -moz-inline-box;
display: inline-block;
text-align: center;
/*height: 20px;*/
line-height: 20px;
width: 13.6%;
border: 1px solid transparent;
cursor: pointer;
box-sizing: border-box;
}
.everyDay .days {
/*解决span不支持width属性*/
display: -moz-inline-box;
display: inline-block;
text-align: center;
height: 35px;
line-height: 35px;
width: 13.6%;
border: 1px solid transparent;
cursor: pointer;
box-sizing: border-box;
}
.leftcolorchang .click {
display: inline-block;
color: #f56c6c;
background-color: #f56c6c;
color: #ffffff !important;
height: 30px;
width: 30px;
line-height: 30px;
margin: 0 auto;
border-radius: 100%;
}
.attenint {
padding-top: 15px;
}
.attenint .attendata {
padding: 0 0 15px;
height: 20px;
line-height: 20px;
}
.attenint .attendance {
padding: 0 0 15px;
height: 20px;
line-height: 20px;
border-bottom: solid 1px #e6e6eb;
}
.scheduleTit {
margin-top: 13px;
height: 20px;
line-height: 20px;
vertical-align: middle;
}
</style>