Skip to content

Commit d1dd2d6

Browse files
committed
update
完成月份切换
1 parent de4bd47 commit d1dd2d6

File tree

4 files changed

+127
-55
lines changed

4 files changed

+127
-55
lines changed

.idea/workspace.xml

Lines changed: 43 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DatePicker/css/base.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
-webkit-box-shadow: 2px 2px 8px 2px rgba(128, 128, 128, .3);
66
-moz-box-shadow: 2px 2px 8px 2px rgba(128, 128, 128, .3);
77
box-shadow: 2px 2px 8px 2px rgba(128, 128, 128, .3);
8+
display: none;
9+
position: absolute;
10+
}
11+
12+
.ui-datepicker-wrapper-show{
13+
display: block;
814
}
915

1016
.ui-datepicker-wrapper .ui-datepicker-header{

DatePicker/index.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@
1313
line-height: 24px;
1414
width: 230px;
1515
}
16+
.datepicker:focus{
17+
outline:0 none;
18+
border: 1px solid #1abc9c;
19+
}
1620
</style>
1721
</head>
1822
<body>
1923
<input type="text" class="datepicker" />
20-
<div class="ui-datepicker-wrapper">
21-
22-
</div>
2324
<script src="js/base.js"></script>
2425
<script src="js/main.js"></script>
2526
<script>
26-
datepicker.init(document.querySelector('.ui-datepicker-wrapper'));
27+
datepicker.init('.datepicker');
2728
</script>
2829
</body>
2930
</html>

DatePicker/js/main.js

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
* Created by Jonathan Zhang on 2017/5/23.
33
*/
44
(function () {
5-
var datepicker = window.datepicker;
5+
var datepicker = window.datepicker,
6+
monthDate,
7+
$wrapper;
68

7-
datepicker.buildUi =function (year,month) {
9+
datepicker.buildUi = function (year,month) {
810

9-
var monthDate = datepicker.getMonthData(year,month);
11+
monthDate = datepicker.getMonthData(year,month);
1012

1113
var html ='<div class="ui-datepicker-header">'+
1214
'<a href="#" class="ui-datepicker-btn ui-datepicker-prev-btn">&lt;</a>'+
@@ -33,7 +35,9 @@
3335
if (i%7 === 0){
3436
html += '<tr>';
3537
}
36-
html += '<td>' + date.showDate + '</td>';
38+
html += '<td data-date="'+ monthDate.days[i].date+'">' +
39+
monthDate.days[i].showDate +
40+
'</td>';
3741
if(i%7 === 6)
3842
{
3943
html += '</tr>';
@@ -44,8 +48,70 @@
4448
'</div>';
4549
return html;
4650
};
47-
datepicker.init = function($dom) {
48-
var html =datepicker.buildUi();
49-
$dom.innerHTML = html ;
51+
52+
datepicker.render = function (direction) {
53+
var year,month;
54+
if(monthDate){
55+
year = monthDate.year;
56+
month = monthDate.month;
57+
}
58+
59+
if(direction ==="prev"){
60+
month--;
61+
if(month===0){
62+
month=12;
63+
year--;
64+
}
65+
}
66+
if(direction === 'next') month++;
67+
68+
var html =datepicker.buildUi(year,month);
69+
70+
if(!$wrapper){
71+
$wrapper = document.createElement("div");
72+
$wrapper.className="ui-datepicker-wrapper";
73+
}
74+
75+
$wrapper.innerHTML = html;
76+
document.body.appendChild($wrapper);
77+
};
78+
79+
datepicker.init = function(input) {
80+
datepicker.render();
81+
82+
var $input = document.querySelector(input);
83+
var isOpen = false;
84+
85+
$input.addEventListener('click',function () {
86+
if(isOpen){
87+
$wrapper.classList.remove('ui-datepicker-wrapper-show');
88+
isOpen =false;
89+
}else {
90+
$wrapper.classList.add('ui-datepicker-wrapper-show');
91+
var left = $input.offsetLeft;
92+
var top = $input.offsetTop;
93+
var height = $input.offsetHeight;
94+
$wrapper.style.top = top + height + 2 + 'px';
95+
$wrapper.style.left = left + 'px';
96+
isOpen = true;
97+
}
98+
},false);
99+
100+
$wrapper.addEventListener('click', function (e){
101+
var $target = e.target;
102+
if(! $target.classList.contains('ui-datepicker-btn')) return;
103+
/*上个月*/
104+
if($target.classList.contains('ui-datepicker-prev-btn')){
105+
datepicker.render('prev');
106+
}
107+
/*下个月*/
108+
else if($target.classList.contains('ui-datepicker-next-btn')){
109+
datepicker.render('next');
110+
}
111+
},false);
112+
113+
$wrapper.addEventListener('click',function (e) {
114+
115+
})
50116
};
51117
})();

0 commit comments

Comments
 (0)