HTML, CSS, JS

[HTML, CSS] 별점, 평점 시스템 예시

yubi5050 2022. 6. 12. 21:58

리뷰 평점 매기거나, 별점 매길때 재사용하기 좋은 코드..

  • 기능 1) 마우스 Hover 시 색 들어옴
  • 기능 2) 별점 클릭시 JavaSciprt로 value 전송

 

👉 예시 사진


 

👉 HTML 파일

<html>
  <body>
      <div class="star-rating space-x-4 mx-auto">
          <input type="radio" id="5-stars" name="rating" value="5" onclick='getRating(event)'/><label for="5-stars">★</label>
          <input type="radio" id="4-stars" name="rating" value="4" onclick='getRating(event)'/><label for="4-stars">★</label>
          <input type="radio" id="3-stars" name="rating" value="3" onclick='getRating(event)'/><label for="3-stars">★</label>
          <input type="radio" id="2-stars" name="rating" value="2" onclick='getRating(event)'/><label for="2-stars">★</label>
          <input type="radio" id="1-stars" name="rating" value="1" onclick='getRating(event)'/><label for="1-stars">★</label>
      </div>
  </body>
</html>

 

👉 CSS 파일

.star-rating {
display: flex;
flex-direction: row-reverse;
font-size: 2.25rem;
line-height: 2.5rem;
justify-content: space-around;
padding: 0 0.2em;
text-align: center;
width: 5em;
}

.star-rating input {
display: none;
}

.star-rating label {
-webkit-text-fill-color: transparent;
-webkit-text-stroke-width: 2.3px;
-webkit-text-stroke-color: #2b2a29;
cursor: pointer;
}

.star-rating :checked ~ label {
-webkit-text-fill-color: gold;
}

.star-rating label:hover,
.star-rating label:hover ~ label {
-webkit-text-fill-color: #fff58c;
}

 

👉 JavaScript 코드

<script>
  function getRating() {
      const rating = document.getElementsByName('rating');

      rating.forEach((star) => {
        if(star.checked)  {
            console.log(star.value);
        }
      }) 
    }
</script>

 

👉 전체 Full 코드

<html>
<head>
  <style>
  .star-rating {
    display: flex;
    flex-direction: row-reverse;
    font-size: 2.25rem;
    line-height: 2.5rem;
    justify-content: space-around;
    padding: 0 0.2em;
    text-align: center;
    width: 5em;
  }
      
  .star-rating input {
    display: none;
  }
      
  .star-rating label {
    -webkit-text-fill-color: transparent;
    -webkit-text-stroke-width: 2.3px;
    -webkit-text-stroke-color: #2b2a29;
    cursor: pointer;
  }
      
  .star-rating :checked ~ label {
    -webkit-text-fill-color: gold;
  }
    
  .star-rating label:hover,
  .star-rating label:hover ~ label {
    -webkit-text-fill-color: #fff58c;
  }
</style>
  <script>
      function getRating() {
          const rating = document.getElementsByName('rating');
          
          rating.forEach((star) => {
            if(star.checked)  {
                console.log(star.value);
            }
          }) 
        }
  </script>
  </head>

  <body>
      <div class="star-rating space-x-4 mx-auto">
          <input type="radio" id="5-stars" name="rating" value="5" onclick='getRating(event)'/><label for="5-stars">★</label>
          <input type="radio" id="4-stars" name="rating" value="4" onclick='getRating(event)'/><label for="4-stars">★</label>
          <input type="radio" id="3-stars" name="rating" value="3" onclick='getRating(event)'/><label for="3-stars">★</label>
          <input type="radio" id="2-stars" name="rating" value="2" onclick='getRating(event)'/><label for="2-stars">★</label>
          <input type="radio" id="1-stars" name="rating" value="1" onclick='getRating(event)'/><label for="1-stars">★</label>
      </div>
  </body>
</html>