纯js日历插件

成品的效果图

纯js日历插件

1.HTML文件


<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>中兴城-考勤信息</title>
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
<meta name="keywords" content="中兴城">
<meta name="description" content="中兴城">
<meta name="format-detection" content="telephone=no,email=no,adress=no">
<!-- 防止UC浏览器字体放大 -->
<meta name="wap-font-scale" content="no">
<!-- 针对手持设备优化,主要是针对一些老的不识别viewport的浏览器,比如黑莓 -->
<meta name="HandheldFriendly" content="true">
<!-- 微软的老式浏览器 -->
<meta name="MobileOptimized" content="320">
<!-- uc强制竖屏 -->
<meta name="screen-orientation" content="portrait">
<!-- QQ强制竖屏 -->
<meta name="x5-orientation" content="portrait">
<!-- UC强制全屏 -->
<meta name="full-screen" content="yes">
<!-- QQ强制全屏 -->
<meta name="x5-fullscreen" content="true">
<!-- UC应用模式 -->
<meta name="browsermode" content="application">
<!-- QQ应用模式 -->
<meta name="x5-page-mode" content="app">
<!-- windows phone 点击无高光 -->
<meta name="msapplication-tap-highlight" content="no">
<!-- 适应移动端end -->
<link href="https://cdn.bootcss.com/normalize/7.0.0/normalize.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/kaoqin.css">
<link rel="stylesheet" href="css/common.css">
</head> <body>
<!-- 主内容 -->
<div class="container">
<!-- 分类 -->
<div class="banner_kq">
<div class="control_">
<a href="javascript:;" class="preMonth"><img src="data:images/to_left_swiper_icon.png" alt=""></a>
<span class="date_">2017年12月</span>
<a href="javascript:;" class="nextMonth"><img src="data:images/to_right_swiper_icon.png" alt=""></a>
</div>
<div class="show_info">
<div class="flex">
<div class="item">
<div class="circle"><span></span>27</div>应出勤</div>
<div class="item">
<div class="circle"><span></span>26</div>实际出勤</div>
<div class="item">
<div class="circle"><span></span>2</div>迟到</div>
<div class="item">
<div class="circle"><span></span>1</div>早退</div>
</div>
</div>
</div>
<ul class="calendT">
<!-- late:迟到 error:早退 errorLate:迟到及早退 today: 今天-->
<li>
<span>周一</span>
<span>周二</span>
<span>周三</span>
<span>周四</span>
<span>周五</span>
<span>周六</span>
<span>周七</span>
</li>
</ul>
<ul class="calendC">
<li>
<span></span>
<span></span>
<span></span>
<span></span>
<span>1</span>
<span>2</span>
<span>3</span>
</li>
<li>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span class="late">8</span>
<span>9</span>
<span>10</span>
</li>
<li>
<span class="error">11</span>
<span>12</span>
<span class="today">13</span>
<span>14</span>
<span>15</span>
<span>16</span>
<span>17</span>
</li>
<li>
<span>18</span>
<span class="errorLate">19</span>
<span>20</span>
<span>21</span>
<span>22</span>
<span>23</span>
<span>24</span>
</li>
<li>
<span>25</span>
<span>26</span>
<span>27</span>
<span>28</span>
<span>29</span>
<span>30</span>
<span>31</span>
</li>
<li>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</li>
</ul>
<div class="show_tips">
<span>今日</span><span>休息日</span><span>迟到/早退</span>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script src="js/kaoqin.js"></script>
</body> </html>

2.CSS文件

ul {
list-style: none;
padding: 0;
margin: 0;
background: #fff;
}
.calendT {}
.calendT li {
color: #999999;
font-size: .13rem;
text-align: center;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
line-height: .4rem;
border-bottom: 1px solid #eee;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
.calendT li span{-webkit-box-flex: 1;-ms-flex-positive: 1;flex-grow: 1;}
.calendC {}
.calendC li {
text-align: center;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
font-size: .13rem;
color: #666666;
line-height: calc(100vw/7);
height: calc(100vw/7);
}
.calendC li span{
position: relative;
width: calc(100vw/7);
border-right: 1px solid #eee;
border-bottom: 1px solid #eee;
}
.calendC li span:nth-child(6),
.calendC li span:nth-child(7){
background: #f3efdd;
}
.calendC span::before{
position: absolute;
top: .02rem;
left: .02rem;
line-height: 1;
}
.error,.late,.errorLate {
background:#ebb5b5;
}
.error::before {
content: "退";
font-size: .12rem;
color: #bf1515;
}
.late::before {
content: "迟";
font-size: .12rem;
color: #bf1515;
}
.errorLate::before {
content: "迟/退";
font-size: .12rem;
color: #bf1515;
position: absolute;
}
.today {
background: #ccd3f3;
}

3.js文件

function Dragon() {
this.init();
}
Dragon.prototype = {
nowDate: null,
showDate: new Date(),
init: function(date) {
var date = date || this.showDate;
var year = date.getFullYear() + "-" + ((date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getDate() + 1)) + "-" + ((date.getDate()) > 9 ? (date.getDate()) : "0" + (date.getDate()));
var currentDays = this.getMonthDays(date);
var prevDays = this.getPrevMonthDays();
var nextDays = this.getNextMonthDays();
var firstDay = currentDays.firstDay;
var indexRow = 0;
html = "<li>";
for (let i = 1; i < firstDay; i++) {
html += "<span></span>";
}
for (let i = 1; i <= currentDays.days; i++) {
html += "<span>" + i + "</span>";
if ((i + firstDay - 1) % 7 == 0) {
html += "</li><li>";
indexRow++;
}
}
if (indexRow == 4) {
var cols = 7 - (firstDay + currentDays.days - 1) % 7;
for (let i = 1; i <= cols; i++) {
html += "<span></span>";
}
html += "</li><li><span></span><span></span><span></span><span></span><span></span><span></span><span></span></li>";
}
if (indexRow == 5) {
var cols = 7 - (firstDay + currentDays.days - 1) % 7;
for (let i = 1; i <= cols; i++) {
html += "<span></span>";
}
html += "</li>";
}
document.querySelector(".calendC").innerHTML = html;
console.log(year);
},
getMonthDays: function(date, str) { //获取传入时间当前的月份有多少天(不传时间就是获取当前月份的天数)
var str = str === undefined ? 0 : str;
var date = date || new Date();
if (str == 0) {
this.showDate = date;
}
date.setDate(1);
var firstDay = date.getDay();
var monthStart = date.getTime();
date.setMonth(date.getMonth() + 1);
var monthEnd = date.getTime();
var obj = {};
var days = Math.ceil((monthEnd - monthStart) / (24 * 60 * 60 * 1000));
date.setMonth(date.getMonth() - 1)
obj = {
days: days,
firstDay: firstDay == 0 ? 7 : firstDay
}
return obj;
},
getCurrentMonthDays: function() { //获取当前月份的天数
var date = new Date();
if (this.nowDate == null) {
this.nowDate = date;
}
date.setDate(1);
var firstDay = date.getDay();
var monthStart = date.getTime();
date.setMonth(date.getMonth() + 1);
var monthEnd = date.getTime();
var obj = {};
var days = Math.ceil((monthEnd - monthStart) / (24 * 60 * 60 * 1000));
date.setMonth(date.getMonth() - 1);
obj = {
days: days,
firstDay: firstDay == 0 ? 7 : firstDay
}
return obj;
},
getNextMonthDays: function() { //获取下一个月的天数
var date = this.showDate;
date.setMonth(date.getMonth() + 1);
this.getMonthDays(date, 1);
date.setMonth(date.getMonth() - 1);
},
getPrevMonthDays: function() { //获取上一个月的天数
var date = this.showDate;
date.setMonth(date.getMonth() - 1);
this.getMonthDays(date, -1);
date.setMonth(date.getMonth() + 1);
},
nextMonth: function() { //显示下一个月的时间
var date = this.showDate;
date.setMonth(date.getMonth() + 1);
this.init(date);
},
prevMonth: function() { //显示上一个月的时间
var date = this.showDate;
date.setMonth(date.getMonth() - 1);
this.init(date);
}
}
var calend = new Dragon();
$(".preMonth").on("click", function() {
calend.prevMonth();
})
$(".nextMonth").on("click", function() {
calend.nextMonth();
})
上一篇:原生JS日历 + JS格式化时间格式


下一篇:setContentType与setCharacterEncoding的区别