<main class="main-wrapper">
<div class="calendar-control">
<button type="button" id="js-calendar-prev" class="calendar-control__prev" aria-label="前の月のカレンダーを表示">前の月へ</button>
<button type="button" id="js-calendar-next" class="calendar-control__next" aria-label="次の月のカレンダーを表示">次の月へ</button>
</div>
<table class="calendar">
<!-- 現在年月をJavaScriptで挿入 -->
<caption id="name" class="caption">YYYY年MM月のカレンダー</caption>
<thead class="table-head">
<tr>
<th>月</th>
<th>火</th>
<th>水</th>
<th>木</th>
<th>金</th>
<th class="saturday">土</th>
<th class="sunday">日</th>
</tr>
</thead>
<tbody id="calendar-body" class="table-body">
<!-- tr要素以下をJavaScriptで動的にカレンダーUIを挿入 -->
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</main>
<main class="main-wrapper">
<div class="calendar-control">
<button type="button" id="js-calendar-prev" class="calendar-control__prev" aria-label="前の月のカレンダーを表示">前の月へ</button>
<button type="button" id="js-calendar-next" class="calendar-control__next" aria-label="次の月のカレンダーを表示">次の月へ</button>
</div>
<table class="calendar">
<!-- 現在年月をJavaScriptで挿入 -->
<caption id="name" class="caption">YYYY年MM月のカレンダー</caption>
<thead class="table-head">
<tr>
<th>月</th>
<th>火</th>
<th>水</th>
<th>木</th>
<th>金</th>
<th class="saturday">土</th>
<th class="sunday">日</th>
</tr>
</thead>
<tbody id="calendar-body" class="table-body">
<!-- tr要素以下をJavaScriptで動的にカレンダーUIを挿入 -->
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</main>
/* No context defined. */
/* /js/modules/Calendar.js */
export function calendar() {
const week = ['月', '火', '水', '木', '金', '土', '日'];
const date = new Date();
const name = document.querySelector('#name');
const buttonPrev = document.getElementById('js-calendar-prev');
const buttonNext = document.getElementById('js-calendar-next');
const showDate = new Date(date.getFullYear(), date.getMonth(), 1);
const calendarBody = document.querySelector('#calendar-body');
if(name) {
showProcess(date, calendar);
buttonPrev.addEventListener('click', () => {
prev();
});
buttonNext.addEventListener('click', () => {
next();
});
}
// 前の月表示
function prev() {
showDate.setMonth(showDate.getMonth() - 1);
showProcess(showDate);
}
// 次の月表示
function next() {
showDate.setMonth(showDate.getMonth() + 1);
showProcess(showDate);
}
// カレンダー表示
function showProcess(date) {
let year = date.getFullYear();
let month = date.getMonth();
name.innerHTML = year + '年' + (month + 1) + '月のカレンダー';
let calendar = createProcess(year, month);
calendarBody.innerHTML = calendar;
}
// カレンダー作成
function createProcess(year, month) {
// 曜日
let calendar = '';
calendar += '';
let count = 0;
let startDayOfWeek = new Date(year, month, 0).getDay();
let endDate = new Date(year, month + 1, 0).getDate();
let lastMonthEndDate = new Date(year, month, 0).getDate();
let row = Math.ceil((startDayOfWeek + endDate) / week.length);
// 1行ずつ設定
for (let i = 0; i < row; i++) {
calendar += '<tr>';
// 1colum単位で設定
for (let j = 0; j < week.length; j++) {
if (i == 0 && j < startDayOfWeek) {
// 1行目で1日まで先月の日付を設定
calendar += '<td class="prev-month-day">' + (lastMonthEndDate - startDayOfWeek + j + 1) + '</td>';
} else if (count >= endDate) {
// 最終行で最終日以降、翌月の日付を設定
count++;
calendar += '<td class="next-month-day">' + (count - endDate) + '</td>';
} else {
// 当月の日付を曜日に照らし合わせて設定
count++;
if(year == date.getFullYear()
&& month == (date.getMonth())
&& count == date.getDate()){
calendar += '<td class="today"><span class="deco">' + count + '</span></td>';
} else {
calendar += '<td>' + count + '</td>';
}
}
}
calendar += '</tr>';
}
return calendar;
}
}
.calendar-control {
display: flex;
justify-content: center;
}
.calendar-control__prev,
.calendar-control__next {
position: relative;
transition: transform .3s var(--ease-out-quad);
}
@media screen and (max-width:64em) {
.calendar-control__prev,
.calendar-control__next {
width: 8rem;
height: 2rem;
}
}
@media print, screen and (min-width:64.0625em) {
.calendar-control__prev,
.calendar-control__next {
width: 10rem;
height: 2.5rem;
}
}
.calendar-control__prev:before,
.calendar-control__next:before {
content: "";
display: block;
position: absolute;
top: 50%;
border-color: var(--border-color);
border-style: solid;
backface-visibility: hidden;
}
@media screen and (max-width:64em) {
.calendar-control__prev:before,
.calendar-control__next:before {
width: 1.25rem;
height: 1.25rem;
border-width: 0 2px 2px 0;
}
}
@media print, screen and (min-width:64.0625em) {
.calendar-control__prev:before,
.calendar-control__next:before {
width: 1.5rem;
height: 1.5rem;
border-width: 0 3px 3px 0;
}
}
.calendar-control__prev:before {
left: .5rem;
transform: translateY(-50%) rotate(135deg);
}
.calendar-control__next:before {
right: .5rem;
transform: translateY(-50%) rotate(-45deg);
}
.calendar {
width: 100%;
}
.main-wrapper > .calendar {
height: 70vh;
}
.calendar > .caption {
caption-side: top;
font-size: 1.2rem;
font-weight: bold;
color: var(--black);
}
.calendar > .table-head > tr > th {
width: 14.2857142857%;
padding: .5rem;
border: .025rem solid var(--border-color);
background-color: #f2f2f2;
text-align: center;
}
.calendar > .table-head > tr > th.saturday {
color: #0d1a54;
}
.calendar > .table-head > tr > th.sunday {
color: #e60026;
}
.calendar > .table-body > tr > td {
width: 14.2857142857%;
height: 14.2857142857%;
border: .025rem solid var(--border-color);
vertical-align: top;
text-align: center;
}
.calendar > .table-body > tr > td.today > .deco {
display: inline-block;
width: 2rem;
height: 2rem;
padding: .2rem;
background-color: #2047f3;
font-weight: bold;
color: var(--white);
border-radius: 50%;
}
.calendar > .table-body > tr > td.prev-month-day, .calendar > .table-body > tr > td.next-month-day {
color: #ccc;
}
No notes defined.