필요없는 목록/CSS 스타일

Quiz 웹사이트 만들기2(간단한 자바스크립트 + HTML + CSS)

비밀안 2023. 3. 9. 14:04
클릭

버튼을 클릭하면 명언이 나와요

-

이름이 나와요

퀴즈사이트 2번째 완성본

완성된 사이트 링크

  • 이번에는 "text"에다가 내용을 입력하고.
  • "정답 확인하기" 버튼을 클릭하면 "정답"과 "오답"을 알려줍니다.

HTML코드

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>퀴즈 이펙트02</title>

    <!-- css -->
    <link rel="stylesheet" href="css/reset.css"/>
    <link rel="stylesheet" href="css/quiz.css"/>

    <!-- 폰트(둥근모) -->
    <link href="https://webfontworld.github.io/DungGeunMo/DungGeunMo.css" rel="stylesheet">
    <link href="https://webfontworld.github.io/PyeongChang/PyeongChang.css" rel="stylesheet">
</head>

<body>
    <header id="header">
        <h1>
            <a href="../assets/javascript/javascript14.html">Quiz</a><span>정답 확인하기 유형</span> <em></em>
        </h1>
        <ul>
            <li><a href="quizEffect01.html">1</a></li>
            <li class="active"><a href="quizEffect02.html">2</a></li>
        </ul>
    </header>
    <!-- testAnGyonam -->
    <!-- //header -->
    <main id="main">
        <div class="quiz__wrap">
            <div class="quiz">
                <div class="quiz__header">
                    <h2 class="quiz__title"><span></span><em></em></h2>
                </div>
                <div class="quiz__main">
                    <div class="quiz__question">
                        <em></em>. <span></span>
                    </div>
                    <div class="quiz__view">
                        <div class="dog__wrap">
                            <div class="card-container">
                              <div class="dog">
                                <div class="head">
                                  <div class="ears"></div>
                                  <div class="face"></div>
                                  <div class="eyes">
                                    <div class="teardrop"></div>
                                  </div>
                                  <div class="nose"></div>
                                  <div class="mouth">
                                    <div class="tongue"></div>
                                  </div>
                                  <div class="chin"></div>
                                </div>
                                <div class="body">
                                  <div class="tail"></div>
                                  <div class="legs"></div>
                                </div>
                              </div>
                            </div>
                          </div>
                    </div>
                    <div class="quiz__answer">
                        <input class="input" type="text" placeholder="정답을 적어주세요!"/>
                        <button class="confirm">정답 확인하기</button>
                        <div class="result">연변대비</div>
                    </div>
                </div>
                <div class="quiz__footer">
                    <div class="quiz__desc">설명</div>
                </div>
            </div>
        </div>
    </main>
    <!-- //main -->
    <footer id="footer">
        <a href="skadldldl123@gmail.com">mailto:skadldldl123@gmail.com</a>
    </footer>
    <!-- //footer -->
</body>
</html>

코드설명

★퀴즈01 HTML과 비슷합니다.(다른것은 8번부터입니다.)

1. main태그는 위에 완성본 이미지 전부를 담아줍니다.

2. div태그를 4개로 나누기 전에 이것들을 전부 담아 줄 / .quiz인 div태그를 생성합니다.

3. div태그 중 .quiz__header는 자바스크립트를 이용해서 "웹디자인 기능사 2012년 1회 기출" 이라고 들어갈 부분입니다.

4. div태그 중 .quiz__main은 div태그 3개(문제, 그림, 정답)를 담아줍니다.

5. div태그의 .quiz__main 안의  .quiz__question의 div태그(문제)는 

   "1.인접하는 두 색의 경계 부분에 색상, 명도, 채도의 대비가 더욱 강하게 일어나는 현상을 무엇이라고 하는가?"를 자바스크립트로 담아줍니다.

6. div태그의 .quiz__main 안의 .quiz__view의 div태그(그림)는 그림을 담아줍니다.(CSS로 조정합니다.)

7. div태그의 .quiz__main 안의 .quiz__answer의 div태그(문제)는 "정답 확인하기" 버튼과 "정답"을 담아줍니다.

8. div태그의 .quiz__main 안의 .quiz__footer의 div태그(TIP+설명)를 만듭니다.

CSS코드

body {
    
}
/* header */
#header {
    position: fixed;
    left: 0;
    top: 0;
    background-color: #262626;
    color: #fff;
    padding: 10px;
    width: 100%;
    z-index: 1000;
    display: flex;
    justify-content: space-between;
}
#header::before {
    content: "";
    border: 4px ridge #a3a3a3;
    position: absolute;
    left: 10px;
    top: 10px;
    /* 하이픈 부분 띄어야 먹힘 */
    width: calc(100% - 10px);
    height: calc(100% - 10px);
}
#header h1 {
    font-size: 28px;
    padding: 5px 5px 5px 10px;
    font-family: "DungGeunMo";
    position: relative;
    z-index: 10;
}
#header h1 span {
    font-size: 0.5rem
}
#header h1 a {
    color: #fff;
}
#header h1 em {
    font-size: 0.5em;
    /* rem : 노트폴더 기준 */
    /* em :  */
}
#header ul {
    padding: 5px;
}
#header ul li {
    display: inline;
    position: relative;
    z-index: 10;
}
#header ul li a {
    color: #fff;
    font-family: "DungGeunMo";
    border: 1px dashed #fff;
    display: inline-block;
    padding: 5px;
    transition: all 0.3s;
}
#header ul li.active a,
#header ul li a:hover {
    background-color: #fff;
    color: #000;
}
/* footer */
#footer {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    background-color: #262626;
    text-align: center;
}
#footer a{
    display: block;
    color: #fff;
    padding: 20px;
    font-family: "DungGeunMo";
    position: relative;
    z-index: 10;
}
#footer::before {
    content: "";
    border: 4px ridge #a3a3a3;
    position: absolute;
    left: 5px;
    top: 5px;
    /* 하이픈 부분 띄어야 먹힘 */
    width: calc(100% - 10px);
    height: calc(100% - 10px);
}
#main {
    padding: 100px 0;
}









/* quiz__wrap */
.quiz__wrap {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
}
.quiz__wrap .quiz {
    width: 500px;
    background-color: #fff;
    border: 8px ridge #cacaca;
}
.quiz__title {
    background-color: #eff308;
    border: 3px ridge #cacaca;
    border-bottom-width: 6px; 
    padding: 5px;
    font-family: "DungGeunMo";
    font-size: 16px;
    color: #3b3b3b;
    text-align: center;
} 
.quiz__question {
    padding: 20px;
    font-size: 24px;
    font-weight: 300;
    font-family: 'PyeongChang';
    border-bottom: 6px ridge #cacaca;
}
.quiz__question em {
    /* color: #ff3c3c; */
}
.quiz__answer {
    font-family: 'PyeongChang';
    padding: 20px;
    text-align: center;
    font-size: 24px;
    /* border-bottom: 6px ridge #cacaca; */
}

.quiz__answer .confirm {
    background-color: #C5EB93;
    border-bottom: 3px ridge #cacaca;
    width: 100%;
    font-family: 'PyeongChang';
    padding: 10px 20px;
    font-size: 22px;
    cursor: pointer;
    transition: all 0.3s;
    font-weight: bold;
}
.quiz__answer .confirm:hover {
    background-color: #d9ffa7;
}
.quiz__answer .result {
    background-color: #fff;
    border: 6px ridge #cacaca;
    width: 100%;
    font-family: 'PyeongChang';
    padding: 10px 20px;
    font-size: 22px;
    /* display: none; */
}
.quiz__answer .input {
    background-color: #fff;
    border: 6px ridge #cacaca;
    border-style: groove;
    width: 100%;
    font-family: 'PyeongChang';
    padding: 10px 20px;
    font-size: 22px;
    margin-bottom: 10px;
}
.quiz__view {
    border-bottom: 6px ridge #cacaca;
    overflow: hidden;
}
.quiz__footer {
    border-top: 6px solid #cacaca;
    padding: 20px;
    font-family: 'PyeongChang';
    background-color: #f5f5f5;
}
.quiz__footer::before {
    content: "★Tip";
    color: #ff3c3c;
    font-weight: bold;
}
.quiz__desc {
    padding-top: 5px;
}

/* dog__wrap */
.dog .tail, 
.dog .chin, 
.dog .tongue::before, 
.dog .tongue::after, 
.dog .mouth, .dog .nose, 
.dog .teardrop, .dog .eyes, 
.dog .face::before, .dog .face::after, 
.dog .ears::before, .dog .ears::after,
.dog__wrap {
    transition: 0.2s ease-in;
}
.dog__wrap {
    background-color: rgb(139, 24, 24);
}
.card-container {
    position: relative;
    width: 360px;
    height: 378px;
    margin: auto;
    padding-top: 125px;
    border-radius: 3%;
    z-index: 0;
}
.card-container::before, .card-container::after {
    content: "";
    position: absolute;
    height: 100%;
    margin: auto;
    left: 0;
    right: 0;
    border-radius: 3%;
    z-index: -1;
}
.card-container::before {
    top: 3%;
    width: 93%;
}
.card-container::after {
    top: 5.5%;
    width: 85%;
}
.dog .head,
.dog .body {
    position: relative;
    width: 115px;
}
.dog .head {
    height: 115px;
    border-radius: 50% 50% 0 0;
    margin: 0 auto;
}
.dog .ears {
    position: relative;
    top: -14%;
    width: 100%;
}
.dog .ears::before, .dog .ears::after {
    content: "";
    position: absolute;
    top: 0;
    width: 35px;
    height: 70px;
    background: #CB7A1D;
    border-top: 11px solid #F7AA2B;
    border-left: 7px solid #F7AA2B;
    border-right: 7px solid #F7AA2B;
}
.dog .ears::before {
    left: 0;
    border-radius: 50% 45% 0 0;
}
.dog .ears::after {
    right: 0;
    border-radius: 45% 50% 0 0;
}
.dog .face {
    position: absolute;
    background: #F7AA2B;
    width: 100%;
    height: 100%;
    border-radius: 50% 50% 0 0;
}
.dog .face::before, .dog .face::after {
    content: "";
    display: block;
    margin: auto;
    background: #FEFEFE;
}
.dog .face::before {
    width: 15px;
    height: 35px;
    margin-top: 24px;
    border-radius: 20px 20px 0 0;
}
.dog .face::after {
    position: absolute;
    bottom: -1px;
    left: 0;
    right: 0;
    width: 60px;
    height: 65px;
    border-radius: 45% 45% 0 0;
}
.dog .eyes {
    position: relative;
    top: 29%;
    text-align: center;
}
.dog .eyes::before, .dog .eyes::after {
    content: "";
    display: inline-block;
    width: 12px;
    height: 12px;
    border-radius: 100%;
    background: #451d1c;
    margin: 0 14.5%;
}
.dog .teardrop {
    position: absolute;
    top: 125%;
    left: 19%;
    width: 6px;
    height: 6px;
    border-radius: 0 50% 50% 50%;
    transform: rotate(45deg);
    background: #FEFEFE;
    visibility: hidden;
}
.dog .nose {
    position: relative;
    top: 35%;
    width: 16px;
    height: 8px;
    border-radius: 35px 35px 65px 65px;
    background: #451d1c;
    margin: auto;
}
.dog .mouth {
    position: relative;
    top: 34.5%;
    width: 4px;
    height: 6px;
    margin: 0 auto;
    text-align: center;
    background: #451d1c;
}
.dog .mouth::before, .dog .mouth::after {
    content: "";
    position: absolute;
    top: -4px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    border: 4px solid #451d1c;
    border-left-color: transparent;
    border-top-color: transparent;
    z-index: 2;
}
.dog .mouth::before {
    transform: translateX(-89%) rotate(45deg);
}
.dog .mouth::after {
    transform: translateX(-2px) rotate(45deg);
}
.dog .tongue {
    position: relative;
    z-index: 1;
}
.dog .tongue::before, .dog .tongue::after {
    content: "";
    position: absolute;
}
.dog .tongue::before {
    top: 10px;
    left: -7px;
    width: 18px;
    height: 0;
    border-radius: 50%;
    background: #451d1c;
    z-index: -1;
}
.dog .tongue::after {
    top: 14px;
    left: -4px;
    width: 12px;
    height: 0;
    border-radius: 20px;
    background: #F5534F;
    z-index: 5;
}
.dog .chin {
    position: relative;
    top: 47.5%;
    margin: 0 auto;
    width: 12px;
    height: 12px;
    border-top: 10px solid #e8e7ec;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-radius: 2px;
    z-index: 0;
}
.dog .body {
    position: relative;
    height: 139px;
    margin: auto;
    z-index: 0;
}
.dog .body::before, .dog .body::after {
    content: "";
    position: absolute;
    top: -1px;
    left: 0;
    right: 0;
    bottom: 0;
    display: block;
    width: 100%;
    margin: auto;
    background: #F7AA2B;
}
.dog .body::after {
    top: -2px;
    bottom: -1px;
    width: 60px;
    background: #FEFEFE;
}
.dog .tail {
    position: absolute;
    left: -60%;
    bottom: 1px;
    background: #F7AA2B;
    width: 93px;
    height: 15px;
    transform: rotate(45deg);
    transform-origin: 100% 50%;
    border-radius: 25px 0 0 25px;
    z-index: -2;
}
.dog .legs {
    position: absolute;
    bottom: 0;
    left: -10%;
    width: 120%;
    height: 15%;
    background: #F7AA2B;
    border-radius: 10px 10px 0 0;
}
.dog .legs::before, .dog .legs::after {
    content: "";
    position: absolute;
    bottom: 1px;
    background: #CB7A1D;
    z-index: -1;
}
.dog .legs::before {
    left: -7.5%;
    width: 115%;
    height: 55%;
    border-radius: 5px 5px 0 0;
}
.dog .legs::after {
    left: -3.5%;
    width: 107%;
    height: 250%;
    border-radius: 20px 20px 35px 35px;
}
@keyframes movetongue {
    100% {
        height: 27px;
    }
}
@keyframes movetail {
    0% {
        transform: rotate(37deg);
    }
    100% {
        transform: rotate(52deg);
    }
}
@keyframes cry {
    100% {
        visibility: visible;
    }
}


.like {
    background: #35a831;
}
.like .face::before {
     margin-top: 10px;
}
.like .face::after {
     height: 85px;
}
.like .eyes {
     top: 13%;
}
.like .eyes::before,
.like .eyes::after {
    width: 18px;
    height: 5px;
    margin: 0px 12.5%;
    transform: rotate(-37.5deg);
    border-radius: 20px;
}
.like .eyes::after {
    transform: rotate(37.5deg);
}
.like .nose {
    top: 18%;
}
.like .mouth {
    top: 16.5%;
}
.like .tongue::before {
    height: 12px;
}
.like .tongue::after {
    height: 24px;
    animation: movetongue 0.1s linear 0.35s infinite alternate forwards;
}
.like .chin {
  top: 34%;
}
.tail {
    animation: movetail 0.1s linear infinite alternate forwards;
}
.like .tail {
    animation: movetail 0.1s linear infinite alternate forwards;
}
.dislike .tail {
    animation: movetail 0.1s linear infinite alternate forwards;
}


/* .btn-dislike:active ~ .wrapper는 dislike로 */
.dislike {
  background: #7575b9;
}
.dislike .ears::before {
  transform: rotate(-50deg) translate(-7px, 2px);
}
.dislike .ears::after {
  transform: rotate(50deg) translate(7px, 2px);
}
.dislike .face::before {
  margin-top: 28px;
}
.dislike .face::after {
  height: 55px;
}
.dislike .eyes {
  top: 38%;
}
.dislike .eyes::before,
.dislike .eyes::after {
  width: 18px;
  height: 5px;
  margin: 0px 14.5%;
  transform: rotate(-37.5deg);
  border-radius: 20px;
}
.dislike .eyes::after {
  transform: rotate(37.5deg);
}
.dislike .teardrop {
  -webkit-animation: cry 0.1s ease-in 0.25s forwards;
          animation: cry 0.1s ease-in 0.25s forwards;
}
.dislike .nose {
  top: 44%;
}
.dislike .mouth {
  top: 42%;
}
.dislike .chin {
  top: 52%;
}
.dislike .tail {
  transform: rotate(0);
}

 

JAVASCRIPT 코드

<script>
      // 선택자
      const quizWrap = document.querySelector(".quiz__wrap");
      const quizTitle = quizWrap.querySelector(".quiz__title span");
      const quizTime = quizWrap.querySelector(".quiz__title em");
      const quizQuestion = quizWrap.querySelector(".quiz__question span");
      const quizQuestionNum = quizWrap.querySelector(".quiz__question em");
      const quizAnswerConfirm = quizWrap.querySelector(".quiz__answer .confirm");
      const quizAnswerResult = quizWrap.querySelector(".quiz__answer .result");
      const quizAnswerInput = quizWrap.querySelector(".quiz__answer .input");
      const dogWrap = quizWrap.querySelector(".dog__wrap");
      const quizDesc = quizWrap.querySelector(".quiz__desc");
      const quizFooter = quizWrap.querySelector(".quiz__footer");

      // 문제 정보
      const infoType = "정보처리 기능사";
      const infoTime = "2011년 5회 기출";
      const infoNumber = "2";
      const infoQuestion = "프레젠테이션에서 화면 전체를 전환하는 단위를 의마하는 것은?";
      const infoAnswer = "슬라이드";
      const infoDesc = "슬라이드는 프레젠테이션의 화면 전체를 말하고 개체는 화면을 구성하는 개개의 요소를 말한다.";
      

      // 문제 출력
      // innerText랑 textContent 두개 똑같음
      quizTitle.textContent = infoType;
      quizTime.textContent = infoTime;
      quizQuestionNum.textContent = infoNumber;
      quizQuestion.textContent = infoQuestion;
      quizDesc.textContent = infoDesc;
      quizAnswerResult.textContent = infoAnswer;

      // 정답 & 해설 숨기기
      quizAnswerResult.style.display = "none";
      quizDesc.style.display = "none";
      quizFooter.style.display = "none";

      // 사용자 정답
      // value를 사용하면 값을 가져옴
      quizAnswerConfirm.addEventListener("click", function(){
        // trim() : 여백이 있으면 여백은 제거 해줌
        // ㄴ이걸 사용하면. 정답 입력하고 스페이스바 눌러도 정답으로 나옴
        const userAnswer = quizAnswerInput.value.trim();
        quizDesc.style.display = "block";
        quizAnswerInput.style.display = "none";
        quizAnswerConfirm.style.display = "none";
        quizAnswerResult.style.display = "block";
        quizFooter.style.display = "block";
        
        if(infoAnswer === userAnswer){
            // 정답 버튼 제거
            dogWrap.classList.add("like");
            quizDesc.style.display = "block";

            // alert("정답입니다.");
        }
        else{
            dogWrap.classList.add("dislike");            
            
            // alert("오답입니다.");
        }
      });
    </script>

코드 설명

1. document.querySelector()로 아이디, 클래스, 태그를 찾습니다.

2. 상수 안에 데이터를 저장합니다.

3. textContent :  HTML요소의 텍스트 내용을 설정하거나 반환하는 자바스크립트 명령어입니다.

4. 대상.textContent = "넣을 문자열"; 이라고 적으면 대상에 text가 추가 및 변경이 됩니다.

5. 대상.style.display= "none" or "block"; 적어주면 대상의 display를 설정 할 수 있습니다.

6. value : form 요소의 값을 설정하거나 반환하는 자바스크립트 명령어입니다.

7. trim() : 여백 제거입니다.(text로 확인할때 여백이 있으면 에러가 납니다. 그떄 trim을 쓰면 여백이 있어도 여백을 없애줍니다.)

8. 대상.classList.add("이름"); 이렇게 주면 대상에 class를 추가합니다.

(대상.classList.remova("이름"); 이렇게 주면 대상의 class를 삭제합니다.)

9. alert("경고창"); alert은 경고창을 띄어줍니다.

아직까지는 견딜만 하는데. 많이 안써봐서 익숙하지가 않네요.