728x90
๋ฐ์ํ
โญ๏ธ Javascript โ Quiz Effect โญ๏ธ
์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ํ์ฉํ์ฌ ํด์ฆ๋ฅผ ๋ง๋ค์ด๋ณด์.
ํด์ฆ์ ๊ตฌ์ฑ
ํด์ฆ๋ฅผ ๋ง๋ค๊ธฐ ์ํด ํ์ํ ์ ๋ณด๋ก๋ ํด์ฆ ์ข
๋ฅ, ํด์ฆ ๋ฒํธ, ์ง๋ฌธ ๋ด์ฉ, ์ ๋ต ๊ฒฐ๊ณผ์ ํ์ธ ๋ฒํผ ๋ฑ์ด ์์ต๋๋ค.
<div>์ ์ด๋ฆ์ ์ง์ ํ๊ณ , ์ํ๋ ๊ณณ์ ์ ๋ณด์ ๊ฐ์ ์ถ๋ ฅํฉ๋๋ค.
[3] ์ฃผ๊ด์ ์ ํ โ ๋ค์์ ํด์ฆ (์ ๋ต ์ค๋ต ํ์ธํ๊ธฐ)
์ฌ๋ฌ ๊ฐ์ ์ง๋ฌธ ๋ด์ฉ๊ณผ ๋ต๋ณ ์ ๋ ฅ์ฐฝ ๋ฐ ๋ต์ด ์์ผ๋ฉฐ, ํด๋ฆญํ๋ฉด ์ ๋ต ํ์ธ ๋ฐ ๊ฐ์์ง์ ํ์ ์ด ๋ณํํ๋ ํด์ฆ ๋ฌธํญ์ ๋ง๋ค์ด๋ด ๋๋ค.
// 01. ์ ํ์ : ์์๋ฅผ ์ ํํ๋ ๋ฐฉ๋ฒ (์ถ๋ ฅ๊ฐ์ด ์ฌ๋ฌ ๊ฐ์ด๋ฏ๋ก querySelector ๋์ 'All'์ ๋ถ์ฌ์ค.)
const quizType = document.querySelectorAll(".quiz__type"); // ํด์ฆ ์ข
๋ฅ
const quizNumber = document.querySelectorAll(".quiz__question .number"); // ํด์ฆ ๋ฒํธ
const quizAsk = document.querySelectorAll(".quiz__question .ask"); // ์ง๋ฌธ ๋ด์ฉ
const quizConfirm = document.querySelectorAll(".quiz__answer .confirm"); // ์ ๋ต ํ์ธ ๋ฒํผ
const quizResult = document.querySelectorAll(".quiz__answer .result"); // ์ ๋ต ๋ด์ฉ
const quizInput = document.querySelectorAll(".quiz__answer .input"); // ์ฌ์ฉ์ ๋ต๋ณ
const quizView = document.querySelectorAll(".quiz__view"); // ๊ฐ์์ง ๋ชจ์ต
// 02. ์ถ๋ ฅํ ์ ๋ณด์ ๋ํ ๊ฐ์ ์
๋ ฅํฉ๋๋ค. (๋ค์์ ๊ฐ, ๊ฐ์ฒด๋ก ํํ)
const quizInfo = [
{
answerType: "์น ๋์์ธ ๊ธฐ๋ฅ์ฌ (2015๋
10์)",
answerNum: "1",
answerAsk: "์์์๋ ์์ ๋๋ ์ ์๋ ํ์์ ๋ฌด์์ด๋ผ๊ณ ํ ๊น์?",
answerResult: "์์ฒญ"
},
{
answerType: "์น ๋์์ธ ๊ธฐ๋ฅ์ฌ (2015๋
10์)",
answerNum: "2",
answerAsk: "์ ์์ฐํธ(e-mail)์ ์ ์กํ ๋ ์ฌ์ฉ๋๋ ํ๋กํ ์ฝ์ ๋ฌด์์ด๋ผ๊ณ ํ ๊น์?",
answerResult: "SMTP"
},
{
answerType: "์น ๋์์ธ ๊ธฐ๋ฅ์ฌ (2015๋
10์)",
answerNum: "3",
answerAsk: "๋ง๋ง์ ๋ค๋ฅธ ์๊ด์ด ์๊ทนํ์ฌ ํผํฉ๋๋ ํ์์ผ๋ก, ์ ์ ์ด ์๋ก ๊ฐ๊น๊ฒ ์์ด ๋ช
๋์ ์ฑ๋๊ฐ ๋จ์ด์ง์ง ์๋ ํผํฉ ๋ฐฉ์์ ๋ฌด์์ด๋ผ๊ณ ํ ๊น์?",
answerResult: "๋ณ์นํผํฉ"
},
{
answerType: "์น ๋์์ธ ๊ธฐ๋ฅ์ฌ (2015๋
10์)",
answerNum: "4",
answerAsk: "์ต์ด์ GUI ํ๊ฒฝ ์น ๋ธ๋ผ์ฐ์ ๋ ๋ฌด์์ผ๊น์?",
answerResult: "๋ชจ์์ดํฌ"
},
]
// 03. ํด์ฆ๋ฅผ ํ๋ฉด์ ์ถ๋ ฅํ๊ธฐ
// (1) ํ๋์ฉ ์ ์ด์ ์ถ๋ ฅํ๊ธฐ -> ํด์ฆ ๊ฐฏ์๊ฐ ๋ง์์ง์๋ก ํ๋์ฉ ์ถ๋ ฅํ๊ธฐ ์ด๋ ต๋ค. (ex. 100๊ฐ์ ํด์ฆ, ๋๋ ๊ทธ ์ด์์ ์๊ฐํด๋ณด๋ฉด)
// quizType[0].textContent = quizInfo[0].answerType; // ํด์ฆ ์ข
๋ฅ
// quizType[1].textContent = quizInfo[1].answerType;
// quizType[2].textContent = quizInfo[2].answerType;
// quizType[3].textContent = quizInfo[3].answerType;
// quizNumber[0].textContent = quizInfo[0].answerNum + ". "; // ํด์ฆ ๋ฒํธ
// quizNumber[1].textContent = quizInfo[1].answerNum + ". ";
// quizNumber[2].textContent = quizInfo[2].answerNum + ". ";
// quizNumber[3].textContent = quizInfo[3].answerNum + ". ";
// quizAsk[0].textContent = quizInfo[0].answerAsk; // ์ง๋ฌธ ๋ด์ฉ
// quizAsk[1].textContent = quizInfo[1].answerAsk;
// quizAsk[2].textContent = quizInfo[2].answerAsk;
// quizAsk[3].textContent = quizInfo[3].answerAsk;
// quizResult[0].textContent = quizInfo[0].answerResult; // ์ ๋ต ๋ด์ฉ
// quizResult[1].textContent = quizInfo[1].answerResult;
// quizResult[2].textContent = quizInfo[2].answerResult;
// quizResult[3].textContent = quizInfo[3].answerResult;
// (2) ์ผ์ผ์ด ํ๋์ฉ ์ถ๋ ฅํ๊ธฐ ์ด๋ ค์ฐ๋ฏ๋ก, ์์ ๋ด์ฉ์ for๋ฌธ์ผ๋ก ํํํด๋ณธ๋ค.
// for( i=0; i<4; i++ ){ // ์ซ์ 4 ๋ก ์ ์ด๋ ๋์ง๋ง, ์ซ์๊ฐ ๋ ์ปค์ก์ ๊ฒฝ์ฐ๋ฅผ ์๊ฐํ์ฌ 'quizInfo.length'๋ก ์ ์ด์ฃผ์.
// quizType[i].textContent = quizInfo[i].answerType;
// quizNumber[i].textContent = quizInfo[i].answerNum + ". ";
// quizAsk[i].textContent = quizInfo[i].answerAsk;
// quizResult[i].textContent = quizInfo[i].answerResult;
// }
// (3) for๋ฌธ์ forEach๋ฌธ์ผ๋ก๋ ํํ์ด ๊ฐ๋ฅํ๋ค. ๊ทธ๋ ๊ทธ๋ ์ํฉ์ ๋ง๋ ๊ฒ์ผ๋ก ๊ณจ๋ผ์ ์ฌ์ฉํ์.
quizInfo.forEach( (e, i) => { // ํ์ดํ ํจ์๋ก ๋ฐ๊ฟ. e ๋ element, i ๋ index ๋ฅผ ์๋ฏธ.
quizType[i].textContent = quizInfo[i].answerType;
quizNumber[i].textContent = quizInfo[i].answerNum + ". ";
quizAsk[i].textContent = quizInfo[i].answerAsk;
quizResult[i].textContent = quizInfo[i].answerResult;
})
// 04. '์ ๋ต ํ์ธ ๋ฒํผ' ๋ฐ์ผ๋ก ๋ณด์ด๋ "์ ๋ต ๋ด์ฉ" ์จ๊ฒจ๋๊ธฐ
// (1) ์ผ์ผ์ด ํ๋์ฉ ์ถ๋ ฅํ๊ธฐ -> ์์ ๋ง์ฐฌ๊ฐ์ง๋ก ์ซ์๊ฐ ์ปค์ง๋ฉด ํ๋ค๋ค.
// quizResult[0].style.display = "none";
// quizResult[1].style.display = "none";
// quizResult[2].style.display = "none";
// quizResult[3].style.display = "none";
// (2) for๋ฌธ์ผ๋ก ํํํ๊ธฐ
// for( i=0; i<quizInfo.length; i++ ){
// quizResult[i].style.display = "none";
// }
// (3) for๋ฌธ์ forEach๋ฌธ์ผ๋ก ํํํ๊ธฐ
quizInfo.forEach((e, i) => {
quizResult[i].style.display = "none";
});
// 05. ์ ๋ต ํ์ธ ๋ฒํผ ๋ง๋ค๊ธฐ (๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ ๋ต,์ค๋ต์ ๋ฐ๋ผ ๊ฐ์์ง ํ์ ์ด ๋ณํ๋๋ก ์ถ๋ ฅํ๊ธฐ)
// quizConfirm.addEventListener("click", () => { // ํ๋๋ง ์์ ๋๋ ์คํ๋์ง๋ง ์ฌ๋ฌ ๊ฐ์ฌ์ ์คํ์ด ๋์ง ์์.
// }); // ๋จ์ผ ํด์ฆ์ผ ๋์๋ ์ฑ๋ฆฝ๋์ง๋ง ์ง๊ธ์ ํด์ฆ๊ฐ ์ฌ๋ฌ๊ฐ ์ด๊ธฐ์ ์์ด ์ฑ๋ฆฝ๋์ง ์์. ์๋์ ๋ค์ ์ ์ด์ฃผ๊ฒ ์.
// quizConfirm.forEach((btn) => { // quizConfirm ์๊ฒ forEach ํจ์๋ฅผ ์ฃผ์ด '์ฌ๋ฌ ๊ฐ๊ฐ ์์ด'๋ผ๊ณ ์๋ฐ์คํฌ๋ฆฝํธ์๊ฒ ์๋ ค์ฃผ์ด์ผ ํจ.
// btn.addEventListener("click", () => { // btn ์ quizConfirm ์ ๋์
๋๋ ๊ฒ.
//
// // ์ฌ์ฉ์ ๋ต๋ณ
// const userWord = quizInput.values.toLowerCase().trim(); // ๋๋ฌธ์๋ฅผ ์๋ฌธ์๋ก ๋ฐ๊พธ๊ณ ๋น์นธ์ ์๊ด ์์ด ์ ๋ต์ ํ์ธํ๋๋ก
// })
// });
quizConfirm.forEach((btn, num) => {
btn.addEventListener("click", () => {
const userWord = quizInput[num].value.toLowerCase().trim(); // ์ฌ์ฉ์ ๋ต๋ณ
// console.log(userWord) // ์ถ๋ ฅ๊ฐ์ด ๋ฐ๋ฅด๊ฒ ๋์ค๋์ง console.log ๋ก ํ์ธํ๊ธฐ
// console.log(quizInfo[num].answerResult)
if(userWord == quizInfo[num].answerResult){ // ์ฌ์ฉ์ ๋ต๋ณ == ๋ฌธ์ ์ ๋ต ์ด๋ฉด~
// alert("์ ๋ต") // ์ ๋ต์ผ ๋ ์ถ๋ ฅ๊ฐ์ด ๋ฐ๋ฅด๊ฒ ๋์ค๋์ง ํ์ธ
quizView[num].classList.add("like"); // ์ฌ๋ฌ ๊ฐ๊ฐ ๋์์ ์ถ๋ ฅ๋์ง ์๋๋ก [num] ์ ์ ์ด์ค๋ค. -> ์์๋ฅผ ์ฐพ์๊ฐ๋๋ก ์ค์
quizConfirm[num].style.display = "none"; // ์ ๋ต์ด๋ฉด ๊ฐ์์ง๊ฐ ์๋ ํ์ ์ด ๋์ค๊ณ ,
} else {
// alert("์ค๋ต") // ์ค๋ต์ผ ๋ ์ถ๋ ฅ๊ฐ์ด ๋ฐ๋ฅด๊ฒ ๋์ค๋์ง ํ์ธ
quizView[num].classList.add("dislike"); // ๊ทธ๋ ์ง ์์ผ๋ฉด ์ฐ๋ ํ์ ์ด ๋์ค๊ฒ ๋๋ค.
quizConfirm[num].style.display = "none";
quizResult[num].style.display = "block";
quizInput[num].style.display = "none"; // ํ๋ ธ์ ๋ ์ ๋ต๋ง ๋ณด์ด๋๋ก display ์ค์ .
}
})
});
๊ฒฐ๊ณผ๋ณด๊ธฐ
728x90
๋ฐ์ํ
'Javascript > Effect' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
search Effect (1) (2) | 2022.08.18 |
---|---|
ํด์ฆ ๋ง๋ค๊ธฐ (5) ๊ฐ๊ด์ ๋ค์ ๋ฌธํญ (3) | 2022.08.16 |
ํด์ฆ ๋ง๋ค๊ธฐ (4) ๊ฐ๊ด์ ๋จ์ผ ๋ฌธํญ (7) | 2022.08.09 |
ํด์ฆ ๋ง๋ค๊ธฐ (2) ์ฃผ๊ด์ ๋จ์ผ ๋ฌธํญ (4) | 2022.08.04 |
ํด์ฆ ๋ง๋ค๊ธฐ (1) ์ ๋ต ํ์ธํ (5) | 2022.08.04 |
๋๊ธ