์๋ฐ์คํฌ๋ฆฝํธ : ๊ฒ์ ํจ๊ณผ : ๊ฒ์ ๊ฒ์
์ง๊ธ๊น์ง ๋ค์ํ ๋ฉ์๋๋ฅผ ํ์ฉํ์ฌ ๊ฒ์ ํจ๊ณผ๋ฅผ ์์ ํด ๋ณด์์ต๋๋ค. ์ด๋ฒ์๋ CSS ์์ฑ์ ์ง์ ํ์ดํํ์ฌ ์ ํ ์๊ฐ ์์ ๋งํ๋ ๊ฒ์ ๊ฒ์์ ๋ง๋ค์ด ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
1. ์ ํ์ ์์ ํ๊ธฐ
const searchTime = document.querySelector(".time span");
const searchList = document.querySelector(".search__list");
const searchAnswers = document.querySelector(".search__answers");
const searchMissAnswers = document.querySelector(".search__missAnswers");
const searchStart = document.querySelector(".search__box .start");
const searchInput = document.querySelector("#search");
const searchAudio = document.querySelector("#audio");
const musicPlay = document.querySelector(".search__audio .play");
const musicStop = document.querySelector(".search__audio .stop");
const searchScoreNow = document.querySelector(".search__info .now");
const searchResult = document.querySelector(".search__result .result");
const searchResultWrap = document.querySelector(".search__result");
const searchRestart = document.querySelector(".search__result .restart");
let timeReamining = 120, // ์ ํ ์๊ฐ
timeInterval = "", // ๋จ์ ์๊ฐ
score = 0, // ์ ์
answers = {}; // ์๋ก์ด ์ ๋ต
๋ ๊ทธ๋์๋ฏ์ด... ์ ํ์๋ฅผ ๋จผ์ ๋ง๋ค์ด ์ค๋๋ค. ๊ฒ์๊ณผ ๊ด๋ จ๋ ๋ณ์๋ค๋ ํจ๊ป ์ ์ธํด ์ฃผ์์ต๋๋ค.
2. ์์ฑ ์ถ๋ ฅํ๊ธฐ
const cssProperty = [
{ num: 1, name: "accent-color", desc: " ์์์ ๊ฐ์กฐ ์์์ ์ง์ ํฉ๋๋ค. " },
{ num: 2, name: "align-content", desc: " ์ฝํ
์ธ ์ฌ์ด์ ์ฝํ
์ธ ์ฃผ์ ๋น ๊ณต๊ฐ์ ํ๋ ์ค ๋ฐ์ค์ ๊ต์ฐจ์ถ ๋๋ ๊ทธ๋ฆฌ๋์ ๋ธ๋ก ์ถ์ ๋ฐ๋ผ ๋ฐฐ์นํ๋ ๋ฐฉ์์ ๊ฒฐ์ ํฉ๋๋ค." },
{ num: 3, name: "align-items", desc: " ๊ต์ฐจ์ถ์์ ์์ดํ
์ ๋ ฌ ํ๋ ์์ฑ์
๋๋ค. ์ฃผ๋ก flex์์ item๋ค์ ์ ๋ ฌํ ๋ ์ฌ์ฉ๋ฉ๋๋ค." },
{ num: 4, name: "align-self", desc: " ํน์ item์ ์ ๋ ฌ์ ๋ฐ๋ก ํ๊ณ ์ถ์ ๋ ์ฌ์ฉํฉ๋๋ค." },
{ num: 5, name: "all", desc: " CSS ์ฌ์ฉ์ ์ง์ ์์ฑ์ ์ ์ธํ ๋ชจ๋ ์์ฑ์ ์ด๊ธฐํํฉ๋๋ค. ์ด๊น๊ฐ, ์์๊ฐ, ์๋๋ฉด ๋ค๋ฅธ ์คํ์ผ์ํธ ์ถ์ฒ์ ๊ฐ๋ก ์ค์ ํ ์ ์์ต๋๋ค." },
{ num: 6, name: "animation", desc: " ์ ๋๋ฉ์ด์
๊ณผ ๊ด๋ จ๋ ์์ฑ์ ์ผ๊ด์ ๋ก ์ฒ๋ฆฌํฉ๋๋ค." },
{ num: 7, name: "animation-delay", desc: " ์ ๋๋ฉ์ด์
์ง์ฐ ์๊ฐ์ ์ค์ ํฉ๋๋ค." },
{ num: 8, name: "animation-direction", desc: " ์ ๋๋ฉ์ด์
์์ง์ ๋ฐฉํฅ์ ์ค์ ํฉ๋๋ค." },
{ num: 9, name: "animation-duration", desc: " ์ ๋๋ฉ์ด์
์์ง์ธ ์๊ฐ์ ์ค์ ํฉ๋๋ค." },
{ num: 10, name: "animation-fill-mode", desc: " ์ ๋๋ฉ์ด์
์ด ๋๋ ํ์ ์ํ๋ฅผ ์ค์ ํฉ๋๋ค." },
{ num: 11, name: "animation-iteration-count", desc: " ์ ๋๋ฉ์ด์
์ ๋ฐ๋ณต ํ์๋ฅผ ์ค์ ํฉ๋๋ค." },
{ num: 12, name: "animation-name", desc: " ์ ๋๋ฉ์ด์
keyframe ์ด๋ฆ์ ์ค์ ํฉ๋๋ค." },
{ num: 13, name: "animation-play-state", desc: " ์ ๋๋ฉ์ด์
์งํ์ํ๋ฅผ ์ค์ ํฉ๋๋ค." },
{ num: 14, name: "animation-timeline", desc: " ์ ๋๋ฉ์ด์
์ ํ์๋ผ์ธ์ ์ค์ ํฉ๋๋ค." },
{ num: 15, name: "animation-timing-function", desc: " ์ ๋๋ฉ์ด์
์์ง์์ ์๋๋ฅผ ์ค์ ํฉ๋๋ค." },
{ num: 16, name: "appearance", desc: " ์ด์์ฒด์ ๋ฐ ๋ธ๋ผ์ฐ์ ์ ๊ธฐ๋ณธ์ ๋ก ์ค์ ๋์ด ์๋ ํ
๋ง๋ฅผ ๊ธฐ๋ฐ๋ก ์์๋ฅผ ํํํ๋ค." },
{ num: 17, name: "aspect-ratio", desc: " ์์์ ํฌ๊ธฐ๋ฅผ ๋น์จ๋๋ก ์กฐ์ ํ ์ ์๊ฒ ํ๋ค." },
{ num: 18, name: "backdrop-filter", desc: " ์์ ๋ค ์์ญ์ ํ๋ฆผ์ด๋ ์์ ์ํํธ ๋ฑ ๊ทธ๋ํฝ ํจ๊ณผ๋ฅผ ์ ์ฉํ ์ ์๋ ์์ฑ์
๋๋ค." },
{ num: 19, name: "backface-visibility", desc: " ์์์ ๋ท์ชฝ์์ ์๋ฉด์ด ๋ณด์ด๊ฒ ํ ์ง ์ ํ๋ ์์ฑ์
๋๋ค." },
{ num: 20, name: "background", desc: " ๋ฐฑ๊ทธ๋ผ์ด๋ ์์ฑ์ ์ผ๊ด์ ๋ก ์ค์ ํฉ๋๋ค. " }
<-- ์๋ต -->
]
document.querySelector(".search__info span").innerHTML = cssProperty.length;
function updateList(){
cssProperty.forEach(data => {
searchList.innerHTML += `${data.name}`;
})
}
updateList();
์์ฑ์ ๋ฐฐ์ด๊ณผ ๊ฐ์ฒด๋ก ์์ฑํด ์ฃผ๊ณ , ์ถ๋ ฅํด ์ค๋๋ค. ์ ์ฒด ์์ฑ์ ๊ฐ์๋ ํจ๊ป ์ถ๋ ฅํด ์ฃผ์์ต๋๋ค.
3. BGM ์์ ํ๊ธฐ
function music(){
musicStop.addEventListener("click", () => {
musicStop.style.display = "none";
musicPlay.style.display = "block";
searchAudio.pause();
})
musicPlay.addEventListener("click", () => {
musicPlay.style.display = "none";
musicStop.style.display = "block";
searchAudio.play();
})
searchAudio.play();
}
์์ ์ ์ฌ์ฉํ BGM์ ์์ ํด ์ค๋๋ค. ์ค๋์ค ๊ด๋ จ ๋ฒํผ์ ๋ ๊ฐ์ง๊ฐ ์์ต๋๋ค. ์์ ์ด ์ฌ์๋๊ณ ์์ ๋์๋ ์์ ์ ์ง ๋ฒํผ๋ง์ด ๋ํ๋๋ฉฐ, ์ ์ง ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์์ ์ด ์ ์ง๋๊ณ , ๋ค์ ์ฌ์ ๋ฒํผ๋ง์ด ๋ํ๋ฉ๋๋ค.
4. ์ ๋ต ์ฒดํฌํ๊ธฐ
function checkInput(){
let input = event.currentTarget.value.trim().toLowerCase();
if(answers.hasOwnProperty(input) && !answers[input]){
answers[input] = true;
// ์ ๋ต ํ์
searchAnswers.style.display = "block";
searchAnswers.innerHTML += `<span>${input}</span>`;
// ์ ์ ๋ฐ์
score++;
searchScoreNow.innerText = score;
// ์ ๋ต ์ด๊ธฐํ
searchInput.value = "";
}
}
function checkAnswers() {
cssProperty.forEach(item => {
let answer = item.name.trim().toLocaleLowerCase();
answers[answer] = false;
});
}
searchInput.addEventListener("input", checkInput);
์ ๋ต์ ์ฒดํฌํ๋ ค๋ฉด ์ฌ์ฉ์๊ฐ input์ ์
๋ ฅํ ๊ฐ์ value๋ก ๊ฐ์ ธ์์ผ ํฉ๋๋ค.
checkInput ํจ์๋ ์ฌ์ฉ์์ ์
๋ ฅ๊ฐ์ CSS ์์ฑ๋ค๊ณผ ๋น๊ตํ๊ณ , ์ผ์นํ ๊ฒฝ์ฐ(์ ๋ต์ผ ๊ฒฝ์ฐ)
์๋์ ํด๋น ์์ฑ์ ํ์ํด ์ฃผ๊ณ , ์ ์๋ฅผ 1์ ์ฌ๋ฆฌ๋ฉฐ, input์ ๋น์์ค๋๋ค.
๋, input์ ๊ฐ์ด ์
๋ ฅ๋์์ ๋, checkInput() ํจ์๊ฐ ์คํ๋๋๋ก ์ด๋ฒคํธ ์์
์ ํด ์ค๋๋ค.
5. ์ ํ ์๊ฐ ์ค์ ํ๊ธฐ
function reduceTime(){
timeReamining--;
if(timeReamining == 0) endQuiz();
searchTime.innerText = displayTime();
}
function displayTime(){
if(timeReamining <= 0){
return "0:00";
} else {
let minutes = Math.floor(timeReamining / 60);
let seconds = timeReamining % 60;
if(seconds < 10) seconds = "0" + seconds;
return minutes + ":" + seconds;
}
}
๊ฒ์์ด๋ ์ ํ ์๊ฐ๋ ์์ด์ผ๊ฒ ์ฃ ? reduceTime() ํจ์๋ ์ดํ setInterval์ ์ด์ฉํ์ฌ 1์ด์ ํ ๋ฒ์ฉ ์คํ์ํฌ ๊ฒ์
๋๋ค.
timeReamining์ ๊ฐ์ 120์ผ๋ก, ์ฒ์์ ์ ํ์ ์์
์ ํ ๋ ์ค์ ํด ๋์์ต๋๋ค. 1์ด์ ํ ๋ฒ์ฉ ๊ฐ์ 1์ฉ ์ค์ฌ์ฃผ๊ณ ,
0์ด ๋์์ ๋ ๊ฒ์์ด ๋๋๋๋ก ํ๊ณ , ์๊ฐ์ ์ถ๋ ฅํด ์ฃผ๋๋ก ์์
ํด ์ฃผ์์ต๋๋ค.
displayTime() ํจ์์ ๊ฒฝ์ฐ, 120, 119 ๋ฑ์ ์ซ์๋ฅผ 2:00, 1:59 ๋ฑ์ผ๋ก ๋ฐ๊พธ์ด์ฃผ๋ ์ญํ ์ ํฉ๋๋ค.
6. ๊ฒ์ ์์ ํจ์ ๋ง๋ค๊ธฐ
function startQuiz(){
searchStart.style.display = "none"; // ์์ ๋ฒํผ ์จ๊ธฐ๊ธฐ
searchList.style.display = "none"; // ์ ๋ต๋ค ์จ๊ธฐ๊ธฐ
searchAnswers.innerHTML = ""; // ๊ธฐ์กด ๋ฐ์ดํฐ ์ด๊ธฐํ
searchMissAnswers.innerHTML = "";
timeInterval = setInterval(reduceTime, 1000); // ์ ํ ์๊ฐ ์ค์
music(); // ์์
let score = 0; // ์ ์ ๊ณ์ฐ
checkAnswers(); // ์ ๋ต ์ฒดํฌ
}
searchStart.addEventListener("click", startQuiz);
์ด์ , ๊ฒ์ ์์ ๋ฒํผ์ ํด๋ฆญํ๋ฉด, ์ง๊ธ๊น์ง ์์ ํ ํจ์๋ค์ด ์คํ๋๋๋ก ์์ ํด ์ค๋๋ค.
7. ์ค๋ต ๋ณด์ฌ์ฃผ๊ธฐ
function missAnswers(){
searchMissAnswers.style.display = "block";
cssProperty.forEach(item => {
let answer = item.name.trim().toLocaleLowerCase();
if(answers[answer] == false){
searchMissAnswers.innerHTML += `<span>${item.name}</span>`
}
})
}
๊ฒ์์ด ๋๋ฌ์ ๋, ์ค๋ต๋ค์ ๋ชจ๋ ๋ณด์ฌ์ค ์ ์๋๋ก ์์ ํด ์ค๋๋ค.
8. ๊ฒ์ ์ค๋ฒ ํจ์ ๋ง๋ค๊ธฐ
function endQuiz(){
searchStart.style.display = "block"; // ์์ ๋ฒํผ
searchStart.style.pointerEvents = "none";
missAnswers(); // ์ค๋ต ๋ณด์ฌ์ฃผ๊ธฐ
searchAudio.pause(); // ์์
์ ์ง
clearInterval(timeInterval); // ์๊ฐ ์ ์ง
searchResultWrap.classList.add("show"); // ๋ฉ์์ง ์ถ๋ ฅ
let point = Math.round(score / cssProperty.length * 100);
searchResult.innerHTML = `๊ฒ์์ด ์ข
๋ฃ๋์์ต๋๋ค.<br>${cssProperty.length}๊ฐ ์ค ${score}๊ฐ๋ฅผ ๋งํ์ต๋๋ค.<br>์ ์๋ ${point}์ ์
๋๋ค.`;
}
์์, reduceTime() ํจ์ ๋ด์์ ์ ํ ์๊ฐ์ด ๋๋ฌ์ ๋, endQuiz() ํจ์๊ฐ ์คํ๋๋๋ก ์์ ํด ์ฃผ์์ต๋๋ค. ๊ฒ์์ด ๋๋๋ฉด ๋ค์ ์์ ๋ฒํผ์ ๋ณด์ฌ์ฃผ๊ณ , ๋ชจ๋ ๊ฒ์ด ์ ์ง๋๋ฉฐ, ์ ์์ ํจ๊ป ๋ฉ์์ง๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค.
9. ๋ค์ ์์ ํจ์ ๋ง๋ค๊ธฐ
function restart(){
searchResultWrap.classList.remove("show");
searchAudio.play();
startQuiz();
timeReamining = 120;
score = 0;
searchScoreNow.innerText = 0;
}
searchRestart.addEventListener("click", restart);
๊ฒ์์ด ๋๋ ์ดํ, ๋ค์ ์์ ๋ฒํผ์ ๋๋ ์ ๋์ ํจ์๋ฅผ ์์ ํด ์ฃผ๋ฉด ๋์ ๋๋ค. ๊ฒ์ ์์ ๋ฒํผ์ ๋๋ ์ ๋์ ๊ฐ์ด, ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์ด๊ธฐํํด ์ค๋๋ค.
๊ฒฐ๊ณผ
'Javascript Effect' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๋ฐ์คํฌ๋ฆฝํธ : ๊ฒ์ ํจ๊ณผ : ๋ฉ๋ชจ๋ฆฌ ๊ฒ์ (3) | 2022.10.29 |
---|---|
์๋ฐ์คํฌ๋ฆฝํธ : ์ฌ๋ผ์ด๋ ํจ๊ณผ : ๋ฒํผ, ๋ท๋ฉ๋ด (1) | 2022.10.18 |
์๋ฐ์คํฌ๋ฆฝํธ : ๊ฒ์ ํจ๊ณผ : ๋ฎค์ง ํ๋ ์ด์ด (1) | 2022.10.18 |
์๋ฐ์คํฌ๋ฆฝํธ : ๊ฒ์ ํจ๊ณผ : sort(), reverse() (0) | 2022.10.18 |
์๋ฐ์คํฌ๋ฆฝํธ ํจ๋ด๋์ค ํจ๊ณผ : ๋ฆฌ๋น ํจ๊ณผ (4) | 2022.10.09 |
๋๊ธ