Skip to content

Commit 6f22d13

Browse files
committed
日期选择
1 parent cea4c92 commit 6f22d13

4 files changed

Lines changed: 302 additions & 0 deletions

File tree

‎日期选择器/data.js‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
(function(){
2+
var datepicker = {};
3+
datepicker.getMonthDate = function (year, month) {
4+
var ret = [];
5+
if(!year || !month){
6+
var today = new Date();
7+
year = today.getFullYear();
8+
month = today.getMonth() + 1;
9+
}
10+
var firstDay = new Date(year, month-1, 1);//获取当月第一天
11+
var firstDayWeekDay = firstDay.getDay();//获取星期几,才好判断排在第几列
12+
if(firstDayWeekDay === 0){//周日
13+
firstDayWeekDay = 7;
14+
}
15+
16+
year = firstDay.getFullYear();
17+
month = firstDay.getMonth() + 1;
18+
19+
var lastDayOfLastMonth = new Date(year, month-1, 0);//获取最后一天
20+
var lastDateOfLastMonth = lastDayOfLastMonth.getDate();
21+
22+
var preMonthDayCount = firstDayWeekDay - 1;
23+
var lastDay = new Date(year, month, 0);
24+
var lastDate = lastDay.getDate();
25+
26+
for(var i=0; i<7*6; i++){
27+
var date = i + 1 - preMonthDayCount;
28+
var showDate = date;
29+
var thisMonth = month;
30+
//上一月
31+
if(date <= 0){
32+
thisMonth = month - 1;
33+
showDate = lastDateOfLastMonth + date;
34+
}else if(date > lastDate){
35+
//下一月
36+
thisMonth = month + 1;
37+
showDate = showDate -lastDate;
38+
}
39+
if(thisMonth === 0){
40+
thisMonth = 12;
41+
}
42+
if(thisMonth === 13){
43+
thisMonth = 1;
44+
}
45+
ret.push({
46+
month: thisMonth,
47+
date: date,
48+
showDate: showDate
49+
})
50+
51+
}
52+
return {
53+
year: year,
54+
month:month,
55+
days: ret
56+
};
57+
}
58+
window.datepicker = datepicker;//该函数唯一暴露的对象
59+
})();

‎日期选择器/index.html‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>日期选择器</title>
8+
<link rel="stylesheet" href="style.css">
9+
<style>
10+
.datepicker {
11+
border: 1px solid #ccc;
12+
border-radius: 4px;
13+
width: 230px;
14+
padding: 5px;
15+
line-height: 24px;
16+
}
17+
.datepicker:focus {
18+
outline: none;
19+
border: 1px solid #1abc9c;
20+
}
21+
</style>
22+
</head>
23+
<body>
24+
<input type="text" class="datepicker">
25+
</body>
26+
<script src="./data.js"></script>
27+
<script src="./main.js"></script>
28+
<script>
29+
// var monthDate = datepicker.getMonthDate(2019, 2);
30+
// console.log(monthDate);
31+
// datepicker.init(document.querySelector('.ui-datepicker-wrapper'))
32+
datepicker.init('.datepicker');
33+
</script>
34+
</html>

‎日期选择器/main.js‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
(function () {
2+
3+
var datepicker = window.datepicker;
4+
5+
var monthData;
6+
var $wrapper;
7+
//渲染函数,由于没有使用第三方插件或库,所以使用的是模板拼接的方法
8+
datepicker.buildUi = function (year, month) {
9+
monthData = datepicker.getMonthDate(year, month);
10+
var html = '<div class="ui-datepicker-header">' +
11+
'<a href="#" class="ui-datepicker-btn ui-datepicker-prev-btn">&lt;</a>' +
12+
'<a href="#" class="ui-datepicker-btn ui-datepicker-next-btn">&gt;</a>' +
13+
'<span class="datepicker-curr-month">'+monthData.year+'-'+monthData.month+'</span>' +
14+
'</div>' +
15+
'<div class="ui-datepicker-body">' +
16+
'<table>' +
17+
'<thead>' +
18+
'<tr>' +
19+
'<th>一</th>' +
20+
'<th>二</th>' +
21+
'<th>三</th>' +
22+
'<th>四</th>' +
23+
'<th>五</th>' +
24+
'<th>六</th>' +
25+
'<th>日</th>' +
26+
'</tr>' +
27+
'</thead>' +
28+
'<tbody>';
29+
30+
for (var i = 0; i < monthData.days.length; i++) {
31+
var date = monthData.days[i];
32+
if (i % 7 === 0) {
33+
html += '<tr>';
34+
}
35+
html += '<td data-date="'+date.date+'">' + date.showDate + '</td>';
36+
if (i % 7 === 6) {
37+
html += '</tr>';
38+
}
39+
}
40+
41+
html+='</tbody>' +
42+
'</table>'+
43+
'</div>';
44+
return html;
45+
};
46+
//日历渲染函数
47+
datepicker.render = function (direction) {
48+
var year, month;
49+
if (monthData) {
50+
year = monthData.year;
51+
month = monthData.month;
52+
}
53+
54+
if (direction === 'prev') month--;
55+
if (direction === 'next') month++;
56+
57+
var html = datepicker.buildUi(year,month);
58+
59+
$wrapper=document.querySelector('.ui-datepicker-wrapper');
60+
61+
if(!$wrapper){
62+
$wrapper = document.createElement('div');
63+
$wrapper.className = 'ui-datepicker-wrapper';
64+
}
65+
66+
$wrapper.innerHTML = html;
67+
68+
document.body.appendChild($wrapper);
69+
70+
};
71+
//初始换函数
72+
datepicker.init = function (input) {
73+
datepicker.render();
74+
75+
var $input=document.querySelector(input);
76+
var isOpen=false;
77+
//给input框赋予点击事件
78+
$input.addEventListener('click',function(){
79+
if(isOpen){
80+
$wrapper.classList.remove('ui-datepicker-wrapper-show');
81+
isOpen=false;
82+
}else{
83+
$wrapper.classList.add('ui-datepicker-wrapper-show');
84+
85+
var left=$input.offsetLeft;
86+
var top=$input.offsetTop;
87+
var height=$input.offsetHeight;
88+
89+
$wrapper.style.top=top+height+2+'px';
90+
$wrapper.style.left=left+'px';
91+
92+
isOpen=true;
93+
}
94+
},false);
95+
//给按钮添加点击事件
96+
$wrapper.addEventListener('click',function(e){
97+
var $target=e.target;
98+
99+
if(!$target.classList.contains('ui-datepicker-btn')){
100+
return false;
101+
}
102+
103+
//上一月,下一月
104+
if($target.classList.contains('ui-datepicker-prev-btn')){
105+
datepicker.render('prev');
106+
}else if($target.classList.contains('ui-datepicker-next-btn')){
107+
datepicker.render('next');
108+
}
109+
},false);
110+
111+
$wrapper.addEventListener('click',function(e){
112+
var $target= e.target;
113+
114+
if($target.tagName.toLocaleLowerCase()!=='td'){
115+
return false;
116+
}
117+
118+
var date=new Date(monthData.year,monthData.month-1,$target.dataset.date);
119+
120+
$input.value=format(date);
121+
122+
$wrapper.classList.remove('ui-datepicker-wrapper-show');
123+
isOpen=false;
124+
125+
},false);
126+
};
127+
//格式化数据
128+
function format(date){
129+
var ret='';
130+
131+
var padding=function(num){
132+
if(num<=9){
133+
return '0'+num;
134+
}
135+
return num;
136+
};
137+
138+
ret+=date.getFullYear()+'-';
139+
ret+=padding(date.getMonth()+1)+'-';
140+
ret+=padding(date.getDate());
141+
142+
return ret;
143+
}
144+
145+
})();

‎日期选择器/style.css‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* 最外层区域 */
2+
.ui-datepicker-wrapper {
3+
width: 240px;
4+
font-size: 16px;
5+
color: #666666;
6+
box-shadow: 2px 2px 8px 2px rgba(128, 128, 128, 0.3);
7+
display: none;
8+
position: absolute;
9+
}
10+
.ui-datepicker-wrapper-show {
11+
display: block;
12+
}
13+
14+
/* 头部区域 */
15+
.ui-datepicker-wrapper .ui-datepicker-header {
16+
padding: 0 20px;
17+
height: 50px;
18+
line-height: 50px;
19+
text-align: center;
20+
background: #f0f0f0;
21+
border-bottom: 1px solid #cccccc;
22+
font-weight: bold;
23+
}
24+
/* 设置两个按钮 */
25+
.ui-datepicker-wrapper .ui-datepicker-btn {
26+
font-family: serif;
27+
font-size: 20px;
28+
width: 20px;
29+
height: 50px;
30+
line-height: 50px;
31+
color: #1abc9c;
32+
text-align: center;
33+
cursor: pointer;
34+
text-decoration: none;
35+
}
36+
.ui-datepicker-wrapper .ui-datepicker-prev-btn {
37+
float: left;
38+
}
39+
.ui-datepicker-wrapper .ui-datepicker-next-btn {
40+
float: right;
41+
}
42+
/* body区域 */
43+
.ui-datepicker-wrapper .ui-datepicker-body table {
44+
width: 100%;
45+
border-collapse: collapse;
46+
}
47+
/* 表头和正文 */
48+
.ui-datepicker-wrapper .ui-datepicker-body th,
49+
.ui-datepicker-wrapper .ui-datepicker-body td {
50+
height: 30px;
51+
text-align: center;
52+
}
53+
.ui-datepicker-wrapper .ui-datepicker-body th {
54+
font-size: 12px;
55+
height: 40px;
56+
line-height: 40px;
57+
}
58+
/* 表格部分 */
59+
.ui-datepicker-wrapper .ui-datepicker-body td {
60+
border: 1px solid #f0f0f0;
61+
font-size: 10px;
62+
width: 14%;
63+
cursor: pointer;
64+
}

0 commit comments

Comments
 (0)