Computer >> 컴퓨터 >  >> 프로그래밍 >> CSS

CSS perspective-origin 속성으로 애니메이션 구현하기

CSS의 perspective-origin 속성은 3D 변형에서 원근감(perspective)의 기준점, 즉 소실점(vanishing point)의 위치를 지정합니다. 이 속성에 애니메이션을 적용하면 관찰자의 시점이 움직이는 듯한 입체적인 효과를 연출할 수 있습니다.

주요 특징

  • X축 위치와 Y축 위치 두 가지 값을 지정하며, px, % 등 다양한 단위를 사용할 수 있습니다.
  • 기본값은 50% 50%으로, 요소의 중심이 소실점이 됩니다.
  • @keyframes 규칙과 함께 사용하면 소실점의 위치를 부드럽게 전환할 수 있습니다.

예제 코드

다음 코드는 perspective-origin 속성에 애니메이션을 적용한 예제입니다. 실행하면 애니메이션이 진행되는 동안 소실점이 이동하고, 그에 따라 내부 요소의 3D 회전 모습이 달라지는 것을 확인할 수 있습니다.

<!DOCTYPE html>
<html>
   <head>
      <style>
         #demo1 {
            position: relative;
            margin: auto;
            height: 250px;  
            width: 350px;
            padding: 10px;
            border: 2px solid orange;
            perspective: 125px;
            animation: myanim 3s infinite;
         }
         @keyframes myanim {
            70% {
               perspective-origin: 5px 30%;
            }
         }
         #demo2 {
            padding: 70px;
            position: absolute;
            border: 2px solid black;
            background-color: blue;
            color: white;
            transform: rotateX(30deg);
         }
      </style>
   </head>
   <body>
      <h1>CSS perspective-origin property</h1>
      <div id = "demo1">This is demo text in div1.
         <div id = "demo2">This is demo text in div2.</div>
      </div>
   </body>
</html>

코드 설명

  • #demo1: 외부 컨테이너로, perspective: 125px를 설정해 자식 요소에 원근감을 부여하고, 3초간 무한 반복되는 myanim 애니메이션을 적용합니다.
  • @keyframes myanim: 애니메이션 진행률 70% 지점에서 perspective-origin5px 30%로 변경하여 소실점의 위치를 왼쪽 위로 이동시킵니다.
  • #demo2: rotateX(30deg)로 X축 방향으로 30도 기울어진 내부 요소로, 소실점이 변함에 따라 마치 카메라가 움직이는 것처럼 입체적으로 보입니다.