重点:1.transform-style: preserve-3d; /*让转换的子元素保留3D转换*/
2.反面的图片需要给它先旋转180度,不然动画的时候,就是反的。
.container {
position: relative;
width: 300px;
height: 300px;
perspective: 300px;
transition: transform .6s ease; /*过渡*/
transform-style: preserve-3d; /*让转换的子元素保留3D转换*/
}
.container .item {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
.container .front {
transform: rotateY(0deg);
z-index: 2;
}
.container .back {
transform: rotateY(180deg);
z-index: 1;
}
/*旋转动画*/
.container:hover {
transform: rotateY(-180deg);
}
html
<div class="container">
<div class="item front " style="background-color: red;">正面</div>
<div class="item back" style="background-color: blue;">反面</div>
</div>