<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="pageTitle" value="MAIN"></c:set>
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
<link rel="stylesheet" href="/resource/background.css" />
<%@ include file="../common/head.jspf"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>간단한 달력</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #dddddd;
text-align: center;
padding: 24px;
font-size: 15px;
}
th {
background-color: #f2f2f2;
padding: 7px;
}
button {
margin: 10px;
padding: 5px 10px;
font-size: 16px;
cursor: pointer;
}
.sunday {
color: red;
}
.saturday {
color: blue;
}
#currentMonth {
text-align: center;
margin-top: 10px;
}
#calendar {
margin: 30 auto;
max-width: 800px;
}
.other-month {
color: rgba(0, 0, 0, 0.3);
}
.highlight {
background-color: blue;
color: white;
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
height: 100px;
background-color: rgba(0,0,0,0.5);
width: 350px;
padding: 30px 30px 20px 30px;
background-color: #fefefe;
border: 1px solid #888;
border-radius: 10px;
font-size: 1rem;
}
}
.modal-content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #fff;
padding: 20px;
}
.close {
position: absolute;
top: 10px;
right: 10px;
font-size: 20px;
cursor: pointer;
}
</style>
</head>
<body>
<h2 id="currentMonth">간단한 달력</h2>
<div id="myModal" class="modal">
<div id="modal-content">
<span class="modal_close_btn" onclick="closeModal()">×</span>
<p id="selectedDate"></p>
</div>
</div>
<table id="calendar">
<thead>
<tr>
<th class="sunday">일</th>
<th>월</th>
<th>화</th>
<th>수</th>
<th>목</th>
<th>금</th>
<th class="saturday">토</th>
</tr>
</thead>
<tbody id="calendarBody"></tbody>
</table>
<button onclick="prevMonth()">이전 달</button>
<button onclick="nextMonth()">다음 달</button>
<script>
let currentDate = new Date();
function displayCalendar(date) {
const currentDate = date || new Date();
const calendarBody = document.querySelector("#calendarBody");
const currentMonthElement = document.getElementById("currentMonth");
calendarBody.innerHTML = "";
const firstDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);
const lastDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0);
let currentDay = new Date(firstDayOfMonth);
currentDay.setDate(1 - firstDayOfMonth.getDay());
currentMonthElement.textContent = getMonthName(currentDate.getMonth()) + " " + currentDate.getFullYear();
while (currentDay <= lastDayOfMonth) {
const weekRow = document.createElement("tr");
for (let i = 0; i < 7; i++) {
const dayCell = document.createElement("td");
dayCell.textContent = currentDay.getDate();
dayCell.addEventListener("click", function () {
openModal('myModal');
});
if (currentDay.getMonth() !== currentDate.getMonth()) {
dayCell.classList.add("other-month");
}
if (isToday(currentDay)) {
dayCell.classList.add("highlight");
}
if (i === 0) {
dayCell.classList.add("sunday");
} else if (i === 6) {
dayCell.classList.add("saturday");
}
weekRow.appendChild(dayCell);
currentDay.setDate(currentDay.getDate() + 1);
}
calendarBody.appendChild(weekRow);
}
}
function prevMonth() {
currentDate.setMonth(currentDate.getMonth() - 1);
displayCalendar(currentDate);
}
function nextMonth() {
currentDate.setMonth(currentDate.getMonth() + 1);
displayCalendar(currentDate);
}
function getMonthName(month) {
const monthNames = [
"1월", "2월", "3월", "4월", "5월", "6월",
"7월", "8월", "9월", "10월", "11월", "12월"
];
return monthNames[month];
}
function isToday(date) {
const today = new Date();
return (
date.getDate() === today.getDate() &&
date.getMonth() === today.getMonth() &&
date.getFullYear() === today.getFullYear()
);
}
function openModal(id) {
var zIndex = 9999;
const modal = document.getElementById("myModal");
const selectedDateElement = document.getElementById("modal-content");
var bg = document.createElement('div');
bg.className = 'modal_bg';
bg.setStyle({
position: 'fixed',
zIndex: zIndex,
left: '0px',
top: '0px',
width: '100%',
height: '100%',
overflow: 'auto',
backgroundColor: 'rgba(0,0,0,0.3)'
});
document.body.append(bg);
modal.querySelector('.modal_close_btn').addEventListener('click', function() {
bg.remove();
modal.style.display = 'none';
});
modal.setStyle({
position: 'fixed',
display: 'block',
boxShadow: '0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)',
zIndex: zIndex + 1,
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
msTransform: 'translate(-50%, -50%)',
webkitTransform: 'translate(-50%, -50%)'
});
}
Element.prototype.setStyle = function(styles) {
for (var k in styles) this.style[k] = styles[k];
return this;
};
function closeModal() {
const modal = document.getElementById("myModal");
modal.style.display = "none";
}
function getFormattedDate(date) {
const options = { year: "numeric", month: "long", day: "numeric" };
return date.toLocaleDateString("en-US", options);
}
displayCalendar();
</script>
</body>
</html>
<%@ include file="../common/foot.jspf"%>
- 여기서 모달이 안나오고 뒤에 흐려지는 현상만 발생
- 아 저번에도 이거로 고생했는데 z-index로 수정했던거 같은데 뭔지 모르겠다...흠