폰트어썸 상품 리뷰 별점 만들기

폰트어썸(FontAwesome)으로 쉽게 별점 만드는 방법
2 min read
폰트어썸 상품 리뷰 별점 만들기 / 별점 반개 표시

쇼핑몰이나 전자상거래(eCommerce) 사이트에서 상품 리뷰 평점을 매길 때 별점을 사용합니다. 별 모양 특수문자 기호를 사용하여 반만 색칠된 별 모양을 만들 수 있습니다. 하지만 스타일이 잘 꾸며져 있지 않아 별로입니다.

또 다른 방법으로 별 모양 이미지 PNG 파일을 사용하여 투명한 배경 위로 별 이미지를 겹쳐서 표시할 수 있습니다. 그러나 이 방법도 수정하기가 어렵고 번거롭습니다. 이 포스팅에서는 폰트 어썸(FontAwesome)으로 간편하게 별점 만드는 방법에 대해 살펴봅니다.

목차

별점 기능 구현

이 기능의 작동 원리는 매우 간단합니다. 제이쿼리없이 폰트어썸과 자바스크립트로만 간단한 코드를 추가하고 리뷰 점수를 수정하면 별점 기능이 자동으로 구현됩니다.

별점 주기를 반개(0.5) 단위로 표시할 수 있습니다.

상품 리뷰 별점 예시

전체 코드

<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.2/css/all.min.css'/>

<div class='RatingStar'>
  <div class='RatingScore'>
    <div class='outer-star'><div class='inner-star'></div></div>
  </div>
</div>

<style>
.inner-star::before{color: #FF9600;}
.outer-star {position: relative;display: inline-block;color: #CCCCCC;}
.inner-star {position: absolute;left: 0;top: 0;width: 0%;overflow: hidden;white-space: nowrap;}
.outer-star::before, .inner-star::before {content: '\f005 \f005 \f005 \f005 \f005';font-family: 'Font Awesome 5 free';font-weight: 900;}
</style>

<script>/*<![CDATA[*/ ratings = {RatingScore: 4.5} 
totalRating = 5;table = document.querySelector('.RatingStar');function rateIt() {for (rating in ratings) {ratingPercentage = ratings[rating] / totalRating * 100;ratingRounded = Math.round(ratingPercentage / 10) * 10 + '%';star = table.querySelector(`.${rating} .inner-star`);numberRating = table.querySelector(`.${rating} .numberRating`);star.style.width = ratingRounded;numberRating.innerText = ratings[rating];}}rateIt()
/*]]>*/</script>

FontAwesome

  • 폰트어썸 5 무료 버전 cdn를 불러옵니다.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.2/css/all.min.css"/>

HTML

  • 'RatingScore'는 점수를 매기기 위한 역할입니다.
  • 'outer-star'는 별점의 배경 역할입니다.
<div class='RatingStar'>
  <div class='RatingScore'>
    <div class='outer-star'><div class='inner-star'></div></div>
  </div>
</div>

CSS

  • 별점의 색상을 바꾸려면 'inner-star'의 color를 수정합니다.
  • 별점의 배경색을 바꾸려면 'outer-star'의 color 를 수정합니다.
  • 별점 갯수를 10개로 변경하려면 '\f005 \f005 \f005 \f005 \f005 \f005 \f005 \f005 \f005 \f005' 으로 변경합니다.
.inner-star::before{color: #FF9600;}
.outer-star {position: relative;display: inline-block;color: #CCCCCC;}
.inner-star {position: absolute;left: 0;top: 0;width: 0%;overflow: hidden;white-space: nowrap;}
.outer-star::before, .inner-star::before {content: '\f005 \f005 \f005 \f005 \f005';font-family: 'Font Awesome 5 free';font-weight: 900;}

Javascript

  • 별점의 평점은 0 부터 5 까지 0.5 단위로 별점을 매길 수 있습니다.
<script>/*<![CDATA[*/ ratings = {RatingScore: 4.5} 
totalRating = 5;table = document.querySelector('.RatingStar');function rateIt() {for (rating in ratings) {ratingPercentage = ratings[rating] / totalRating * 100;ratingRounded = Math.round(ratingPercentage / 10) * 10 + '%';star = table.querySelector(`.${rating} .inner-star`);numberRating = table.querySelector(`.${rating} .numberRating`);star.style.width = ratingRounded;numberRating.innerText = ratings[rating];}}rateIt()
/*]]>*/</script>

You may like these posts

  • Snippet 연산자는 게시물 본문 요약글입니다. 게시물 목록의 짧은 글을 볼 수 있을 텐데, Snippet 연산자를 이용한 것입니다. Snippet 연산자 파라미터 설명 length …
  • 비교 연산자는 두 값이 동등한지 검사하는 연산자입니다. 결과는 불리언 값을 반환합니다. 연산자 목록 이름 연산자 피연산자 기본 구문 응용 구문 결과 값 …
  • 람다는 괄호와 화살표를 이용해 표현합니다.블로거는 람다 형식의 연산자를 지원합니다. Ramda 연산자 블로거에서 람다 표현식의 작동 방식을 보다 잘 이해하려면 데이터에 대한 지식과 다른 연산자에 대한 지식이 필요할지 모릅니다. 블로거에서 사용되는 람다식은 개별 인스턴스가 아닌 전체 데이터(라벨,…
  • 이미지 연산자는 이미지의 크기와 비율을 재정의하거나 반응형으로 만드는 것과 같은 특성을 Google 이미지의 URL에 적용할 수 있도록 하는 Blogger 전용 연산자입니다. 구문 이름 연산자 피연산자 응용 구문 …
  • Blogger의 논리 연산자는 불리언 값을 비교하는 연산자입니다. 결과는 불리언 값을 반환합니다. 구문 이름 연산자 피연산자 기본 구문 응용 구문 결과 …
  • eval 연산자는 표현식의 결과를 표시하는데 사용됩니다. 블로거 데이터 값이나 문자열과 결합한 값을 출력합니다. eval 연산자 eval 연산자는 expr에 있는 블로거 변수와 일반 문자열, 그리고 숫자와 같은 값을 결합한 새로운 값을 출력합니다. <b:eval expr='조건식'/> 예를 들어 구독 …

Post a Comment