transition 속성은 변화하는 단계의 중간 움직임을 생성하도록 도와준다.
예를들어, select메뉴에 커서를 올려놓았을 때 뚝뚝 끊기듯 서브메뉴가 펼쳐지는 것이 아니라 자연스럽게 시간차를 두고 펼쳐지도록 해준다.
속성 적용은 최초 상태의 태그에 한다.
transition: property duration timing-function delay;
<p class="box"><p>
<style>
p.box{
width:200px;
height:200px;
background-color:red;
transition: all 1s ease-in; //모든 속성을 1초동안 ease-in
}
p.box:hover{
width:400px;
height:200px;
background-color: pink;
}
p.box:active{ //actvie - 클릭한 상태
width:300px;
height:200px;
background-color: pink;
}
</style>
빨간색 박스에서 hover 시, 1초동안 파란박스로 색상이 자연스럽게 변화되어 간다.
그리고 박스를 active(클릭) 하고있는 동안, 핑크박스의 색깔과 크기로 변화된다.
transition: font-size 1s ease-out, background-color 2s ease-in 1s;0
초기값에 영향을 미치지 않는 요소의 변형
transform : scale(x,y) translate(x,y) rotate(deg) skew(xdeg,ydeg);
<div id="wrap">
<article>
<p class="one"></p>
</article>
<article>
<p class="two"></p>
</article>
<article>
<p class="three"></p>
</article>
</div>
<style>
#wrap{
width: 800px;
margin: 30px auto;
}
article{
width: 200px;
height: 200px;
margin: 0 0 80px 200px;
border: 5px solid orange;
}
article>p{
width: 200px;
height: 200px;
opacity: 0.5;
}
p.one{
background-color: #0f0;
transform: scale(1.2,1.2) skew(10deg); //1.2(120%) 확대, skew(마름모꼴) 변형
}
p.two{
background-color: pink;
transform-origin: right top; //오른쪽 위를 기준으로 설정
transform:scale(1.2) rotate(45deg); //1.2배 확대, 45도 시계방향회전
}
p.three{
background-color: gold;
transform: translate(70px,50px); //원래 위치에서 가로70, 세로50 이동
}
</style>
프레임안에 있는 이미지에 hover했을 때, 이미지가 1.2(120%)배 확대되며 0.7(70%) 불투명도가 적용된다. 이미지는 0.3초동안 ease-in-out으로 확대된다. 이 때 frame에 overflow:hidden을 적용하지 않으면 이미지 크기가 1.2배 그대로 커지게 된다.
<style>
.frame{
overflow: hidden; //확대된 부분 숨기기
width: 300px;
height: 150px;
margin: 0 auto;
border-radius: 10px;
}
.frame>img{
display: block;
width: 100%;
transition:all 0.3s ease-in-out; //0.3초동안 변화
}
.frame>img:hover{
transform:scale(1.2); //1.2배 확대
opacity: 0.7; //불투명도 70% 적용
}
</style>
<body>
<p class="frame">
<img src="./images/big1.jpg" alt="#">
</p>
</body>