Giant Danio Fish
본문 바로가기
CSS

CSS 애니메이션 - 마우스 오버효과(3D첨가)

by 코딩왕자 2022. 9. 20.

CSS 애니메이션 연습하기

마우스 오버 효과에 3D를 간단하게 첨가해서 멋진 애니메이션 효과를 만들어 보겠습니다.


결과

HTML

사진4장을 각각 두장씩 한 태그로 묶어서 만들어줍니다.

<div class="hover__wrap">
    <div class="hover__updown">
        <figure class="front">
            <img src="https://github.com/younghwan12/coding3/blob/main/animation/img/homeup.jpg?raw=true" alt="계란 후라이">
            <figcaption>
            <h3>Mouse Hover Effect</h3>
            <p>마우스 올리면 Up</p>
            </figcaption>
        </figure>
        <figure class="back">
            <img src="https://github.com/younghwan12/coding3/blob/main/animation/img/homedown.jpg?raw=true" alt="깨진 계란">
            <figcaption>
            <h3>Mouse Hover Effect</h3>
            <p>마우스 내리면 Down</p>
            </figcaption>
        </figure>
        </div>
        <div class="hover__leftright">
        <figure class="front">
            <img src="https://github.com/younghwan12/coding3/blob/main/animation/img/homeright.jpg?raw=true" alt="깨진 계란">
            <figcaption>
            <h3>Mouse Hover Effect</h3>
            <p>마우스 올리면 to Right</p>
            </figcaption>
        </figure>
        <figure class="back">
            <img src="https://github.com/younghwan12/coding3/blob/main/animation/img/homeleft.jpg?raw=true
    " alt="깨진 계란">
            <figcaption>
            <h3>Mouse Hover Effect</h3>
            <p>마우스 내리면 to Left</p>
            </figcaption>
        </figure>
        </div>
</div>

CSS

사진 두장을 앞뒤로 겹치고 뒤집었을때 뒷면을 안보이게 설정합니다.

@font-face {
    font-family: 'LocusSangsang';
    font-weight: normal;
    font-style: normal;
    src: url('https://cdn.jsdelivr.net/gh/webfontworld/locus/LocusSangsang.eot');
    src: url('https://cdn.jsdelivr.net/gh/webfontworld/locus/LocusSangsang.eot?#iefix') format('embedded-opentype'),
         url('https://cdn.jsdelivr.net/gh/webfontworld/locus/LocusSangsang.woff2') format('woff2'),
         url('https://cdn.jsdelivr.net/gh/webfontworld/locus/LocusSangsang.woff') format('woff'),
         url('https://cdn.jsdelivr.net/gh/webfontworld/locus/LocusSangsang.ttf') format("truetype");
    font-display: swap;
}
body {
    font-family: 'LocusSangsang';
    background-image: linear-gradient(135deg, yellowgreen 0%, blueviolet 40%, gold 100%);
    height: 100vh;
}

.hover__wrap {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}
.hover__wrap > div {
    max-width: 400px;
    margin: 3%;
    position: relative;
    perspective: 1000px;
}
.hover__wrap > div img {
    width: 100%;
    border: 10px solid #F0F8FF; 
    box-shadow: 2px 2px 2px 2px rgba(0,0,0,0.2);
    box-sizing: border-box;
    vertical-align: top;
}
.hover__wrap > div .front {
    transition: transform 1s;
    backface-visibility: hidden;
    transform-style: preserve-3d;
}
.hover__wrap > div .back {
    position: absolute;
    left: 0;
    top: 0;
    z-index: -1;
    transition: transform 1s;
    transform-style: preserve-3d;
}
.hover__wrap > div .right {
    position: absolute;
    left: 0;
    top: 0;
    z-index: -1;
}
.hover__wrap > div figcaption {
    background: rgba(0,0,0,0.2);
    color: #f8f8ff;
    padding: 10px;
    text-align: center;
    line-height: 1.5;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%) translatez(100px);
    width: 60%;
    backface-visibility: hidden;
}

/* mouse hover effect */
.hover__updown .front {
    transform: rotateX(0deg);
}
.hover__updown:hover .front {
    transform: rotateX(180deg);
}
.hover__updown .back {
    transform: rotateX(-180deg);
}   
.hover__updown:hover .back {
    transform: rotateX(0deg);
}

.hover__leftright .front {
    transform: rotateY(0deg);
}
.hover__leftright:hover .front {
    transform: rotateY(180deg);
}
.hover__leftright .back {
    transform: rotateY(-180deg);
}
.hover__leftright:hover .back {
    transform: rotateY(0deg);
}

새로배운 코드

backface-visibility: hidden;       //뒷면은 안보이게 한다.
perspective: 1000px;        //원근법 설정 숫자가 작을수록 원근법효과가 커진다.  
transform: translatez(100px);      //Z축으로 100px 떨어트린다(3D)

댓글


광고 준비중입니다