Jieunny์˜ ๋ธ”๋กœ๊ทธ

Unit 11. [์‹ค์Šต] ๋‚˜๋งŒ์˜ ์•„๊ณ ๋ผ ์Šคํ…Œ์ด์ธ  ๋งŒ๋“ค๊ธฐ ๋ณธ๋ฌธ

CodeStates/Training

Unit 11. [์‹ค์Šต] ๋‚˜๋งŒ์˜ ์•„๊ณ ๋ผ ์Šคํ…Œ์ด์ธ  ๋งŒ๋“ค๊ธฐ

Jieunny 2023. 1. 10. 17:00

๐Ÿ“ฃ ๋‚˜๋งŒ์˜ ์•„๊ณ ๋ผ ์Šคํ…Œ์ด์ธ  ๋งŒ๋“ค๊ธฐ

1๏ธโƒฃ Bare Minimum Requirment

 

โœ”๏ธ ๋””์Šค์ปค์…˜ ๋‚˜์—ด ๊ธฐ๋Šฅ

// agoraStatesDiscussions ๋ฐฐ์—ด์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ํ™”๋ฉด์— ๋ Œ๋”๋งํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
const render = (element) => {
  for (let i = 0; i < agoraStatesDiscussions.length; i += 1) {
    element.append(convertToDiscussion(agoraStatesDiscussions[i]));
  }
  return;
};

// ul ์š”์†Œ์— agoraStatesDiscussions ๋ฐฐ์—ด์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ํ™”๋ฉด์— ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.
const ul = document.querySelector("ul.discussions__container");
render(ul);

โžฐ ์›๋ž˜๋Š” render() ์‚ฌ์šฉํ•ด์„œ ul์— ๋””์Šค์ปค์…˜ ํ•˜๋‚˜ํ•˜๋‚˜ li๋กœ ์ถ”๊ฐ€ํ•ด์คฌ๋‹ค.


โœ”๏ธ ๋””์Šค์ปค์…˜ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ
โžฐ ๋นˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ์ถ”๊ฐ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ , ๊ทธ ๊ฐ์ฒด๋ฅผ agoraStatesDiscussions ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•ด์คฌ๋‹ค.


โœ”๏ธ CSS


โœ”๏ธ Github ๋ฐฐํฌ

https://jieuny0314.github.io/fe-sprint-my-agora-states/

 


 

2๏ธโƒฃ Advanced Challenge

๐Ÿ“Œ Advanced ํ•˜๋ฉด์„œ ๋””์Šค์ปค์…˜ ๋‚˜์—ด, ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ์ˆ˜์ •

โœ”๏ธ ํ˜„์ง€ ์‹œ๊ฐ„ ์ ์šฉ

โžฐ new Date().toLocaleString() ์‚ฌ์šฉ

//convertToDiscussion(obj, itemLi) ํ•จ์ˆ˜
let localeDate = new Date(obj.createdAt).toLocaleString();

//๋ฒ„ํŠผ ๋ˆŒ๋ €์„ ๋•Œ ๋””์Šค์ปค์…˜ ์ถ”๊ฐ€ ๋ถ€๋ถ„
let currentTime = new Date();
// currenTime์ด convertToDiscussion() ์•ˆ์—์„œ toLocaleSting()์ด ์ ์šฉ๋˜๋ฏ€๋กœ ๋ฏธ๋ฆฌ ์ ์šฉ์‹œํ‚ค๋ฉด ์•ˆ๋œ๋‹ค.

 

โœ”๏ธ ํŽ˜์ด์ง€๋„ค์ด์…˜ ๊ธฐ๋Šฅ

โžฐ displayLIst() : ํ™”๋ฉด์— ๋””์Šค์ปค์…˜ ํ•˜๋‚˜ํ•˜๋‚˜ ๋ Œ๋”๋ง ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜

const listElement = document.querySelector('.discussions__container');  // ๋””์Šค์ปค์…˜ ๋‹ด์„ ul ๊ฐ€์ ธ์˜ค๊ธฐ
const pageList = document.querySelector('.page_list');  // ํŽ˜์ด์ง€ ๋ชฉ๋ก ๋„ฃ์„ ๊ณณ ๊ฐ€์ ธ์˜ค๊ธฐ

let discussionsCount = document.querySelector('.discussions__count');

let currentPage = 1;  //์ดˆ๊ธฐํ™”ํ•˜๋ฉด ์‹œ์ž‘ ํŽ˜์ด์ง€
let rows = 10; //ํ•œ ๋ชฉ๋ก์— ๋ณด์—ฌ์ค„ ์•„์ดํ…œ

let firstPage = 1;  // ์ฒ˜์Œ ํŽฒ์ด์ง€

function displayList(items, wrapper, rows_per_page, page){  // ํ™”๋ฉด์— ๋ Œ๋”๋ง ํ•จ์ˆ˜(๋ Œ๋”๋ง ํ•  ์š”์†Œ, ์–ด๋””์— append ํ• ๊ฑด์ง€, ํ•œ ํŽ˜์ด์ง€์— ํ‘œ์‹œํ•  ๋””์Šค์ปค์…˜ ๊ฐœ์ˆ˜, ํ˜„์žฌ ํŽ˜์ด์ง€) 
  wrapper.innerHTML = '';
  page--; // ๋ฐฐ์—ด 0๋ถ€ํ„ฐ ๋Œ์•„์•ผ๋˜๋‹ˆ๊นŒ -- ํ•ด์ฃผ๊ธฐ

  let start = rows_per_page * page;  // ํ˜„์žฌ ํŽ˜์ด์ง€์— ๋ณด์—ฌ์ค„ ์ฒซ๋ฒˆ์งธ ์•„์ดํ…œ ๋ช‡๋ฒˆ์งธ ์ธ์ง€
  let end = start + rows_per_page;  // ํ˜„์žฌ ํŽ˜์ด์ง€์— ๋ณด์—ฌ์ค„ ๋งˆ์ง€๋ง‰ ์•„์ดํ…œ ๋ช‡๋ฒˆ์งธ์ธ์ง€
  let paginatedItems = items.slice(start, end);  // ๋ฐฐ์—ด์—์„œ ํ˜„์žฌ ํŽ˜์ด์ง€ ์ฒซ๋ฒˆ์งธ ์•„์ดํ…œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ์•„์ดํ…œ๊นŒ์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ
  
  for(let i = 0; i < paginatedItems.length; i++){ // ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ์•„์ดํ…œ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
    let item = paginatedItems[i]; // ํ˜„์žฌ ํŽ˜์ด์ง€์— ๋“ค์–ด๊ฐˆ ์•„์ดํ…œ ๋„ฃ์–ด์ฃผ๊ธฐ

    let itemElement = document.createElement('li'); // ํ•œ ๋””์Šค์ปค์…˜์ด ํ•˜๋‚˜์˜ li์ด๋ฏ€๋กœ li ์ƒ์„ฑ
    itemElement.className = "discussion__container";

    wrapper.append(itemElement);  // ul์— li ๋„ฃ์–ด์ฃผ๊ณ 

    convertToDiscussion(item, itemElement); // ๋งŒ๋“  li์— ๊ฐ ๊ฐ์ฒด ์š”์†Œ ๋„ฃ์–ด์ฃผ๊ธฐ

  }
}


โžฐ setupPagination(): ํŽ˜์ด์ง€ ์ด๋™ ์ œ์–ดํ•จ์ˆ˜ (pagenationButton์„ ํŽ˜์ด์ง€ ์ˆ˜๋งŒํผ ํ˜ธ์ถœ)

// ํŽ˜์ด์ง€ ์ด๋™ ์ œ์–ด ํ•จ์ˆ˜
function setupPagination(items, wrapper, rows_per_page) {
  wrapper.innerHTML = '';

  let page_count = Math.ceil(items.length / rows_per_page); // ์ด ํ•„์š”ํ•œ ํŽ˜์ด์ง€ ์ˆ˜ ๊ตฌํ•˜๊ธฐ
  for(let i = 1; i < page_count + 1 ; i++){	// ์—ฌ๊ธฐ์„œ i๋Š” ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
    let btn = paginationButton(i, items);	
    pageList.append(btn);
  }
}


โžฐ paginationButton() : ํŽ˜์ด์ง€ ๋ฒ„ํŠผ ์ƒ์„ฑ + ํด๋ฆญํ•˜๋ฉด ํŽ˜์ด์ง€ ์ด๋™ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜

// ํŽ˜์ด์ง€ ๋ฒ„ํŠผ ์ƒ์„ฑ + ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ํŽ˜์ด์ง€ ์ด๋™ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ 
function paginationButton(page, items) {
  let paginationBtn = document.createElement('button');	// ํŽ˜์ด์ง€ ๋ฒ„ํŠผ ์ƒ์„ฑ

  paginationBtn.innerText = page;	// ํ˜„์žฌ ์ƒ์„ฑํ•œ ํŽ˜์ด์ง€๊ฐ€ ๋ช‡๋ฒˆ์งธ ํŽ˜์ด์ง€์ธ์ง€ 

  if(currentPage === page) {	// ํ˜„์žฌ ํŽ˜์ด์ง€๊ฐ€ ๋‚ด๊ฐ€ ๋ˆ„๋ฅธ ํŽ˜์ด์ง€๋ฉด
    paginationBtn.classList.add('active');	// ๊ทธ ํŽ˜์ด์ง€ ๋ฒ„ํŠผ์— active ์ฃผ๊ธฐ (์ƒ‰ ๋‹ค๋ฅธ CSS์ถ”๊ฐ€)
  }

  paginationBtn.addEventListener('click', function() {
    currentPage = page;	// ํ˜„์žฌ ํŽ˜์ด์ง€๋ฅผ ์ง€๊ธˆ ๋ˆ„๋ฅธ ํŽ˜์ด์ง€๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
    displayList(items, listElement, rows, currentPage);	// ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ๋””์Šค์ปค์…˜์„ ๋ Œ๋”๋ง ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ํ˜ธ์ถœ

    let currentBtn = document.querySelector('.page_list .active');  // ํ˜„์žฌ active ๋˜์–ด์žˆ๋Š” ๋ฒ„ํŠผ ๊ฐ€์ ธ์™€์„œ
    currentBtn.classList.remove('active');  // active ์ง€์›Œ์ฃผ๊ณ 

    paginationBtn.classList.add('active');  // ์ง€๊ธˆ ๋ˆ„๋ฅธ ๋ฒ„ํŠผ์— active ํ•ด์ฃผ๊ธฐ
  })  

  return paginationBtn;	// ํ˜„์žฌ ๋งŒ๋“  ๋ฒ„ํŠผ์„ ๋ฐ˜ํ™˜
}


โžฐ ํŽ˜์ด์ง€๋„ค์ด์…˜ '<' '>' ๋ˆ„๋ฅด๋ฉด ์ด์ „ ํŽ˜์ด์ง€, ๋‹ค์ŒํŽ˜์ด์ง€๋กœ ์ด๋™
๐Ÿšจ items์ด ์„ ์–ธ๋œ ํ•จ์ˆ˜ ์•ˆ์—์„œ ๊ตฌํ˜„ํ•œ๊ฒŒ ์•„๋‹ˆ๋ผ์„œ items ๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์—†๋‹ค.
๊ทธ๋ž˜์„œ ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ displayList()์— ๋„˜๊ฒจ์ค˜์„œ ๋””์Šค์ปค์…˜์„ ๋ Œ๋”๋งํ•ด์™”๋‹ค.

leftBtn.addEventListener('click', function(clickedPage) {
  if(currentPage > firstPage){  // 1ํŽ˜์ด์ง€์ผ ๊ฒฝ์šฐ ์ „ ํŽ˜์ด์ง€๋กœ ์•ˆ๊ฐ„๋‹ค.
    let currentBtn = document.querySelector('.page_list .active');
    currentBtn.classList.remove('active');  // ์ง€๊ธˆ ํ™œ์„ฑํ™” ๋˜์–ด์žˆ๋Š” ๋ฒ„ํŠผ ๋„๊ณ 
    let prevBtn = currentBtn.previousSibling; // ์ง€๊ธˆ ํ™œ์„ฑํ™” ๋˜์–ด์žˆ๋Š” ๋ฒ„ํŠผ์˜ ์ด์ „ ํ˜•์ œ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
    prevBtn.classList.add('active');
    currentPage = currentPage - 1;  // ํ˜„์žฌ ํŽ˜์ด์ง€๋ฅผ ์ง€๊ธˆ ํŽ˜์ด์ง€ - 1 ํ•ด์ฃผ๊ธฐ
    displayList(JSON.parse(localStorage.getItem('Discussions')), listElement, rows, currentPage); // items๋ฅผ ์ „๋‹ฌ๋ฐ›์ง€ ๋ชปํ•ด์„œ ํ˜„์žฌ ๋ฐ์ดํ„ฐ๋กœ ๋ Œ๋”๋ง ํ•ด์ฃผ๊ธฐ
  }
  else{
    alert('์ฒซ๋ฒˆ์งธ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.');
  }
});

rightBtn.addEventListener('click', function() {
  let lastPage = Math.ceil(JSON.parse(localStorage.getItem('Discussions')).length / rows);  // ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€
  if(currentPage < lastPage) {  // ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€์ผ ๊ฒฝ์šฐ ๋‹ค์Œ ํŽ˜์ด์ง€๋กœ ์•ˆ๊ฐ„๋‹ค.
    let currentBtn = document.querySelector('.page_list .active');
    currentBtn.classList.remove('active');
    let nextBtn = currentBtn.nextSibling;
    nextBtn.classList.add('active');
    currentPage = currentPage + 1;
    displayList(JSON.parse(localStorage.getItem('Discussions')), listElement, rows, currentPage);
  }
  else{
    alert('๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.');
  }
});


๐Ÿ“Œ ์ฐธ๊ณ  : Pagination in JavaScript

โœ”๏ธ ๋””์Šค์ปค์…˜ ์œ ์ง€ ๊ธฐ๋Šฅ

โžฐ 'localStorage๊ฐ€ ๋นˆ ๊ฒฝ์šฐ' ์™€ '๋‚ด๊ฐ€ ์ถ”๊ฐ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์–ด์„œ ์ฐจ์žˆ๋Š” ๊ฒฝ์šฐ' ๋ฅผ ๋‚˜๋ˆ ์„œ ๊ตฌํ˜„

// ๊ฒฝ์šฐ์˜ ์ˆ˜ ๋‘๊ฐœ ๋‚˜๋ˆ„์ง€ ์•Š์œผ๋ฉด discussions์ด ๊ณ„์† ์ดˆ๊ธฐํ™” ๋˜์„œ ์ถ”๊ฐ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚ ์•„๊ฐ.
if(localStorage.length === 0){  // ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์œผ๋ฉด (๋‚ด๊ฐ€ ์ถ”๊ฐ€ํ•œ ๊ฑฐ ์—†์„ ๋•Œ)
  localStorage.setItem('Discussions', JSON.stringify(agoraStatesDiscussions));  // ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€์— ์ด๋ฏธ ์ฃผ์–ด์ง„ agoraStatesDiscussion ๋ฐ์ดํ„ฐ ์ €์žฅ
  discussions = JSON.parse(localStorage.getItem('Discussions'));  // ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€์— ์ €์žฅํ•œ ๋ฐ์ดํ„ฐ ๋‹ด์•„์ฃผ๊ธฐ
  displayList(discussions, listElement, rows, currentPage); // ํ™”๋ฉด์— ๋‹ด์•„์˜จ ๋ฐ์ดํ„ฐ ๊ทธ๋ ค์ฃผ๊ธฐ
  setupPagination(discussions, pageList, rows); // ํŽ˜์ด์ง€๋„ค์ด์…˜ ๊ทธ๋ ค์ฃผ๊ธฐ
}

else{ // ์ถ”๊ฐ€ ํ•œ ๊ฒŒ ์žˆ์„ ๋•Œ
  discussions = JSON.parse(localStorage.getItem('Discussions'));  // ์ถ”๊ฐ€ํ•œ๊ฒŒ ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€์— ์ด๋ฏธ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.(๋‚ด๊ฐ€ ์ถ”๊ฐ€ํ•œ ๋ฐ์ดํ„ฐ๊นŒ์ง€ ํฌํ•จ๋˜์–ด์žˆ์Œ)
  displayList(discussions, listElement, rows, currentPage);
  setupPagination(discussions, pageList, rows);
}

// js ์ „์ฒด์— ๊ตฌํ˜„ํ•ด์ค€๋‹ค. -> ์ƒˆ๋กœ๊ณ ์นจํ•˜๊ฑฐ๋‚˜ ํŽ˜์ด์ง€ ์ด๋™ํ–ˆ์„ ๋•Œ ์ƒˆ๋กœ์šด ํ™”๋ฉด์„ ๋ณด์—ฌ์ค˜์•ผํ•˜๋‹ˆ๊นŒ


โžฐ ๋ฒ„ํŠผ ๋ˆŒ๋Ÿฌ์„œ ๋””์Šค์ปค์…˜ ์ถ”๊ฐ€ํ–ˆ์„ ๊ฒฝ์šฐ ๊ทธ ๋””์Šค์ปค์…˜์„ localStorage ์— ์ €์žฅํ•ด์ค€๋‹ค.
โžฐ input์— ๋นˆ์นธ์ด ์žˆ์œผ๋ฉด ์ถ”๊ฐ€ ์•ˆ๋˜๊ณ  alert ๋„์šด๋‹ค.

// submit ๋ˆ„๋ฅด๋ฉด ๋””์Šค์ปค์…˜ ์ถ”๊ฐ€ํ•˜๋Š” ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ
const submitBtn = document.querySelector('.form__submit');
let discussions= [];  // ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๋‹ด์„ ๋ฐฐ์—ด ์„ ์–ธ

submitBtn.addEventListener('click', function(e) {
  e.preventDefault();
  let nameValue = document.querySelector('#name').value;
  let titleValue = document.querySelector('#title').value;
  let storyValue = document.querySelector('#story').value;
  let form = document.querySelector('.form');
  let currentTime = new Date();
  
  const newDiscussion = {
    id: 'random id',
    createdAt: currentTime,
    author: nameValue,
    title: titleValue,
    body: storyValue,
    url: "https://github.com/codestates-seb/agora-states-fe/discussions/45",
    avatarUrl: "./images/๊ณ„ํ”ผ_.png",
    answer: null  // ์•ˆ ๋„ฃ์–ด์ฃผ๋ฉด answer null ์•„๋‹Œ๊ฑฐ if๋ฌธ์— ๊ฑธ๋ ค์„œ ์˜ค๋ฅ˜๋‚œ๋‹ค.
  };
  if(nameValue !== '' && titleValue !== '' && storyValue !== '' ){	// ๋นˆ์นธ ์—†์œผ๋ฉด
    agoraStatesDiscussions.unshift(newDiscussion);  // ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด์— ์ƒˆ๋กœ๋งŒ๋“  ๊ฐ์ฒด ์ถ”๊ฐ€

    discussions.unshift(newDiscussion); // ์›๋ž˜ discussions ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•œ ๋ฐ์ดํ„ฐ ๊ฐ์ฒด ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ 

    localStorage.setItem('Discussions', JSON.stringify(discussions)); // ์ถ”๊ฐ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋”ํ•ด์ง„ discussions๋ฅผ ๋‹ค์‹œ ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€์— ๋ฐ์ดํ„ฐ๋กœ ๋„ฃ์–ด์ค€๋‹ค.
    
    discussionsCount.innerText = '( ' + discussions.length + ' )';  // ๋””์Šค์ปค์…˜ ๊ฐœ์ˆ˜ ์—…๋ฐ์ดํŠธ

    displayList(JSON.parse(localStorage.getItem('Discussions')), listElement, rows, currentPage); // ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ๋„ ํ™”๋ฉด์— ๊ทธ๋ ค์ค˜์•ผ ํ•˜๋ฏ€๋กœ ํ˜ธ์ถœ
    setupPagination(JSON.parse(localStorage.getItem('Discussions')), pageList, rows); // ๋ฐ์ดํ„ฐ๊ฐ€ ๋Š˜์—ˆ์„ ๋•Œ ํŽ˜์ด์ง€ ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ํ˜ธ์ถœ
    
    form.reset();
  }
  else {	// ๋นˆ์นธ ์žˆ์œผ๋ฉด alert
    alert('๋ชจ๋‘ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.');
  }
});

๐Ÿ“Œ localStroage ์„ค๋ช…์€ ์—ฌ๊ธฐ๋กœ


โœ”๏ธ ๋‹ต๋ณ€ ์žˆ์œผ๋ฉด ๋‹ต๋ณ€ ๋ฐ•์Šค๋„ ๋ Œ๋”๋ง

 if(obj.answer !== null){	// ๋‹ต๋ณ€ ์žˆ์œผ๋ฉด ์ด๋ชจ์ง€ ๋„์šฐ๊ณ 
    answeredCheck.textContent = '๐Ÿ’ฌ';
  }
  discussionAnswered.append(answeredCheck);
if(obj.answer !== null) {
    // ๋‹ต๋ณ€ ์ „์ฒด ๋ฐ•์Šค
    const answeredContainer = document.createElement("div");
   answeredContainer.className = "answered__container";
   answeredContainer.classList.add('hide');		// ์ฒ˜์Œ์—” ์•ˆ๋ณด์ด๊ฒŒ ํ–ˆ๋‹ค๊ฐ€


    itemLi.append(answeredContainer);

    answeredCheck.addEventListener('click', function() {	// ์ด๋ชจ์ง€ ๋ˆ„๋ฅด๋ฉด ๋ณด์ด๊ฒŒ
      answeredContainer.classList.toggle('hide');	// ๋ณด์˜€๋‹ค ์•ˆ๋ณด์˜€๋‹ค
    });

 

โœ”๏ธ ๋ฐ”ํƒ•ํ™”๋ฉด์— ์›€์ง์ด๋Š” ์ด๋ฏธ์ง€ ๋งŒ๋“ค๊ธฐ (ํ™”๋ฉด์— ๋‹ฟ์œผ๋ฉด ๊ณต์ฒ˜๋Ÿผ ํŠ•๊ฒจ๋‚˜์˜จ๋‹ค)

โžฐ canvas ์‚ฌ์šฉ

โžฐ init() : ์ดˆ๊ธฐํ™” ํ•จ์ˆ˜

let canvas = document.createElement('canvas');  // ์›๋“ค์ด ์›€์ง์ผ ์บ”๋ฒ„์Šค ์ƒ์„ฑ
canvas.id = "canvas";
body.prepend(canvas);

let getCanvas = document.querySelector('#canvas'); // ์บ”๋ฒ„์Šค ๊ฐ€์ ธ์˜ค๊ธฐ
let ctx = getCanvas.getContext('2d');

getCanvas.setAttribute("width", window.innerWidth);
getCanvas.setAttribute("height", window.innerHeight);

let canvasWidth = getCanvas.width, canvasHeight = getCanvas.height;  // ์บ”๋ฒ„์Šค ํฌ๊ธฐ ๋‹ด๊ธฐ

let circle = [
  { x: 100, y: 100, radius: 80} // ๊ณต ์ขŒํ‘œ, ๋ฐ˜์ง€๋ฆ„ 
]
let dx = 4;
let dy = 4; // ๊ณต ์†๋„ ์กฐ์ ˆ

let anim; // ์• ๋‹ˆ๋ฉ”์ด์…˜ ๋‹ด์„ ๋ณ€์ˆ˜

function init() {
  anim = window.requestAnimationFrame(draw);  // ์ ์ ˆํ•œ ํƒ€์ด๋ฐ์— draw ํ•จ์ˆ˜๋ฅผ ๊ณ„์† ํ˜ธ์ถœํ•œ๋‹ค.
}
๋”๋ณด๊ธฐ

โž• requestAnimationFrame() ์ด๋ž€?

โžฐ ๋ถ€๋“œ๋Ÿฌ์šด ์• ๋‹ˆ๋ฉ”์ด์…˜ ๊ตฌํ˜„์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

โžฐ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ๋ฐ›๋Š”๋ฐ, ๋ฆฌํŽ˜์ธํŠธ ์ด์ „์— ์‹คํ–‰ํ•  ํ•จ์ˆ˜์ด๋‹ค.


โžฐ draw() : ์‹ค์ œ ๋„ํ˜•, ์ด๋ฏธ์ง€๋ฅผ ํ™”๋ฉด์— ๊ทธ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜

function draw() {
  clear();

  for(let i = 0; i < circle.length; i++){
    ball(circle[i].x, circle[i].y, circle[i].radius);
    circle[i].x += dx;  // x ์ขŒํ‘œ๊ฐ€ ๊ณ„์† ๋Š˜์–ด๋‚˜๊ฒŒ
    circle[i].y += dy;  // y ์ขŒํ‘œ๊ฐ€ ๊ณ„์† ๋Š˜์–ด๋‚˜๊ฒŒ
 
    if(circle[i].x >= canvasWidth - circle[i].radius || circle[i].x <= 0 + circle[i].radius){
      dx = -dx; // ์™ผ์ชฝ์ด๋‚˜ ์˜ค๋ฅธ์ชฝ ๋ฒฝ์— ๋‹ฟ์œผ๋ฉด - ๊ฐ’์„ ๊ณฑํ•ด์ค˜์„œ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€๊ฒŒ๋”
    }
    if( circle[i].y >= canvasHeight - circle[i].radius || circle[i].y <= 0 + circle[i].radius){
      dy = -dy;
    }
  }
    anim = window.requestAnimationFrame(draw);
}


โžฐ clear() : ์บ”๋ฒ„์Šค ์ดˆ๊ธฐํ™” ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜

// canvas ์ดˆ๊ธฐํ™”ํ•จ์ˆ˜
function clear() {  // ๋งค ํ˜ธ์ถœ ์‹œ ์บ”๋ฒ„์Šค๋ฅผ ๋น„์›Œ์ค€๋‹ค.
  ctx.clearRect(0, 0, canvasWidth, canvasHeight);
}


โžฐ ball() : ๊ณต ๊ทธ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜

// ๊ณต ๊ทธ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜
function ball(x, y, r) {
  let img = new Image();
  img.src = './images/cinnamon.png';
  ctx.fillStyle = "white";
  ctx.beginPath();
  ctx.arc(x, y, r, 0, Math.PI * 2, true);
  ctx.closePath();
  ctx.fill();

  ctx.drawImage(img, x-80, y-80, 160, 160);
}

 

โœ”๏ธ ์ƒ๋‹จ๋ฐ”์— ๋ฉ”๋‰ด ๋ˆ„๋ฅด๋ฉด ๋‹ซ๊ธฐ, ํƒญ์œผ๋กœ ๋‚ด๋ฆฌ๊ธฐ, ํ™•๋Œ€ & ์ถ•์†Œ
โžฐ ๋ˆ„๋ฅด๋ฉด ์‚ฌ๋ผ์ง€๊ฑฐ๋‚˜ ํฌ๊ธฐ ๋ณ€ํ™” ํ•  ์š”์†Œ์— class ์ œ๊ฑฐ, ์ถ”๊ฐ€ ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„

/* ๋ฉ”๋‰ด ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด */
let exitBtn = document.querySelector('.exitBtn');
let downBtn = document.querySelector('.downBtn');
let smallBtn = document.querySelector('.smallBtn');
let agorastatesTab = document.querySelector('.agorastatesTab');


exitBtn.addEventListener('click', function() {
  main.classList.add('exit');
});

downBtn.addEventListener('click', function() {
  agorastatesTab.classList.remove('change__opacity__zero');
  agorastatesTab.classList.add('change__opacity__one');
  main.classList.add('down');
  main.classList.remove('up');
});


agorastatesTab.addEventListener('click', function() {
  agorastatesTab.classList.remove('change__opacity__one');
  agorastatesTab.classList.add('change__opacity__zero');
  main.classList.add('up');
  main.classList.remove('down');
});


smallBtn.addEventListener('click', function() {
  if(main.classList.contains('sizeBig')){
    main.classList.remove('sizeBig');
    main.classList.add('sizeSmall');
  }
  else {
    main.classList.add('sizeBig');
    main.classList.remove('sizeSmall');
  }
});

 

โžฐ ์•„์‰ฌ์šด์ 

๋ฐ˜์‘ํ˜• ๋ ˆ์ด์•„์›ƒ์œผ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์€ ๊ฒƒ
์ฝ”๋“œ ๋ฆฌํŒฉํ† ๋ง ํ•˜์ง€ ์•Š์€ ๊ฒƒ

 

๐Ÿ“ฃ ๊ตฌํ˜„ ํ™”๋ฉด