pink老师的粉丝
css3d动画之3D呈现:transform-style: preserve-3d;代码行不起作用,可能的原因是没有添加 backface-visibility: hidden;第一个盒子的背面隐藏,如果不添加背面隐藏,那么盒子旋转180deg之后会呈现pink盒子的背面,不会呈现紫色盒子。
关于backface-visibility属性的使用方法参考w3school链接:
https://www.w3school.com.cn/cssref/pr_backface-visibility.asp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>两面翻转的盒子</title>
<style>
body {
perspective: 400px;
}
.box {
position: relative;
width: 200px;
height: 200px;
margin: 100px auto;
/* 呈现3d效果,给父元素添加 */
transform-style: preserve-3d;
/* 谁过渡给谁加 */
transition: all 2s;
}
.box:hover {
transform: rotateY(180deg);
}
.front,
.back {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 50%;
font-size: 20px;
color: #fff;
text-align: center;
line-height: 200px;
}
.front {
background-color: pink;
/* 添加背面隐藏 */
backface-visibility: hidden;
z-index: 1;
}
.back {
background-color: blueviolet;
transform: rotateY(180deg);
}
</style>
</head>
<body>
<div class="box">
<div class="front">pink老师等你</div>
<div class="back">黑马程序员我来了</div>
</div>
</body>
</html>
参考效果如下