๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Javascript Effect

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ€ด์ฆˆ ํšจ๊ณผ : ์ฃผ๊ด€์‹(์—ฌ๋Ÿฌ ๋ฌธ์ œ) ์œ ํ˜•

by ์ฝ”ํŒŒ์นด 2022. 8. 6.
728x90

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ : ํ€ด์ฆˆ ํšจ๊ณผ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋‹ค์–‘ํ•œ ํšจ๊ณผ๋“ค ์ค‘, ํ€ด์ฆˆ๋ฅผ ๋งžํž ์ˆ˜ ์žˆ๋Š” ํ€ด์ฆˆ ํšจ๊ณผ์ž…๋‹ˆ๋‹ค. ๊ทธ ์ค‘์—์„œ๋„, ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์ œ์— ์ฃผ๊ด€์‹ ์ •๋‹ต์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ํ€ด์ฆˆ ์œ ํ˜•์„ ์ž‘์—…ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


์ฃผ๊ด€์‹(์—ฌ๋Ÿฌ ๋ฌธ์ œ) ์œ ํ˜•

์˜ˆ์‹œ๋Š” ์›น๋””์ž์ธ๊ธฐ๋Šฅ์‚ฌ์˜ ํ•„๊ธฐ ์‹œํ—˜ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๊ฐ ๋ฌธ์ œ์˜ ์ •๋‹ต์„ ๋งžํžˆ๋ฉด ๋ฌธ๊ตฌ๊ฐ€ ๋‚˜ํƒ€๋‚˜๋ฉฐ ๊ฐ•์•„์ง€ ์• ๋‹ˆ๋ฉ”์ด์…˜์ด ๊ธฐ๋ปํ•˜๊ณ , ์ •๋‹ต์„ ๋งžํžˆ์ง€ ๋ชปํ•˜๋ฉด ์Šฌํผํ•˜๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์œผ๋กœ ์ž…๋ ฅ๊ฐ’์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.

// ์„ ํƒ์ž
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"); // ๊ฐ•์•„์ง€

// ๋ฌธ์ œ ์ •๋ณด : ์ƒ์ˆ˜๋กœ ์„ ์–ธ๋œ quizInfo ๋‚ด์— ๋ฐฐ์—ด๋กœ ๊ฐ ๋ฌธ์ œ์˜ ์ •๋ณด๋ฅผ ์ €์žฅ
const quizInfo = [
    {
        answerType: "์›น๋””์ž์ธ๊ธฐ๋Šฅ์‚ฌ 2016๋…„ 1ํšŒ",
        answerNum: "1",
        answerAsk: "๋ฏธ์  ๋Œ€์ƒ์„ ๊ตฌ์ƒํ•˜๋Š” ๋ถ€๋ถ„๊ณผ ๋ถ€๋ถ„์˜ ์‚ฌ์ด์— ์งˆ์ ์œผ๋กœ๋‚˜ ์–‘์ ์œผ๋กœ ๋ชจ์ˆœ๋˜๋Š” ์ผ ์—†์ด ์งˆ์„œ๊ฐ€ ์žกํ˜€ ์–ด์šธ๋ฆฌ๋Š” ๊ฒƒ์€?",
        answerResult: "์กฐํ™”"
    },
    {
        answerType: "์›น๋””์ž์ธ๊ธฐ๋Šฅ์‚ฌ 2016๋…„ 1ํšŒ",
        answerNum: "2",
        answerAsk: "๋””์ž์ธ ์›๋ฆฌ ์ค‘ ๋™์งˆ์˜ ๋ถ€๋ถ„์ด ์กฐํ•ฉ๋  ๋•Œ ์ด๋ฃจ์–ด์ง€๋Š” ๊ฒƒ์€?",
        answerResult: "์œ ์‚ฌ"
    },
    {
        answerType: "์›น๋””์ž์ธ๊ธฐ๋Šฅ์‚ฌ 2016๋…„ 1ํšŒ",
        answerNum: "3",
        answerAsk: " ์ƒ‰๊ด‘์˜ ํ˜ผํ•ฉ์—์„œ ์ƒ‰์„ ํ˜ผํ•ฉํ•˜๋ฉด ํ• ์ˆ˜๋ก ๋†’์•„์ง€๋Š” ์ƒ‰์˜ ์†์„ฑ์€?",
        answerResult: "๋ช…๋„"
    },
    {
        answerType: "์›น๋””์ž์ธ๊ธฐ๋Šฅ์‚ฌ 2016๋…„ 1ํšŒ",
        answerNum: "4",
        answerAsk: "์ƒ‰์ฑ„๋ฅผ ๊ณผํ•™์ ์œผ๋กœ ์ •๋ฆฌํ•˜์—ฌ ์ŠคํŽ™ํŠธ๋Ÿผ์„ 7์ƒ‰์œผ๋กœ ๋ถ„๋ฆฌํ•œ ์‚ฌ๋žŒ์€?",
        answerResult: "๋‰ดํ„ด"
    }
]


// ๋ฌธ์ œ ์ถœ๋ ฅ : 01. ์ผ์ผ์ด ์ถœ๋ ฅ
{
    // ์ถœ์ œ ์‹œ๊ธฐ
    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
}

// ๋ฌธ์ œ ์ถœ๋ ฅ : 02. for๋ฌธ์œผ๋กœ ์ถœ๋ ฅ
for(let i=0; i<quizInfo.length; i++){
    quizType[i].textContent = quizInfo[i].answerType     // ์ถœ์ œ ์‹œ๊ธฐ
    quizNumber[i].textContent = quizInfo[i].answerNum + "."     // ๋ฌธ์ œ ๋ฒˆํ˜ธ
    quizAsk[i].textContent = quizInfo[i].answerAsk     //์งˆ๋ฌธ
    quizResult[i].textContent = quizInfo[i].answerResult     //์ •๋‹ต
}

// ๋ฌธ์ œ ์ถœ๋ ฅ : 03. forEach๋ฌธ์œผ๋กœ ์ถœ๋ ฅ
quizInfo.forEach(function(e, i){
    quizType[i].textContent = quizInfo[i].answerType     // ์ถœ์ œ ์‹œ๊ธฐ
    quizNumber[i].textContent = quizInfo[i].answerNum + "."     // ๋ฌธ์ œ ๋ฒˆํ˜ธ
    quizAsk[i].textContent = quizInfo[i].answerAsk     //์งˆ๋ฌธ
    quizResult[i].textContent = quizInfo[i].answerResult     //์ •๋‹ต
})


// ์ •๋‹ต ์ˆจ๊ธฐ๊ธฐ : 01. ์ผ์ผ์ด ์ˆจ๊ธฐ๊ธฐ
quizResult[0].style.display = "none";
quizResult[1].style.display = "none";
quizResult[2].style.display = "none";
quizResult[3].style.display = "none";

// ์ •๋‹ต ์ˆจ๊ธฐ๊ธฐ : 02. for๋ฌธ์œผ๋กœ ์ˆจ๊ธฐ๊ธฐ
for(let i=0; i<quizInfo.length; i++){
    quizResult[i].style.display = "none";
}

// ์ •๋‹ต ์ˆจ๊ธฐ๊ธฐ : 03. forEach๋ฌธ์œผ๋กœ ์ˆจ๊ธฐ๊ธฐ
quizInfo.forEach(function(e, i){
    quizResult[i].style.display = "none";
})
        

// ์ •๋‹ต ํ™•์ธํ•˜๊ธฐ
// ๋ฒ„ํŠผ์ด ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์œผ๋ฏ€๋กœ, ์•Œ๋ ค์ฃผ๊ธฐ ์œ„ํ•ด for๋ฌธ ํ˜น์€ forEach๋ฌธ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•จ
// ์•„๋ž˜์ฒ˜๋Ÿผ alert์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์—… ์ „ ์˜ค๋ฅ˜๊ฐ€ ์—†๋Š”์ง€ ์ ๊ฒ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ
quizConfirm.addEventListener("click", () => {
    alert("add")
});

// forEach๋ฌธ์„ ์‚ฌ์šฉ
quizConfirm.forEach((btn, num) => {
    btn.addEventListener("click", () => {

        // ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’
        const userWord = quizInput[num].value.toLowerCase().trim();

        // ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’๊ณผ ์ •๋‹ต ๋น„๊ตํ•˜๊ธฐ
        if(userWord == quizInfo[num].answerResult){
            // ์ •๋‹ต์ผ ๊ฒฝ์šฐ
            quizView[num].classList.add("like");     // ๊ฐ•์•„์ง€
            quizConfirm[num].style.display = "none"; // ์ž…๋ ฅ์ฐฝ ์‚ฌ๋ผ์ง€๊ฒŒ ํ•˜๊ธฐ
            quizInput[num].style.display = "none";   // ๋ฒ„ํŠผ ์‚ฌ๋ผ์ง€๊ฒŒ ํ•˜๊ธฐ
            quizResult[num].style.display = "block"; // ์ •๋‹ต ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ
        } else {
            // ์˜ค๋‹ต์ผ ๊ฒฝ์šฐ
            quizView[num].classList.add("dislike");
            quizConfirm[num].style.display = "none";
            quizInput[num].style.display = "none";
            quizResult[num].style.display = "block";
        }
    })
})
728x90

๋Œ“๊ธ€

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๐Ÿฆ™

CSS
๊ด‘๊ณ  ์ค€๋น„์ค‘