CSS-keyframes animation

이상헌·2023년 4월 5일

css

목록 보기
2/3
post-thumbnail

keyframes, animation

animation 과 keyframes를 사용하면 어떤 이벤트 없이도 css를 통해 움직임을 구현할수있다.

keyframes

animation의 시작 중간 끝의 상태를 정의 한다.

ex)

@keyframes moveBox {
  from {
    border-radius: 0;
    left: 0;
    background-color: blue;
    transform: scale(1);
  }
  to {
    border-radius: 50%;
    left: calc(100% - 100px);
    background-color: green;
    transform: scale(0.75);
  }
}

위의 예시처럼 시작을 from 끝을 to로 작성할수있고
더욱 세밀한 움직임을 표현 할경우 %를 사용할수있다.

animation

animation: name duration timing-function delay iteration-count direction

animation-name : 어떤 keyframes를 적용할건지
animation-duration : animation 작동 시간
animation-timing-function : anmation 재생 패턴
animation-delay : 지연
animation-iteration-count : animation 재생 횟수
animation-direction : animation 재생 방향

ex)

<div class="container">
      <div class="item">
        <span>item</span>
      </div>
    </div>
.container {
  width: 100%;
  height: 104px;
  border: 2px solid red;
  position: relative;
}

.item {
  width: 100px;
  height: 100px;
  background-color: blue;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  animation: moveBox 2s ease-in-out infinite alternate;
  /* animation-name: moveBox;
  animation-duration: 2s;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
  animation-direction: alternate; */
  position: absolute;
}

.item span {
  color: white;
}

@keyframes moveBox {
  from {
    border-radius: 0;
    left: 0;
    background-color: blue;
    transform: scale(1);
  }
  to {
    border-radius: 50%;
    left: calc(100% - 100px);
    background-color: green;
    transform: scale(0.75);
  }
}
profile
반갑습니다.

0개의 댓글