ํฐ์คํ ๋ฆฌ ๋ทฐ
[๋ ธ๋ง๋ ์ฝ๋] ๋ฐ๋๋ผ JS๋ก ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ
stable_sound 2022. 5. 9. 13:36๋ช~~~~~๋ฌ ์ ์ ๋ง๋ค์๋ ๋ ธ๋ง๋ ์ฝ๋์ ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ๋ฅผ ์ด์ ์ผ ์ฌ๋ฆฐ๋ค^^..ใ ใ
์ผ๋ฅธ ๊ฐ์ธ ํ๋ก์ ํธ๋ ํด๋ณด๊ณ ์ถ์๊ณ ์ ์ฒ๊ธฐ ๊ณต๋ถ๋ ํด์ผํ์ด์ ์ ๋ฆฌํ ์๊ฐ์ด ์์๋ค.
ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ์์๋ ๋ค์๊ณผ ๊ฐ์ ๊ธฐ๋ฅ์ ๊ตฌํ ํ ์ ์๋ค.
โพ ์ค์๊ฐ ์๊ฐ ํ์ธํ๊ธฐ
โพ ์ฌ์ฉ์์ ์ด๋ฆ์ ์ ๋ ฅํ๋ฉด Local Storage์ ์ ์ฅ๋๊ณ ๊ธฐ์ต
โพ ํ ์ผ์ ์ถ๊ฐํ๊ธฐ / ์ญ์ ํ๊ธฐ
โพ ์์น ์ ๋ณด๋ฅผ ๋ฐ์์์ ์ง์ญ๊ณผ ์จ๋ ํ์ธํ๊ธฐ
โพ ์๋ก ๊ณ ์นจ ์, ๋ฐฐ๊ฒฝํ๋ฉด๊ณผ ๋ช ์ธ ๋๋ค ๋ณ๊ฒฝํ๊ธฐ
๋๋ ํฐ๋ฆฌ ์ค์
- css
- img
- js
- background.js
- clock.js
- greetings.js
- quotes.js
- todo.js
- weather.js
- index.html
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css">
<title>Hello! From JS!</title>
</head>
<body>
<h3 id="clock">00:00:00</h3>
<h2 id="calendar">00/00/0</h2>
<form id="login-form" class="hidden">
<input required maxlength="15" type="text" placeholder="What is your name?">
<!--<input type="submit" value="Log In" /> submit์ ์ํฐ/๋ฒํผ์ ๋๋ฅด๋ฉด ๋ฐ์ํ๊ณ ๋ธ๋ผ์ฐ์ ๊ฐ ์ฌ์์๋๋ค. -->
</form>
<h1 id="greeting" class="hidden"></h1>
<form id="todo-form">
<input type="text" placeholder="Write a To Do here" required />
</form>
<ul id="todo-list"></ul>
<div id="quote">
<span></span>
<span></span>
</div>
<div id="weather">
<span></span>
<span></span>
</div>
<script src="js/greetings.js"></script>
<script src="js/clock.js"></script>
<script src="js/quotes.js"></script>
<script src="js/background.js"></script>
<script src="js/todo.js"></script>
<script src="js/weather.js"></script>
</body>
</html>
style.css
body {
text-align: center;
color: white;
background-position:center;
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}
h3 {
font-size: 80px;
text-align: center;
color: white;
margin-bottom: auto;
}
h2 {
margin-top: auto;
color: white;
}
h1 {
color: white;
}
.hidden {
display: none;
}
#login-form input {
font-size: 20px;
margin-bottom: 20px;
}
#todo-form input {
font-size: 17px;;
}
input {
border: none;
border-bottom: 2px solid white;
background: transparent;
color: white;
text-align: center;
}
input::placeholder {
color: white;
}
li {
list-style: none;
}
button {
background: transparent;
border: none;
}
#quote {
position: absolute;
bottom: 0;
left: 0;
font-size: 13px;
font-style: italic;
}
#weather {
position: absolute;
top: 15px;
width: 130px;
}
img {
position: absolute;
width: 100%;
height: 100%;
left: 0px;
top: 0px;
right: 0px;
bottom: 0px;
z-index: -1;
opacity: 80%;
filter: brightness(70%);
}
background.js
const images = [
"0.jpg",
"1.jpg",
"2.jpg",
"3.jpg",
"4.jpg",
"5.jpg",
"6.jpg",
"7.jpg",
"8.jpg",
"9.jpg",
"10.jpg",
];
const chosenImage = images[Math.floor(Math.random() * images.length)];
const bgImage = document.createElement("img");
bgImage.src = `img/${chosenImage}`;
document.body.appendChild(bgImage); // body์ ๋งจ ๋ค์ ๋ฃ๋๋ค.
/*
appendChild : ๋งจ ๋ค์ ์ถ๊ฐ
prependChild : ๋งจ ์์ ์ถ๊ฐ
*/
์ด๋ฏธ์ง๊ฐ ์๊ณ ๋ก์นจ์ด ๋ ๋๋ง๋ค ๋๋ค์ผ๋ก ์ถ๋ ฅ๋๋๋ก ํ๋ค.
clock.js
const calendar = document.querySelector("h2#calendar");
const clock = document.querySelector("h3#clock");
function getClock() {
const dateDefine = new Date();
const year = dateDefine.getFullYear();
const month = String(dateDefine.getMonth() + 1).padStart(2, "0");
const date = String(dateDefine.getDate()).padStart(2, "0");
const hours = String(dateDefine.getHours()).padStart(2, "0");
const minutes = String(dateDefine.getMinutes()).padStart(2, "0");
//const seconds = String(dateDefine.getSeconds()).padStart(2, "0");
calendar.innerText = `${year}.${month}.${date}`;
clock.innerText = `${hours}:${minutes}`;
}
//setInterval(sayHello, 5000); 5์ด ํ ๋ง๋ค ํจ์ ์คํ
//setTimeout(getClock, 1000); // 5์ด ํ์ ํจ์ ์คํ
getClock(); // 1์ด ๊ธฐ๋ค๋ ธ๋ค๊ฐ ์ถ๋ ฅ๋๋๊ฒ ์๋๋ผ ๋ฐ๋ก ์ถ๋ ฅํ๊ณ setInterval์ด ๋์๊ฐ๋๋ก ํ๋ค.
setInterval(getClock, 1000); // ๋งค ์ด๋ง๋ค ์๋ก์ด Date object๋ฅผ ๋ง๋ค๊ณ ์์
// "1".padStart(2, "0") -> string์ ์์๋ถ๋ถ์ padding์ ์ถ๊ฐํ๋ค. ๊ธธ์ด 2๊ฐ ๋์ง ์๋๋ค๋ฉด "0"์ ์ถ๊ฐํ์ฌ ๊ธธ์ด๋ฅผ 2๋ก ๋ง๋ค์ด ์ค๋ค.
greeting.js
const loginForm = document.querySelector("#login-form");
const loginInput = document.querySelector("#login-form input");
const greeting = document.querySelector("#greeting");
const HIDDEN_CLASSNAME = "hidden"; // ๋ณ๋ก ์ค์ํ์ง ์์ ๊ฑด ๋ณ์ ๋ช
์ ๋๋ฌธ์๋ก ์ ๋๋ค.
const USERNAME_KEY = "username"; // ๋ฐ๋ณต๋๋ string์ ๋ณ์์ ์ ์ฅํด์ ์ฌ์ฉํ๋๊ฒ ๊ฐ๋ฐ์์ ์คํ ์ค๋ฅ๋ก ์ธํ ์๋ฌ๋ฅผ ๋ฐฉ์งํ ์ ์๋ค.
function onLoginSubmit(e) {
e.preventDefault(); //event์ ๊ธฐ๋ณธ ํ๋์ด ๋ฐ์๋์ง ์๋๋ก ํจ. ์ฌ๊ธฐ์๋ form์ submitํ๋ฉด ๋ธ๋ผ์ฐ์ ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์๋ก๊ณ ์นจ ํ๋ ๊ธฐ๋ณธ ๋์์ ํ๋๋ฐ ๊ทธ๊ฒ์ ๋ง์์ค๋ค.
loginForm.classList.add(HIDDEN_CLASSNAME); // form์ ์จ๊ฒจ์ค๋ค.
const username = loginInput.value; //input์ ์
๋ ฅํ ๊ฐ์ username์ ์ ์ฅํ๋ค.
localStorage.setItem(USERNAME_KEY, username); //์
๋ ฅํ ์ ์ ๋ช
์ ์ ์ฅํด์ ๊ธฐ์ตํด์ค.
paintGreetings(username);
}
function paintGreetings(username) {
greeting.innerText = `Hello ${username}!`; // "Hello + username" ๋ณด๋ค ํจ์ฌ ์ข์!
greeting.classList.remove(HIDDEN_CLASSNAME); //hidden ํด๋์ค๋ฅผ ์ญ์ ํ๋ค. (h1 ํ๊ทธ๊ฐ ๋ณด์ฌ์ผ ํ๋๊น form์ ์๋ณด์ด๊ฒ ํจ.)
}
const savedUsername = localStorage.getItem(USERNAME_KEY);
if (savedUsername === null) {
//show the form (๋ง์ฝ ์ ์ฅ๋ username์ด ์์ผ๋ฉด hidden ํด๋์ค ์ญ์ ํ๊ณ ํผ์ ๋์ด๋ค.)
loginForm.classList.remove(HIDDEN_CLASSNAME);
loginForm.addEventListener("submit", onLoginSubmit); //submit ์ด๋ฒคํธ ๋ฐ์ ์ onLoginSubmit ํจ์ ์คํ
} else {
// show the greetings (๋ง์ฝ ์ ์ฅ๋ username์ด ์์ผ๋ฉด h1ํ๊ทธ์ username ๋ฃ์ด์ ๋์ด๋ค.)
paintGreetings(savedUsername);
}
/*
์ ๋ฆฌ
: ์ฒ์์๋ form๊ณผ h1์ hidden ํด๋์ค๋๋ฌธ์ ์จ๊ฒจ์ ธ ์์.
js๊ฐ username_key๋ฅผ ๊ฐ์ง๊ณ localStorage ํ์ธํจ.
์ฒ์์ ์์ผ๋๊น form์ ๋ณด์ฌ์ค. (hidden ํด๋์ค ์ญ์ )
์๋ธ๋ฐ ๊ธฐ๋ค๋ ท๋ค๊ฐ ์ด๋ฒคํธ ๋ฐ์ ์ onLoginSubmit ํจ์ ์คํ.
๊ธฐ๋ณธ ๋์ ๋ชปํ๊ฒ ๋ง๊ณ from์ ๋ค์ ์จ๊ธด ํ, input์ ์ ์ ๊ฐ์ username์ ์ ์ฅ.
์
๋ ฅํ ์ ์ ๋ช
์ ์ ์ฅํ๊ณ paintGretings ํจ์ ์คํ.
h1ํ๊ทธ์ text ์ ์ด์ฃผ๊ณ ๋์์ผํ๊ธฐ ๋๋ฌธ์ hidden ํด๋์ค ์ญ์ ํจ.
๊ทธ๋ผ localStorage์์ username์ด key์ธ ๊ฐ์ ๊ฐ์ ธ์์ savedUsername์ ๋ฃ๊ณ ๋ค์ if๋ฌธ ๋๋ฆผ.
๋ก์ปฌ์คํ ๋ฆฌ์ง์์ ๊ฐ ๋ฐ์์์ null์ด ์๋๋๊น paintGreetings ํจ์ ์คํ (h1 ํ๊ทธ ๋ณด์ฌ์ค)
*/
quotes.js
const quotes = [
{
quote: "I never dreamed about success, I worked for it",
author: "Estee Lauder",
},
{
quote: "Do not try to be original, just try to be good.",
author: "Paul Rand",
},
{
quote: "Do not be afraid to give up the good to go for the great",
author: "John D. Rockefeller",
},
{
quote:
"If you cannot fly then run. If you cannot run, then walk. And if you cannot walk, then crawl, but whatever you do, you have to keep moving forward.",
author: "Martin Luther King Jr.",
},
{
quote:
"Our greatest weakness lies in giving up. The most certain way to succeed is always to try just one more time.",
author: "Thomas Edison",
},
{
quote:
"The fastest way to change yourself is to hang out with people who are already the way you want to be",
author: "REid Hoffman",
},
{
quote:
"Money is like gasoline during a road trip. You do not want to run out of gas on your trip, but you are not doing a tour of gas stations",
author: "Tim O Reilly",
},
{
quote:
"Some people dream of success, while other people get up every morning and make it happen",
author: "Wayne Huizenga",
},
{
quote:
"The only thing worse than starting something and falling.. is not starting something",
author: "SEth Godin",
},
{
quote:
"If you really want to do something, you will find a way. If you do not, you will find an excuse.",
author: "Jim Rohn",
},
];
const quote = document.querySelector("#quote span:first-child");
const author = document.querySelector("#quote span:last-child");
/* ์ ๋ฆฌ
* Math.random() : 0~1 ์ฌ์ด์ ์ซ์๋ฅผ ๋๋ค์ผ๋ก ์ถ๋ ฅ
* Math.random() * 10 ํ๋ฉด 0~9 ์ฌ์ด ์ซ์ ๋๋ค ์ถ๋ ฅ ๊ฐ๋ฅํ์ง๋ง ์์์ ์ด ๋ถ์.
* Math.round(์ซ์) : ์ค์๋ฅผ ์ ์๋ก ๋ฐ์ฌ๋ฆผํด์ค.
* Math.ceil(์ซ์) : ์ค์๋ฅผ ๊ฐ์ฅ ์์ ์ซ์๋ก ์ฌ๋ ค์ค. (ex. 1.1์ 2๋ก ๋ฐ์ฌ๋ฆผํ๋ค. 1.0๋ง 1์ด ๋ ์ ์์)
* Math.floor(์ซ์) : ์ค์๋ฅผ ๊ฐ์ฅ ๋ฐ๋ฅ์ ์ซ์๋ก ๋ด๋ ค์ค. (ex. 1.999๋ฅผ 1๋ก ๋ด๋ ค์ค.)
*/
// console.log(quotes[Math.floor(Math.random() * 10)]); quotes ๋ฐฐ์ด์ 0~9 ์ฌ์ด์ ๊ฐ์ ๋๋ค์ผ๋ก ์ถ๋ ฅ. ๊ทผ๋ฐ ์ด๊ฑด ๋ฑ 10๊ฐ๋ฅผ ๊ฐ์ง๊ณ ์์ ๋์๋ง ๊ฐ๋ฅ. ๋ฐฐ์ด์ ๊ธธ์ด๋งํผ ๋๋ค๋๊ฒ ํ ํ์๊ฐ ์์.
const todaysQuote = quotes[Math.floor(Math.random() * quotes.length)];
quote.innerText = todaysQuote.quote;
author.innerText = `<${todaysQuote.author}>`;
quotes ์ญ์ ์ฌ๋ฌ ๊ฐ๋ฅผ ๋ฃ์ด๋๊ณ ๋๋ค์ผ๋ก ์ถ๋ ฅ๋๋๋ก ํ๋ค.
todo.js
const toDoForm = document.getElementById("todo-form");
const toDoInput = toDoForm.querySelector("input");
const toDoList = document.getElementById("todo-list");
const TODOS_KEY = "todos";
let toDos = [];
function saveTodos() {
localStorage.setItem(TODOS_KEY, JSON.stringify(toDos)); // JSON.stringify : ๋ฐฐ์ด ๋๋ ๊ฐ์ฒด ๋ฑ์ string์ผ๋ก ๋ง๋ค์ด์ค๋ค.
}
function deleteToDo(event) {
const li = event.target.parentElement;
li.remove();
toDos = toDos.filter((toDo) => toDo.id !== parseInt(li.id)); // ์ญ์ ํ๋ ค๊ณ ๋๋ฅธ liํ๊ทธ์ id์ ๊ฐ์ง ์์ผ๋ฉด ์๋ก ๋ฐฐ์ด์ ๋ง๋ค์ด์ ๋ณด์ฌ์ค๋ค. ์ด๋ li.id๋ string์ด๊ธฐ ๋๋ฌธ์ ํ๋ณํ์ ํด์ค์ผ ํ๋ค.
saveTodos(); // ์ญ์ ํ์ ํ๋ฒ ๋ ๋ฐฐ์ด์ ์ ์ฅํด์ค๋ค.
}
function paintTodo(newTodo) {
const todo = document.createElement("li");
todo.id = newTodo.id;
const span = document.createElement("span");
span.innerText = newTodo.text;
const button = document.createElement("button");
button.innerText = "โ";
button.addEventListener("click", deleteToDo);
todo.appendChild(span); // li์ ์์ span
todo.appendChild(button);
toDoList.appendChild(todo);
}
function handleToDoSubmit(event) {
event.preventDefault();
const newTodo = toDoInput.value; // input ๊ฐ ์ ์ฅ
toDoInput.value = ""; // input ์ฐฝ ๋น์ฐ๊ธฐ
const newTodoObj = {
text: newTodo,
id: Date.now(),
}; // ๊ฐ๊ฐ์ todo์ id๋ฅผ ๋ถ์ฌํด์ ํน์ todo๋ฅผ ์ญ์ ํ ์ ์๋๋ก ํ๋ค.
toDos.push(newTodoObj); // ๋ฐฐ์ด์ input ๋ด์ฉ๋ค์ ์ ์ฅ
paintTodo(newTodoObj);
saveTodos(); // ๋ฐฐ์ด์ ๋ด์ฉ์ ๋ฃ์ ํ localStorage์ ์ ์ฅํ๋ค.
}
toDoForm.addEventListener("submit", handleToDoSubmit);
const savedToDos = localStorage.getItem(TODOS_KEY);
if (savedToDos !== null) {
const parsedToDos = JSON.parse(savedToDos);
toDos = parsedToDos;
parsedToDos.forEach(paintTodo); // forEach : ๋ฐฐ์ด์ ๊ฐ ์์ดํ
๋ค์ ๋ํด์ function ์ ์คํํ๊ฒ ํจ.
}
weather.js
const API_KEY = "๊ฐ์ ์์ ์ ํค ์ฌ์ฉ!";
function onGeoOk(position) {
const lat = position.coords.latitude; // ์๋ ๊ฒฝ๋๋ฅผ ํ์ํด์ค
const lon = position.coords.longitude;
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`;
fetch(url)
.then((response) => response.json())
.then((data) => {
const weather = document.querySelector("#weather span:first-child");
const city = document.querySelector("#weather span:last-child");
city.innerText = data.name;
weather.innerText = `${data.main.temp}°`;
});
}
function onGeoError() {
alert("Can't find you. No weather for you.");
}
navigator.geolocation.getCurrentPosition(onGeoOk, onGeoError);
โจ ๊ฒฐ๊ณผ๋ฌผ โจ
์ด๋ฆ๊ณผ ํฌ๋๋ฆฌ์คํธ๋ฅผ ์ ๋ ฅํ๋ฉด ์์ ๊ฐ์ ๋ชจ์ต์ด๋ค!
์ผ์ชฝ ์๋จ์๋ ์์ ์ ์์น ์ ๋ณด๋ฅผ ๋ฐ์์์ ์ง์ญ๊ณผ ๋ ์จ๋ฅผ ํ์ํ๊ณ ,
์ผ์ชฝ ํ๋จ์๋ ๋ฐฐ๊ฒฝํ๋ฉด๊ณผ ๊ฐ์ด ๋๋ค์ผ๋ก quote๋ฅผ ๋ณด์ฌ์ค๋ค.
์๋ก ๊ณ ์นจํ๋ฉด ๋ฐฐ๊ฒฝํ๋ฉด์ด ๋ฐ๋๋ค.
์์ ์์งค์ ํ๋ฉด์ด ๋ฐ๋๋ ๋ชจ์ต, ์ด๋ฆ๊ณผ ํฌ๋๋ฅผ ์ ๋ ฅํ๋ ๋ชจ์ต์ด๋ค.
'Front-end ๐ฅ๐ค > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java Script] String.prototype.split()์ด๋? (0) | 2022.05.13 |
---|---|
[Vanilla JS] const ์ let, var ์ ์ฐจ์ด์ (0) | 2022.05.12 |
[๋ ธ๋ง๋ ์ฝ๋] ๋ฐ๋๋ผ js๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ (0) | 2022.02.17 |
๐ ๋๋ฆผ์ฝ๋ฉ by ์๋ฆฌ JavaScript ์ ๋ฆฌ 5. ํด๋์ค (0) | 2022.02.04 |
๐ ๋๋ฆผ์ฝ๋ฉ by ์๋ฆฌ JavaScript ์ ๋ฆฌ 4. ํจ์์ ์ ์ธ๊ณผ ํํ (0) | 2022.02.04 |
- Total
- Today
- Yesterday
- ์กธ์ ์ํ์ค๋น
- set ๊ฐ์ฒด
- ์กธ์ ์ํ
- ๋ ธ๋ง๋์ฝ๋
- JavaScript
- css grid
- indexOf()
- ๊ฐค๋ฌ๋ฆฌ๋์ฐ๊ธฐ
- ํ๋ก๊ทธ๋๋จธ์ค
- ์ค๋งํธ์ปจํธ๋ํธ
- php
- ์ ๋ณด์ฒ๋ฆฌ๊ธฐ์ฌ
- php ๋ฌ๋ ฅ๋ง๋ค๊ธฐ ์์ฉ
- ํ์ฅ์ค์ต
- ํ์ฅ์ค์ต ๊ธฐ๋ก
- C์ธ์ด
- ์ ์ฒ๊ธฐ ์ค๊ธฐ
- ์ ๋ณด์ฒ๋ฆฌ๊ธฐ์ฌ ์ค๊ธฐ ์ ๋ฆฌ
- CSS
- HTML
- ๋ฐฑ์ค
- ํํ์ด์ง ๋ง๋ค๊ธฐ
- ์ ์ฒ๊ธฐ ์ค๊ธฐ ์ ๋ฆฌ
- ์ด๋๋ฆฌ์
- php๊ฒ์ํ๋ง๋ค๊ธฐ
- ๋ฆฌ์กํธ
- ๋ธ๋ก์ฒด์ธ
- MySQL
- DAPP
- ์ ๋ณด์ฒ๋ฆฌ๊ธฐ์ฌ ์ค๊ธฐ
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |