본 내용은 엘리스 SW 4기에서 학습한 내용을 정리한 것입니다.
애니메이션을 구현하는 4가지 방법
1. CSS - Transition
2. CSS - Keyframe Animation
3. Javascript - Web Animation API
4. Javascript - requestAnimationFrame
웹사이트 특정 영역에서 object각도 틀거나 크기 조정, 위치 변경할 때 사용
css3의 최신 언어이기때문에 하위버전에선 사용 불가.
transform으로 단독으로 입력하면 익스플로러에서 사용 불가
각 버전에 맞게 접두사를 붙임
.transition {
-webkit-transform: translate(100px, 200px); /* 크롬, 사파리 */
-moz-transform: translate(100px, 200px); /* 파이어폭스 */
-ms-transform: translate(100px, 200px); /* 익스플로러 9.0 */
-o-transform: translate(100px, 200px); /* 오페라 */
}
transform 속성과 함께 사용되는 속성, 회전 중심을 지정, rotate(), skew() 등의 회전, 변형 속성을 사용하기 전에 기준점 지정
.transform {
width: 100px;
height: 100px;
background-color: aquamarine;
margin: 200px 0 0 200px;
transform: rotate(45deg);
transform: scale(0.5, 2);
transform: translate(100px, 200px);
transform: skew(10deg, 20deg);
}
transform을 여러개 기입하면 겹쳐지게 되어 맨 마지막 속성만 적용이 된다.
/* 일반 width지정 */
.box {
width: 200px;
}
/* transform width지정 */
.box {
width: 100px;
transform: scale(2,1);
}
직접 구현하기 전까지는 레이아웃에 영향을 미치는 지 안 미치는 지 정확히 알 수 없다.
Transform으로 어떻게 지지고 볶든 해도 그 다음 오는 엘리먼트의 위치는 고정이다
마지막에 쓰여진 것부터 실행시킨다. 뒤부터 앞으로 사용된다.
변화하는 과정을 보여주고 싶을 때 사용
transition-property: width;
→ width값이 변화하는 과정을 사용자에게 보여주겠다.transition-duration: 2s;
→ 바뀌는데 어느정도의 소요시간을 설정할 것인지, 2초의 시간을 적용transition-timing-function: linear;
→ linear은 ‘일정하게’라는 의미transition-delay: 1s;
→ 1s는 ‘1초 후’라는 의미선택자:hover { width: 300px; }
: 마우스를 올렸을 때 width값을 바꾼다.마우스를 올리면 1초 후에 width값이 300px로, 2초 동안, 속도 일정하게 변하는 애니메이션 효과 발동
.transition: width 2s linear 1s;
.transition:hover { width: 300px; }
1줄에 다 쓸 수 있지만, 항상 먼저 나오는 숫자가 duration이고 그 다음에 나오는 숫자가 delay. (숫자가 하나인 경우 ⇒ duration)
마우스를 올리면 1초 후에 width값이 300px로, 2초 동안 속도 일정하게 변하는 애니메이션 효과 발동
조건에 상관없이 어떤 이벤트를 적용하고 싶을 때 사용
ex) 웹사이트 들어가자마자 사진이 돌아간다던가
.animation {
animation-name: changeWidth;
animation-duration: 3s;
animation-timing-function: linear;
animation-delay: 1s;
animation-iteration-count: 6;
animation-direction: alternate;
}
/* keyframs 옆에는 애니메이션 이름을 작성
위에는 조건, 아래는 실제 변화되는 결과값을 입력.
300픽셀에서 600픽셀로 변화
*/
@keyframes changeWidth{
from { width: 300px ;}
to { width: 600px; }
}
무한: infinite
% 속성 : 애니메이션의 실행 중간 속성
.box1 {
animation: rotation 1500ms linear infinite alternate;
}
@keyframes rotation{
from { transform: rotate(-10deg); }
to { transform: rotate(10deg); }
}
.box1 {
animation: rotation 3s linear 1s 6 alternate;
}
@-webkit-keyframes rotation {
from {
-webkit-transform: rotate(-10deg);
}
to {
-webkit-transform: rotate(10deg);
}
}
#intro nav ul li a{
transition: color 1s;
}
#intro nav ul li a: hover {
color: #917f7f;
}
/* 배경색 변경 효과 */
#main article.one{
transition: background-color 1s;
}
#main article.one{
background-color: #8683bd;
}
/* 이미지 크기 확대 효과 */
#main article img {
transition: all 1s;
}
#main article img: hover{
transform: scale(1.1);
}
.container {
width: 960px;
margin: 0 auto;
}
🧐 사각형이 떨리면서 움직이게 하고 싶다
=> div태그를 부모, 자식으로 나누고, 부모의 div태그에 shake애니메이션을 지정하고, 자식의 div태그에 움직이는 애니메이션을 넣으면 됨
<style>
div.container {
width: 100px;
height: 100px;
animation-name: shake;
animation-duration: 400ms;
animation-iteration-count: infinite;
}
div.container div {
width: 100%;
height: 100%;
background-color: #333;
animation: move, color;
animation-duration: 5s, 1s;
animation-timing-function: ease-out, ease-in-out;
animation-delay: 300ms, 100ms;
animation-play-state: running;
animation-direction: normal;
animation-iteration-count: infinite;
}
@keyframes move {
from,to { transform: translate(0, 0); }
10% { transform: translate(30px, 80px) rotate(45deg); }
30% { transform: translate(200px, 0px); }
50% {
transform: translate(100px, 50px) rotate(140deg) scale(1.3, 1.3);
color: red;
}
60% { transform: translate(200px, 200px); }
80% {
transform-origin: 0 100%;
transform: translate(0px, 200px) scale(2, 2);
}
}
@keyframes color {
from, to { background-color: #333; }
50% { background-color: blue; }
70% { background-color: yellow; }
90% { background-color: crimson; }
}
@keyframes shake {
from, to { transform: scale(1, 1); }
33% { transform: scale(1.4, 1.4); }
66% { transform: scale(0.8, 0.8); }
}
</style>
...
<body>
<div class="container">
<div>Hello</div>
</div>
</body>
.media {
width: 500px;
height: 500px;
background-color: red;
}
@media (min-width: 320px) and (max-width: 800px) {
width: 300px;
height: 300px;
background-color: yellow;
}
viewport라는 meta태그에 속성값을 반드시 기입 > head태그 안 쪽에 기입
<meta name="viewport" content="width=device-width", initial-scale=1.0">
.media {
width: 500px;
height: 500px;
background-color: yellow;
}
@media (min-width: 320px) and (max-width: 800px) {
.media {
width: 300px;
height: 300px;
background-color: none;
}
}
@media (min-width: 320px) and (max-width: 1000px) {
.container {
width: 640px;
}
#intro {
width: 100%;
}
#intro h1 {
width: 100%;
height: 160px;
}
#intro h1 a {
text-align: center;
padding: 22px 0 0 0;
}
#intro nav {
width: 100%;
}
#main article {
width: 100%;
height: 420px;
}
#footer {
padding: 30px 0;
}
#footer .copyright,
#footer .address {
width: 100%;
}
#footer .copyright p,
#footer .address p {
text-align: center;
padding: 0;
}
#footer .address p {
padding-top: 20px;
}
}
이것은 내가 프로젝트하면서 믹스인 사용할 때 썼던 것임. 엘리스에서는 scss 안 배움
/*반응형 화면 크기 변수*/
$breakpoint-mobile: 335px;
$breakpoint-tablet: 758px;
$breakpoint-desktop: 1024px;
@mixin mobile {
@media (min-width: #{$breakpoint-mobile}) and (max-width: #{$breakpoint-tablet - 1px}) {
@content;
}
}
@mixin tablet {
@media (min-width: #{$breakpoint-tablet}) and (max-width: #{$breakpoint-desktop - 1px}) {
@content;
}
}
@mixin desktop {
@media (min-width: #{$breakpoint-desktop}) {
@content;
}
}
@import "**_common.scss**";
@include mobile {
.img-card {
width: 100px;
}
}
@include tablet {
.img-card {
width: 200px;
}
}
@include desktop {
.img-card {
width: 300px;
}
}