💡 CSS의 Transition, Transform, Animation 예제 정리
Interative 한 웹 페이지를 만들기 위한 기초인 CSS Transition, Animation을 정리해보자.
사용자의 상호작용에 따라 화려하게 움직이는 웹 사이트를 만들기 위해서는 Javascript 지식이 상당히 필요하지만, 기본적으로 Transition과 Animation을 아는 것만으로도 어느 정도 구현이 가능하다.
이번 포스트에서는 Transition과 Animation의 속성들을 다뤄보도록 하겠다.
CSS transitions는 여러분이 (명시적으로 목록을 작성해서) 어떤 속성을 움직이게 할지, (딜레이를 설정해서) 언제 애니메이션이 시작할지, (지속 시간을 설정해서) 트랜지션을 얼마나 지속할지, 그리고 (예를 들면, 선형이거나 초기 빠름, 종료 느림과 같은 타이밍 함수를 정의해서) 어떻게 트랜지션을 실행하는지 결정하게 합니다.
<style>
.transition {
transition-property: width;
transition-duration: 2s;
}
</style>
.property
: 효과를 적용하고자 하는 css 속성
.duration
: 효과가 나타나는데 걸리는 시간
<style>
.transition {
transition-timing-function: linear;
transition-delay: 1s;
}
</style>
.timing-function
: 효과의 속도 (linear는 ‘일정하게’라는 의미)
.delay
: 특정 조건 하에서 효과 발동 (1s는 ‘1초 후’라는 의미)
<style>
.transition:hover { width: 300px; }
</style>
.transition:hover
를 띄어쓰기 없이 작성해야 함종합 예시
<style>
.transition {
transition: width 2s linear 1s;
}
.transition:hover { width: 300px; }
</style>
CSS 속성을 변경할 때 애니메이션 속도를 조절할 수 있게 한다.
CSS의 속성 변화가 지정한 설정에따라 일어날 수 있게 만들어 준다.
<style>
.transition {
transfrom: rotate(45deg);/* 회전 */transform: scale(2,3);/* 확대 축소 */
}
</style>
.rotate(angle)
: 입력한 각도만큼 회전하며, 음수도 입력 가능.scale(x, y)
: 숫자는 비율의 의미하며 width를 x배, height를 y배 만큼 확대Point IIskew, translate
<style>
.transition {
transform: skew(10deg, 20deg);/* 각도 변경 */transform: translate(100px; 200px);/* 위치 변경 */
}
</style>
.skew(x, y)
: x축, y축을 기준으로 입력한 각도만큼 비틂.translate(x, y)
: 선택한 오브젝트의 좌표 변경Point IIIprefix 접두사
<style>
.transition {
-webkit-transform: translate(100px, 200px);/* 크롬, 사파리 */
-mox-transform: translate(100px, 200px);/* 파이어폭스 */
-ms-transform: translate(100px, 200px ;/* 익스플로러 9.0 */
-o-transform: translate(100px, 200px);/* 오페라 */
}
</style>
CSS Selector의 종류를 알아보고 예제를 통해 어떻게 적용되는지 확인해보겠다.
.animation {
animation-name: changeWidth;/* 애니매이션 이름 */animation-duration: 3s;
animation-timing-function: linear;
animation-delay: 1s;
animation-iteration-count: 6;
animation-direction: alternate;
}
@keyframes changeWidth {
from { width: 300px; }
to {width: 600px; ]
}
.iteration-count
: 애니메이션 반복 횟수.direction
: 애니매이션 진행 방향alternate
: from → to → fromnormal
: from → to, from → toreverse
: to → from, to → fromPoint ITransform & Animation
.box1 {
animation: rotation 1500ms linear infinite alternate;
}
@keyframes rotation {
from { transform: rotate(-10deg); }
to { transform: rotate(10deg); }
}
.duration
이고, 나중에 나오는 숫자가 .delay
Point IIprefix 작성시 유의사항
.box1 {
-webkit-animation: rotation 3s linear 1s 6 alternate;
}
@-webkit-keyframes rotation {
from {-webkit-transform: rotate(-10deg); }
to {-webkit-transform: rotate(10deg); }
}