별점

Rating

별 같은 아이콘을 채워서 5점 만점 등 정해진 척도의 점수를 주고받는 컨트롤.

다른 이름: Star ratingScore picker
···
html
<div class="rt">
  <div class="stars" id="stars" role="radiogroup" aria-label="평점">
    <button role="radio" aria-checked="false" data-v="1">★</button>
    <button role="radio" aria-checked="false" data-v="2">★</button>
    <button role="radio" aria-checked="false" data-v="3">★</button>
    <button role="radio" aria-checked="false" data-v="4">★</button>
    <button role="radio" aria-checked="false" data-v="5">★</button>
  </div>
  <span class="rtval" id="rtval">0.0 / 5</span>
</div>
css
.rt{display:grid;place-items:center;gap:10px}
.stars{display:flex;gap:2px}
.stars button{border:0;background:none;font-size:26px;line-height:1;color:var(--line);cursor:pointer;padding:2px;transition:color .12s}
.stars button[data-fill]{color:#f5b32a}
.rtval{font-size:12px;color:var(--muted);font-weight:600}
js
const stars = [...document.querySelectorAll('.stars button')];
const val = document.getElementById('rtval');
function paint(n) {
  stars.forEach((s) => { s.toggleAttribute('data-fill', Number(s.dataset.v) <= n); s.setAttribute('aria-checked', String(Number(s.dataset.v) === n)); });
  val.textContent = n.toFixed(1) + ' / 5';
}
let auto = true, committed = 0;
stars.forEach((s) => {
  s.addEventListener('mouseenter', () => paint(Number(s.dataset.v)));
  s.addEventListener('click', () => { auto = false; committed = Number(s.dataset.v); paint(committed); });
});
document.querySelector('.stars').addEventListener('mouseleave', () => paint(committed));
function loop() {
  if (!auto) return;
  let n = 0;
  const fill = setInterval(() => {
    if (!auto) return clearInterval(fill);
    n++;
    paint(n);
    if (n >= 4) { clearInterval(fill); setTimeout(() => { if (auto) { paint(0); setTimeout(loop, 700); } }, 1200); }
  }, 220);
}
setTimeout(loop, 500);

role="radiogroup"으로 감싸고 별 하나하나를 role="radio"로 만들면(또는 시각적으로 숨긴 radio input 5개) 정확히 하나의 점수만 선택되는 라디오 그룹과 동일하게 동작합니다. 별 모양 자체는 스크린리더에 의미가 없으니 "5점 중 3점"처럼 텍스트로 값을 읽어줘야 합니다.

슬라이더와의 차이는 연속/이산입니다 — 슬라이더는 임의의 연속값을 다루지만 별점은 1, 2, 3처럼 딱 떨어지는 작은 척도이고, 아이콘을 눈으로 세어 바로 파악할 수 있다는 게 장점입니다.

마우스를 올렸을 때 미리 채워지는 "미리보기"와 실제로 클릭해 확정하는 "선택"을 구분해야 합니다 — hover만으로 서버에 점수를 저장하면 안 됩니다.