CSS3实现旋转的魔法盒子

在学习CSS3的强大动画效果的时候被深深震撼,在B站观看学习视频并且仿照着写了一个页面,效果和代码如下:

CSS3实现旋转的魔法盒子_第1张图片

代码如下html:




    
    
    
    
    to your girl friend


    

css代码如下:

*{
    margin: 0;
    padding: 0;
}

html,body{
    width: 100%;
    height: 100%;
    background-color: pink;
}

.container{
    width: 400px;
    height: 440px;
    margin: 200px auto;
    /* flat	表示所有子元素在2D平面呈现;  preserve-3d 表示所有子元素在3D空间呈现*/
    transform-style: preserve-3d;       
    animation: rotate 8s linear infinite;
}

.container>.inner-box, .container>.outer-box{
    transform-style: preserve-3d;
}

.container div{
    position: absolute;
    transition: all .4s;
}

.outer-box div{
    position: absolute;
    width: 300px;
    height: 300px;
    opacity: 0.9;
    top: 200px;
    left: 0px;

}

.inner-box div{
    width: 200px;
    height: 200px;
    opacity: 0.9;
    top: 250px;
    left: 50px;
}

/* outer box*/
.outer-box>.front{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: translateZ(150px);
}

.outer-box>.back{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: translateZ(-150px);
}

.outer-box>.left{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: rotateY(90deg) translateZ(150px);
}

.outer-box>.right{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: rotateY(-90deg) translateZ(150px);
}

.outer-box>.up{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: rotateX(-90deg) translateZ(-150px);
}

.outer-box>.down{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: rotateX(-90deg) translateZ(150px);
}

/*inner box*/
.inner-box>.front{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: translateZ(100px);
}

.inner-box>.back{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: translateZ(-100px);
}

.inner-box>.left{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: rotateY(90deg) translateZ(100px);
}

.inner-box>.right{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: rotateY(-90deg) translateZ(100px);
}

.inner-box>.up{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: rotateX(-90deg) translateZ(-100px);
}

.inner-box>.down{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: rotateX(-90deg) translateZ(100px);
}

/* when you put the cursor on*/
.container:hover{
    cursor: pointer;
}

.container:hover .outer-box>.front{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: translateZ(200px);
}

.container:hover .outer-box>.back{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: translateZ(-200px);
}

.container:hover .outer-box>.left{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: rotateY(90deg) translateZ(200px);
}

.container:hover .outer-box>.right{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: rotateY(-90deg) translateZ(200px);
}

.container:hover .outer-box>.up{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: rotateX(-90deg) translateZ(-200px);
}

.container:hover .outer-box>.down{
    background-image: url("../images/img1.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    transform: rotateX(-90deg) translateZ(200px);
}



@keyframes rotate{
    from{
        transform-origin:center center;
        transform: rotateX(0deg) rotateY(0deg);
    }
    to {
        transform-origin:center center;
        transform: rotateX(360deg) rotateY(360deg);
    }
}

 

你可能感兴趣的:(重学前端----css篇)