๋ฌ๋ ฅ์ ๋ง๋ค์์ผ๋..๋์์ธ์ด ์ฒ์ฐธํ๋ ์ง๋ ๊ณผ๊ฑฐ..
boxSizing="border-box"๋ก ํด๊ฒฐ!
![]() | ![]() |
|---|
#wrapper{
width: 700px;
height: 750px;
margin: auto;
}
#info{
width: 100%;
height: 60px;
background-color: rgb(52, 66, 30);
}
#header{
width: 100%;
height: 50px;
background-color: green;
text-align: center;
line-height: 50px;
}
#header h2{
display: inline-block;
margin: 0px 5px;
}
#header a{text-decoration: none;}
#days{
width: 100%;
height: 40px;
background-color: rgb(200, 243, 137);
float: left;
}
#content{
width: 100%;
height: 600px;
background-color: rgb(246, 255, 231);
float: left;
position: relative;
}
#dialog{
width: 220px;
height: 240px;
border-radius: 10px;
background-color: pink;
position: absolute;
/* content div์ ๋ค๋ฅธ ์
๋ค๊ณผ ๊ณต์กดํด์ผ ํ๋ฏ๋ก */
z-index: 50;
/* z-index๋ ์ด๋ค ์์๊ฐ ํ๋ฉด์์
๋ค๋ฅธ ์์๋ค๋ณด๋ค "์์ ๋ณด์ด๊ฒ" ํ ์ง๋ฅผ ๊ฒฐ์ ํ๋ ์ซ์
z์ถ ๊ฐ์ด ํด์๋ก ์๋ก ์ฌ๋ผ์ด */
display: none;
text-align: center;
}
#dialog input, textarea {
border: 1px solid #ccc;
margin: 1px;
}
#dialog input{
width: 96%;
height: 27px;
}
#dialog textarea{
width: 96%;
height: 163px;
}
<body>
<div id="wrapper">
<div id="info"></div>
<div id="header">
<a href="javascript:prev()">์ด์ </a>
<h2></h2>
<a href="javascript:next()">๋ค์</a>
</div>
<div id="days"></div>
<div id="content">
<div id="dialog">
<div>
<input type="text" placeholder="์ ๋ชฉ ์
๋ ฅ" id="t_title">
</div>
<div>
<textarea placeholder="๋ด์ฉ ์
๋ ฅ" id="t_detail"></textarea>
</div>
<div>
<button id="bt_regist">๋ฑ๋ก</button>
<button id="bt_close">๋ซ๊ธฐ</button>
</div>
</div>
</div>
</div>
</body>
[ ํ์ด์ง ๋ก๋ ]
โ
[ getCurrentTime() โ ํ์ฌ ์ฐ/์ ์ ์ฅ ]
โ
[ createCell() โ 42๊ฐ ์ ๋ง๋ค๊ธฐ ]
โ
[ printNum() โ ๋ ์ง ์ถ๋ ฅ ]
โ
[ ์ด์ / ๋ค์ ํด๋ฆญ ์ ]
โ getCurrentTime()
โ printTitle()
โ clearCell()
โ printNum()
โ
[ ์ ํด๋ฆญ ] โ openDialog(obj)
โ
[ '๋ซ๊ธฐ' ๋๋ esc ] โ closeDialog()
โ
[ ์ ๋ชฉ/๋ด์ฉ ์ ๋ ฅ ํ '๋ฑ๋ก' ] โ regist()
โ
[ ์ ์ ์ ๋ชฉ/์์ด์ฝ ํ์ + diaryArray์ ์ ์ฅ ]
let cellArray=Array.from({length:6},()=> Array(7).fill(0)); // 6ํ 7์ด ์
๋ฐฐ์ด
let currentYear; // ํ์ฌ ์ฐ๋
let currentMonth; // ํ์ฌ ์
let currentCell; // ์ ํ๋ ์
๊ฐ์ฒด (๋ชจ๋ ๊ฒ ๋ค ๋ค์ด ์์)
let diaryArray=[]; // ๋ฑ๋ก๋ ๋ค์ด์ด๋ฆฌ ๊ฐ์ฒด๋ค ์ ์ฅ (DB ๋์ ๋ฐฐ์ด)
function printTitle(){
document.querySelector("#header h2").innerText = `${currentYear}๋
${currentMonth +1}์`;
}
function getStartday(year, month){
let d = new Date(year, month, 1);
return d.getDay(); // 1์ผ์ ์์ผ
}
function getLastdate(year, month){
let d = new Date(year, month + 1, 0);
return d.getDate(); // ๋ง์ง๋ง ๋ ์ง
}
function printNum(){
let index = 0; //์
์ ์๋ฒ
let date = 1; //์ถ๋ ฅ๋ ์ค์ ๋ ์ง
for (let i = 0; i < 6; i++){
for (let j = 0; j < 7; j++) {
//โญ์ด๋ฒ ๋ฌ 1์ผ ์์ผ(index)๋ถํฐ ๋ ์ง๋ฅผ ํ๋์ฉ ์ฐ๊ณ , ๋ง์ง๋ง ๋ ์ง๊น์ง๋ง ์ฐ์
if (index >= getStartday(currentYear, currentMonth) &&
date <= getLastdate(currentYear, currentMonth)){
cellArray[i][j].setDate(currentYear, currentMonth, date);
date++;
}
index++;
}
}
}
/*---------------------------------------------
๋ก๋ ์์ ๋ฟ๋ง ์๋๋ผ, ๋ค์๋ฒํผ, ์ด์ ๋ฒํผ์ ์ํด์๋
currentYear, currentMonth ๊ตฌํด์ผ ํ๋ฏ๋ก
์ฝ๋๊ฐ ์ค๋ณต๋๊ธฐ ๋๋ฌธ์ ํจ์๋ก ๋นผ๋์!
------------------------------------------------*/
function getCurrentTime(year, month){
/*์ ๋ฌ๋ฐ์ month๊ฐ ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ฉด (์: -1, 12)
Date๊ฐ ์๋์ผ๋ก ์ฐ๋ ๊ณ์ฐํด์ค*/
let d = new Date(year, month);
currentYear = d.getFullYear();
currentMonth = d.getMonth();
}
function createCell(){
for (let i = 0; i < 6; i++){
for (let j = 0; j < 7; j++){
cellArray[i][j] = new Cell(
document.getElementById("content"),
100 * j, 100 * i, 100, 100,
"rgb(246, 255, 231)", "lightgreen", 0);
}
}
}
function clearCell(){
for (let i = 0; i < 6; i++){
for (let j = 0; j < 7; j++){
cellArray[i][j].numDiv.innerText = "";
cellArray[i][j].titleDiv.innerText = "";
// ์์ด์ฝ ์ ๊ฑฐ๋ ํ์ ์ ๊ตฌํ
}
}
}
function prev(){
getCurrentTime(currentYear, currentMonth - 1);
printTitle();
clearCell();
printNum();
}
function next(){
getCurrentTime(currentYear, currentMonth + 1);
printTitle();
clearCell();
printNum();
}
/*---------------------------------------------
์จ๊ฒจ์ ธ ์๋ ๋ํ์ฐฝ์ ๋์ฐ๋,
๊ทธ ์์น๋ ์ง๊ธ ํด๋ฆญํ ๋ฐ๋ก ๊ทธ ์
์ x,y๋ฅผ ๋ฐ๋ผ๊ฐ์ผํจ
------------------------------------------------*/
function openDialog(obj){ // ํด๋ฆญ๋ Cell ์ธ์คํด์ค๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ๋ฐ์
//ํ์ฌ ์
์ ๋ํ ์ ์ญ๋ณ์์ธ currentCell์
//obj๋ฅผ ๋ณด๊ดํด๋์
currentCell = obj;
let dialog = document.getElementById("dialog");
dialog.style.display = "block"; // โ none
dialog.style.position = "absolute"; //ํด๋ฆญํ ์
์์น์ ๋ง์ถฐ ์ ํํ ๋์ฐ๊ธฐ ์ํด
dialog.style.left = obj.x + "px";
dialog.style.top = obj.y + "px";
}
function closeDialog(){
document.getElementById("dialog").style.display = "none";
document.getElementById("t_title").value = "";
document.getElementById("t_detail").value = "";
}
function regist(){
let diary ={ //๋ค์ด์ด๋ฆฌ ํ ๊ฑด์ ๋ฐ์ดํฐ๋ฅผ โญ๊ฐ์ฒด ๋ฆฌํฐ๋ดโญ๋ก ๊ตฌ์ฑ
year: currentYear,
month: currentMonth,
date: currentCell.date,
title: document.getElementById("t_title").value, //์ ๋ชฉ ์
๋ ฅ๊ฐ
icon: "./images/diarystar.png",
detail: document.getElementById("t_detail").value //๋ด์ฉ ์
๋ ฅ๊ฐ
};
currentCell.titleDiv.innerText = diary.title;
currentCell.renderIcon(diary.icon, 25);
diaryArray.push(diary);
closeDialog();
}
addEventListener("load", ()=>{
let d = new Date();
getCurrentTime(d.getFullYear(), d.getMonth());
printTitle();
createCell();
printNum();
//๋ฑ๋ก ์ด๋ฒคํธ ๊ตฌํ
document.getElementById("bt_regist").addEventListener("click", ()=>{
regist();
});
//์ ์ฐฝ ๋ซ๊ธฐ ์ด๋ฒคํธ ๊ตฌํ
document.getElementById("bt_close").addEventListener("click", ()=>{
closeDialog();
});
document.body.addEventListener("keyup", function (e){
if(e.keyCode == 27) closeDialog(); //esc๋๋ ์ ๋ ์ฐฝ ๋ซ๊ธฐ
});
});
0์ผ๋ก ์ฑ์ด 6ํ 7์ด์ 2์ฐจ์ ๋ฐฐ์ด์ ๋ง๋ค์ด๋ณด์
"๋ฐ๋ณต๋ฌธ์ ๋ด๊ฐ ๋๋ ค?"
์ด ๋ ์ฌ์ฉํ๋๊ฒ ์ ์ธํ ํ๋ก๊ทธ๋๋ฐ
โ๋ฌด์์ ํ ๊ฒ์ธ์งโ๋ฅผ ์ ์ธ์ ์ผ๋ก ์ ์ ๋ฐฉ์
let cellArray = Array.from({ length: 6 }, ()=> Array(7).fill(0));
Array.from({ length: 6 }, ...)
๊ธธ์ด 6์ ๋ฐฐ์ด์ ๋ง๋ค๊ณ โ ํ(row) 6๊ฐ
()=> Array(7).fill(0)
๊ฐ ํ๋ง๋ค ๊ธธ์ด 7์ ๋ฐฐ์ด์ ๋ง๋ค์ด์ 0์ผ๋ก ์ฑ์ โ ์ด(column) 7๊ฐ
โ์ด๋ป๊ฒ ํ ๊ฒ์ธ์งโ๋ฅผ ์ ์ฐจ์ ์ผ๋ก ๋ช ๋ นํ๋ ๋ฐฉ์
let cellArray = [];
for (let i = 0; i < 6; i++) {
let row = [];
for (let j = 0; j < 7; j++) {
row.push(0);
}
cellArray.push(row);
}
๋ชฉ์ : ์บ๋ฆฐ๋ ์ 1์นธ์ ๊ตฌ์ฑํ๋ ๊ฐ์ฒด
class Cell{
constructor(container,x,y,width,height,bg,borderColor,date){
//[๋ค์ด์ด๋ฆฌ ๊ด๋ จ ๋ด์ฉ]
this.year;
this.month;
this.date=date;
this.icon;
//[UI ๊ด๋ จ ๋ด์ฉ]
this.container=container;
this.div=document.createElement("div"); //์
์์ฒด๋ฐ์ค
this.numDiv=document.createElement("div"); //๋ฌ๋ ฅ ๋ ์ง ์ถ๋ ฅํ ์์ญ
this.titleDiv=document.createElement("div"); //์ผ์ ์ ๋ชฉ
this.iconDiv=document.createElement("div"); //์์ด์ฝ์ด ์์นํ ์์ญ
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.bg=bg;
this.borderColor=borderColor;
//์
div style
this.div.style.position="absolute";
this.div.style.left=this.x+"px";
this.div.style.top=this.y+"px";
this.div.style.width=this.width+"px";
this.div.style.height=this.height+"px";
this.div.style.background=this.bg;
this.div.style.border="1px solid "+this.borderColor;
this.div.style.borderRadius=5+"px";
this.div.style.boxSizing="border-box"; //border, margin, padding์ ์ํ ๋ฐ์ค์ ํฌ๊ธฐ๊ฐ
// outside์ชฝ์ผ๋ก ์ปค์ง์ง ์๊ณ inside์ชฝ์ผ๋ก ์ ์ฉ๋์ด
// ๋๋น์ ์ํฅ์ ์ฃผ์ง ์์
//๋ ์ง ์ถ๋ ฅ div style
this.numDiv.style.width=100+"%";
this.numDiv.style.height=25+"px";
this.numDiv.style.textAlign="right";
this.numDiv.style.padding="0px 5px 0px 0px"; // (top-right-bottom-left) ์ค๋ฅธ์ชฝ์ 5pxํจ๋ฉ
this.numDiv.style.boxSizing="border-box";
//๋ค์ด์ด๋ฆฌ ์ ๋ชฉ div style
this.titleDiv.style.width=100+"%";
this.titleDiv.style.height=25+"px";
//์์ด์ฝ ์์ญ div style
this.iconDiv.style.width=100+"%";
this.iconDiv.style.height=50+"px";
//์
์ 3์ธต div ๊ฐ๊ฐ ๋ถ์ฐฉ
this.div.appendChild(this.numDiv);
this.div.appendChild(this.titleDiv);
this.div.appendChild(this.iconDiv);
this.container.appendChild(this.div);
//์
ํด๋ฆญ์ ํ์
๋์ฐ๊ธฐ
this.div.addEventListener("click", ()=>{ //ํ์ดํ ํจ์๋ this๋ฅผ ๊ฐ์ง ์ ์์
openDialog(this); //์ด this๋ ์์์ค์ฝํ์ this(=ํ์ฌ ์
์ธ์คํด์ค)
});
}
//์
์ ๋ณด์ฌ์ง ๋ ์ง๋ฅผ ์์๋ก ๋ณ๊ฒฝํด์ผ ํ๋ฏ๋ก, ๋ฉ์๋์ ๋์์ด ๋ ์ ์์
setDate(year, month, date){
this.year=year;
this.month=month;
this.date=date;
//๋ ๋๋ง ์ฒ๋ฆฌ
this.numDiv.innerText=date;
}
//์
์ด ์์ ์ด ๋ณด์ ํ ์์ด์ฝ์ ๊ทธ๋ฆฌ๊ธฐ
renderIcon(src, width){ //์ด๋ค ์ด๋ฏธ์ง๋ฅผ ์ํ๋์ง๋ ํธ์ถ์๊ฐ ๊ฒฐ์
this.icon=document.createElement("img");
this.icon.src=src;
this.icon.style.width=width+"px"; //์ด๋ฏธ์ง์ ํฌ๊ธฐ ์กฐ์
this.iconDiv.appendChild(this.icon);
}
}