Giant Danio Fish
본문 바로가기
CSS

CSS 애니메이션 - 3D cube

by 코딩왕자 2022. 9. 22.

CSS 애니메이션 연습하기

큐브를 만들어서 3d효과로 입체적으로 만들어보겠습니다.


결과

HTML

정육면체를 만들기 위해서 div박스를 6개 만들어줍니다.

<div class="cube">
    <div>북극꼼</div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>

CSS

배경색을 넣고 z축까지 위치를 설정해서 정육면체를 만들어줍니다. 그리고 각각의 면에 애니메이션을 각각 줘서 위 아래로 커졌다가 작아지는 애니메이션을 만들어줍니다.

body {
    background-color: gray;
    height: 100vh;
    perspective: 1000px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.cube {
    position: relative;
    width: 100px;
    height: 100px;
    transform-style: preserve-3d;
    transform: rotatex(-20deg) rotatey(-140deg) translatez(0);
    animation: rotate 8000ms linear infinite;
}
.cube div {
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 0;
    left: 0;
    width: 100px;
    height: 100px;
    opacity: 0.75;
    color: #fff;
}
.cube div:nth-child(1) {
    background-color: #ffcc80;
    transform-origin: center top;
    transform: rotatex(90deg) translatey(-100px);
    animation: top 4000ms ease-in-out 8000ms infinite;
}
.cube div:nth-child(2) {
    background-color: #ffb74d;
    transform-origin: center bottom;
    transform: rotatex(-90deg) translatey(100px);
    animation: bottom 4000ms ease-in-out 8000ms infinite;
}
.cube div:nth-child(3) {
    background-color: #ffa726;
    transform-origin: left center;
    transform: rotatey(-90deg) translatex(-100px);
    animation: left 4000ms ease-in-out 8000ms infinite;
}
.cube div:nth-child(4) {
    background-color: #ff9800;
    transform-origin: right center;
    transform: rotatey(90deg) translatex(100px);
    animation: right 4000ms ease-in-out 8000ms infinite;
}
.cube div:nth-child(5) {
    background-color: #fb8c00;
    transform-origin: center center;
    transform: rotatex(0deg);
    animation: front 4000ms ease-in-out 8000ms infinite;
}
.cube div:nth-child(6) {
    background-color: #ffb74d;
    transform-origin: center center;
    transform: rotatey(180deg) translatez(100px);
    animation: back 4000ms ease-in-out 8000ms infinite;
}  
@keyframes rotate {
    0% {transform: rotatex(0) rotatey(0) rotatez(0) translatez(0)}
    100% {transform: rotatex(360deg) rotatey(360deg) rotatez(360deg) translatez(0)}
}
@keyframes top {
    0% {transform: rotatex(90deg) translatey(-100px) translatez(0)}
    50% {transform: rotatex(90deg) translatey(-100px) translatez(100px)}
    100% {transform: rotatex(90deg) translatey(-100px) translatez(0)}
}
@keyframes bottom {
    0% {transform: rotatex(-90deg) translatey(100px) translatez(0)}
    50% {transform: rotatex(-90deg) translatey(100px) translatez(100px)}
    100% {transform: rotatex(-90deg) translatey(100px) translatez(0)}
}
 
@keyframes right {
    0% {transform: rotatey(90deg) translatex(100px) scaley(1)}
    50% {transform: rotatey(90deg) translatex(100px) scaley(3)}
    100% {transform: rotatey(90deg) translatex(100px) scaley(1)}
}
@keyframes left {
    0% {transform: rotatey(-90deg) translatex(-100px) scaley(1)}
    50% {transform: rotatey(-90deg) translatex(-100px) scaley(3)}
    100% {transform: rotatey(-90deg) translatex(-100px) scaley(1)}
}
@keyframes front {
    0% {transform: rotatex(0deg) scaley(1)}
    50% {transform: rotatex(0deg) scaley(3)}
    100% {transform: rotatex(0deg) scaley(1)}
}
@keyframes back {
    0% {transform: rotatex(180deg) translatez(100px) scaley(1)}
    50% {transform: rotatex(180deg) translatez(100px) scaley(3)}
    100% {transform: rotatex(180deg) translatez(100px) scaley(1)}
}

댓글


광고 준비중입니다