Thanks to visit codestin.com
Credit goes to github.com

Skip to content

dandeKim/JS_app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

10 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

JS_app

JS_app ๋ฐ”๋กœ๊ฐ€๊ธฐ

index


Clock

  • getTime() : ํ˜„์žฌ ์‹œ๊ฐ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜
    • Date ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํ˜„์žฌ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ
    • clockHours, clockMinutes, clockSeconds์˜ ํ…์ŠคํŠธ๋ฅผ ํ˜„์žฌ ์‹œ๊ฐ์œผ๋กœ ๋ณ€๊ฒฝ
    • ์‚ผํ•ญ ์กฐ๊ฑด ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ํ•œ ์ž๋ฆฟ์ˆ˜์ผ ๊ฒฝ์šฐ ์•ž์— '0'์„ ๋ถ™์—ฌ์คŒ
function getTime() {
  const date = new Date();
  const hours = date.getHours();
  const minutes = date.getMinutes();
  const seconds = date.getSeconds();
  clockHours.innerText = `${hours < 10 ? `0${hours}` : hours}:`;
  clockMinutes.innerText = `${minutes < 10 ? `0${minutes}` : minutes}:`;
  clockSeconds.innerText = `${seconds < 10 ? `0${seconds}` : seconds}`;
}
  • setInterval(getTime, 1000); : 1์ดˆ๋งˆ๋‹ค getTime ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ์„ค์ •

Weather

  • getWeather() : ๋‚ ์”จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜
    • Fetch API๋ฅผ ์‚ฌ์šฉํ•ด openWeatherMap์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ด
    • weather icon์˜ ์ด๋ฆ„์„ ํด๋ž˜์Šค ๊ทœ์น™์œผ๋กœ ์žก์•„๋‘ฌ์„œ ํ˜„์žฌ ๋‚ ์”จ์— ๋งž๋Š” ์•„์ด์ฝ˜์„ ์ถœ๋ ฅ ex).weather-01d
    • ์•„์ด์ฝ˜ ์ด๋ฏธ์ง€๋Š” ์Šคํ”„๋ผ์ดํŠธ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉ
      .weather-icon {
        position: absolute;
        left: -70px;
        display: block;
        width: 60px;
        height: 60px;
        background: url(img/weather-icon.svg) no-repeat;
        background-size: cover;
      }
      .weather-icon.weather-01d {
        background-position: 0 0;
      }
function getWeather(lat, lon) {
  fetch(
    `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`
  )
    .then(function(response) {
      return response.json();
    })
    .then(function(json) {
      const temperature = Math.floor(json.main.temp);
      const weatherIcon = json.weather[0].icon;
      weatherIconClass.classList.add(`weather-${weatherIcon}`);
      const place = json.name;
      weather.innerText = `${temperature}ยฐ`;
      weatherLocation.innerText = place;
    });
}
  • ๊ทธ ์™ธ ํ•จ์ˆ˜๋“ค

    ํ•จ์ˆ˜๋ช… ๊ธฐ๋Šฅ
    askForCoords() ํ˜„์žฌ ์œ„์น˜์˜ ํฌ์ง€์…˜๊ฐ’์„ ์ „๋‹ฌ
    handleGeoSuccess() ํฌ์ง€์…˜๊ฐ’์„ ์„ฑ๊ณต์ ์œผ๋กœ ๊ฐ€์ ธ์™”์„ ๋•Œ ์œ„๋„, ๊ฒฝ๋„๊ฐ’์„ ๊ตฌํ•จ
    handleGeoError() ํฌ์ง€์…˜๊ฐ’์„ ๊ฐ€์ ธ์˜ค์ง€ ๋ชปํ–ˆ์„ ๋•Œ console์— ๋ฉ”์‹œ์ง€๋ฅผ ๋„์›€
    saveCoords() localStorage์— ์œ„๋„, ๊ฒฝ๋„๊ฐ’์„ ์ €์žฅ
    loadCoords() localStorage์— ์ €์žฅ๋œ ์œ„๋„, ๊ฒฝ๋„๊ฐ’์ด ์žˆ๋‹ค๋ฉด ์ด๋ฅผ ๊ฐ€์ ธ์™€ ๋‚ ์”จ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด askForCoords()๋ฅผ ํ˜ธ์ถœ

User

ํ•จ์ˆ˜๋ช… ๊ธฐ๋Šฅ
askForName() form์— .showing ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•ด display:block ์ƒํƒœ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ณ , ๊ฐ’์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ handleSubmit()ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ
handleSubmit() input์˜ ๊ฐ’(์‚ฌ์šฉ์ž ์ด๋ฆ„)์„ ๊ตฌํ•ด paintGreeting()๊ณผ saveName()ํ•จ์ˆ˜๋กœ ๋ณด๋‚ด์คŒ
paintGreeting() form์„ display:none์ƒํƒœ๋กœ ๋งŒ๋“ค๊ณ  ํ™”๋ฉด์— ์‚ฌ์šฉ์ž ์ด๋ฆ„์„ ์ถœ๋ ฅ
saveName() localStorage์— ์‚ฌ์šฉ์ž ์ด๋ฆ„์„ ์ €์žฅ
loadName() localStorage์— ์ €์žฅ๋œ ์‚ฌ์šฉ์ž ์ด๋ฆ„์ด ์žˆ๋‹ค๋ฉด ์ด๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด askForName()์„ ํ˜ธ์ถœ

To-do List

  • getUUID() : UUID๋ฅผ ์‚ฌ์šฉํ•ด To-do List์˜ ๊ฐ ํ•ญ๋ชฉ์— ๊ณ ์œ  id๋ฅผ ๋ถ€์—ฌ

    UUID : ๋ฒ”์šฉ ๊ณ ์œ  ์‹๋ณ„์ž(Universally Unique IDentifier)๋Š” ์†Œํ”„ํŠธ์›จ์–ด ๊ตฌ์ถ•์— ์“ฐ์ด๋Š” ์‹๋ณ„์ž ํ‘œ์ค€์ด๋‹ค. UUID ํ‘œ์ค€์— ๋”ฐ๋ผ ์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜๋ฉด ์‹ค์ œ ์‚ฌ์šฉ์ƒ์— ์ค‘๋ณต๋  ๊ฐ€๋Šฅ์„ฑ์ด ๊ฑฐ์˜ ์—†๋‹ค๊ณ  ์ธ์ •๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค. ํ‘œ์ค€ ํ˜•์‹์—์„œ UUID๋Š” 32๊ฐœ์˜ ์‹ญ์œก์ง„์ˆ˜๋กœ ํ‘œํ˜„๋˜๋ฉฐ ์ด 36๊ฐœ ๋ฌธ์ž(32๊ฐœ ๋ฌธ์ž์™€ 4๊ฐœ์˜ ํ•˜์ดํ”ˆ)๋กœ ๋œ 8-4-4-4-12๋ผ๋Š” 5๊ฐœ์˜ ๊ทธ๋ฃน์„ ํ•˜์ดํ”ˆ์œผ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.

function getUUID() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
    var r = (Math.random() * 16) | 0,
      v = c == "x" ? r : (r & 3) | 8;
    return v.toString(16);
  });
}
  • checkedToDos() : checkbox์˜ ์ฒดํฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธ ํ›„, ์ฒดํฌ๋œ ๋ฆฌ์ŠคํŠธ์— .checked ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•ด label์˜ ์ด๋ฏธ์ง€ ๋ณ€๊ฒฝ
    • checkbox์˜ ์ƒํƒœ๊ฐ€ ๋ณ€ํ•  ๋•Œ๋งˆ๋‹ค checked ๊ฐ’(true/false)์„ localStorage์— ์ €์žฅ
.toDoList li label {
  display: inline-block;
  width: 12px;
  height: 12px;
  margin-right: 10px;
  background: url(img/checkbox.svg);
  cursor: pointer;
}
.toDoList li label.checked {
  background: url(img/checked.svg);
}
  • ๊ทธ ์™ธ ํ•จ์ˆ˜๋“ค

    ํ•จ์ˆ˜๋ช… ๊ธฐ๋Šฅ
    handleSubmit() To-do List๋ฅผ ์ž…๋ ฅํ•˜๋ฉด paintToDo()ํ•จ์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์„ ์ „๋‹ฌ
    paintToDo() li๋ฅผ ์ƒ์„ฑํ•ด ์ž…๋ ฅ๋ฐ›์€ To-do List๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅ, 'text, id, checked'๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด localStorage์— ์ €์žฅ, checkedToDos()ํ˜ธ์ถœ
    deleteToDo() del ๋ฒ„ํŠผ์˜ ๋ถ€๋ชจ li๋ฅผ ์ฐพ์•„ ์‚ญ์ œ ํ›„, localStorage์— ์ €์žฅ๋œ id์™€ ํ•ด๋‹น li์˜ id๋ฅผ ๋น„๊ตํ•ด ๊ฐ™์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋งŒ saveToDos()๋กœ ์ „๋‹ฌ
    saveToDos() localStorage์— To-do List ๋ฐฐ์—ด ์ €์žฅ
    loadToDos() localStorage์— ์ €์žฅ๋œ To-do List๊ฐ€ ์žˆ๋‹ค๋ฉด ์ด๋ฅผ ์ถœ๋ ฅ

Background Image

ํ•จ์ˆ˜๋ช… ๊ธฐ๋Šฅ
getRandom() Math ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด 1~6 ์‚ฌ์ด์˜ ๋žœ๋ค ์ˆซ์ž ์ƒ์„ฑ
paintImage() ์ „๋‹ฌ๋ฐ›์€ ์ž„์˜์˜ ์ˆซ์ž๋ฅผ ์ด์šฉํ•ด ํด๋ž˜์Šค ๊ทœ์น™์„ ๋ถ€์—ฌํ•ด์„œ ๋žœ๋ค์œผ๋กœ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๋ฅผ ์ถœ๋ ฅ

About

๐Ÿ’ก Momentum with Vanilla JavaScript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published