No.4 - 3D 空间的卡片翻转动效

 

html

<div class="container">
    <img src="img/4-2.jpg" alt="" class="up">
    <img src="img/4-1.jpg" alt="" class="down">
</div>

css

html,body{
     height: 100%;
}
.container{
    position: relative;
    width: 250px;
    height: 350px;
    top:50%;
    left:50%;
    margin-left:-125px;
    margin-top: -175px;
    cursor: pointer;
}
.container img{
    position: absolute;
    top:0;
    left:0;
    width: 250px;
    height: 350px;
}

首先给容器添加 perspective

.container{
    perspective: 1000;
}

perspective 属性定义 3D 元素距视图的距离,以像素计。该属性允许您改变 3D 元素查看 3D 元素的视图。 当为元素定义 perspective 属性时,其子元素会获得透视效果,而不是元素本身。 注释:perspective 属性只影响 3D 转换元素。

 

给每个图片添加

.container img{
    transform-style: preserve-3d;
    transition: all 1s ease-in-out; 
}  

.down的图片初始化的时候应该是它的镜像,即左右颠倒

.container .down{
    transform: rotateY(-180deg);
}

此时会发现,下面的图片遮挡住了上方那一张,所以

.container img{
    backface-visibility: hidden; 
} 

backface-visibility 属性定义当元素不面向屏幕时是否可见。 如果在旋转元素不希望看到其背面时,该属性很有用。

最后

.container:hover .down{
    transform: rotateY(0deg); 
}

.container:hover .up{
    transform: rotateY(-180deg);
}

 

posted @ 2019-07-01 11:28  里德曼有驴耳朵  阅读(333)  评论(0编辑  收藏  举报