CSS 를 활용한 애니메이션 체크박스 스타일링

Beginner

This tutorial is from open-source community. Access the source code

소개

이 랩에서는 CSS 를 사용하여 상태 변경 시 애니메이션이 적용된 사용자 정의 체크박스를 만드는 방법을 배웁니다. 체크 기호를 만들기 위해 SVG 요소를 사용하고, 체크박스 레이아웃을 위해 Flexbox 를 사용하며, 줌 효과를 만들기 위해 CSS 애니메이션을 사용합니다. 이 랩을 마치면 웹 프로젝트에 스타일리시하고 인터랙티브한 체크박스를 만들 수 있는 기술을 갖추게 됩니다.

이것은 가이드 실험입니다. 학습과 실습을 돕기 위한 단계별 지침을 제공합니다.각 단계를 완료하고 실무 경험을 쌓기 위해 지침을 주의 깊게 따르세요. 과거 데이터에 따르면, 이것은 초급 레벨의 실험이며 완료율은 92%입니다.학습자들로부터 89%의 긍정적인 리뷰율을 받았습니다.

사용자 정의 체크박스

index.htmlstyle.css는 이미 VM 에 제공되었습니다.

상태 변경 시 애니메이션이 적용된 스타일 지정된 체크박스를 만들려면 다음 단계를 따르세요.

  1. <symbol> 요소가 내부에 있는 <svg> 요소로 체크 기호를 만듭니다. 재사용 가능한 SVG 아이콘으로 삽입하려면 <use> 요소를 사용합니다.
  2. .checkbox-container 클래스가 있는 컨테이너 div 를 만듭니다. Flexbox 를 사용하여 체크박스에 적절한 레이아웃을 만듭니다.
  3. <input> 요소를 숨기고 레이블을 연결하여 체크박스와 제공된 텍스트를 표시합니다.
  4. 상태 변경 시 체크 기호에 애니메이션을 적용하려면 stroke-dashoffset을 사용합니다.
  5. 줌 애니메이션 효과를 만들려면 CSS 애니메이션을 통해 transform: scale(0.9)를 사용합니다.

HTML:

<svg class="checkbox-symbol">
  <symbol id="check" viewbox="0 0 12 10">
    <polyline
      points="1.5 6 4.5 9 10.5 1"
      stroke-linecap="round"
      stroke-linejoin="round"
      stroke-width="2"
    ></polyline>
  </symbol>
</svg>

<div class="checkbox-container">
  <input class="checkbox-input" id="apples" type="checkbox" />
  <label class="checkbox" for="apples">
    <span>
      <svg width="12px" height="10px">
        <use xlink:href="#check"></use>
      </svg>
    </span>
    <span>Apples</span>
  </label>
  <input class="checkbox-input" id="oranges" type="checkbox" />
  <label class="checkbox" for="oranges">
    <span>
      <svg width="12px" height="10px">
        <use xlink:href="#check"></use>
      </svg>
    </span>
    <span>Oranges</span>
  </label>
</div>

CSS:

.checkbox-symbol {
  position: absolute;
  width: 0;
  height: 0;
  pointer-events: none;
  user-select: none;
}

.checkbox-container {
  box-sizing: border-box;
  background: #ffffff;
  color: #222;
  height: 64px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-flow: row wrap;
}

.checkbox-container * {
  box-sizing: border-box;
}

.checkbox-input {
  position: absolute;
  visibility: hidden;
}

.checkbox {
  user-select: none;
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 6px;
  overflow: hidden;
  transition: all 0.3s ease;
  display: flex;
}

.checkbox:not(:last-child) {
  margin-right: 6px;
}

.checkbox:hover {
  background: rgba(0, 119, 255, 0.06);
}

.checkbox span {
  vertical-align: middle;
  transform: translate3d(0, 0, 0);
}

.checkbox span:first-child {
  position: relative;
  flex: 0 0 18px;
  width: 18px;
  height: 18px;
  border-radius: 4px;
  transform: scale(1);
  border: 1px solid #cccfdb;
  transition: all 0.3s ease;
}

.checkbox span:first-child svg {
  position: absolute;
  top: 3px;
  left: 2px;
  fill: none;
  stroke: #fff;
  stroke-dasharray: 16px;
  stroke-dashoffset: 16px;
  transition: all 0.3s ease;
  transform: translate3d(0, 0, 0);
}

.checkbox span:last-child {
  padding-left: 8px;
  line-height: 18px;
}

.checkbox:hover span:first-child {
  border-color: #0077ff;
}

.checkbox-input:checked + .checkbox span:first-child {
  background: #0077ff;
  border-color: #0077ff;
  animation: zoom-in-out 0.3s ease;
}

.checkbox-input:checked + .checkbox span:first-child svg {
  stroke-dashoffset: 0;
}

@keyframes zoom-in-out {
  50% {
    transform: scale(0.9);
  }
}

오른쪽 하단 모서리에 있는 'Go Live'를 클릭하여 포트 8080 에서 웹 서비스를 실행하세요. 그런 다음 Web 8080 탭을 새로 고쳐 웹 페이지를 미리 볼 수 있습니다.

요약

축하합니다! 사용자 정의 체크박스 랩을 완료했습니다. LabEx 에서 더 많은 랩을 연습하여 기술을 향상시킬 수 있습니다.