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>
参考效果如下


本文通过一个实例展示了如何使用CSS3的transform-style: preserve-3d和backface-visibility属性创建一个3D翻转的动画效果。当不设置backface-visibility:hidden时,翻转后的盒子会显示pink老师的背面,而非预期的紫色盒子。通过添加backface-visibility:hidden,可以确保在翻转过程中只显示正面。详细代码和解释帮助理解这两个关键属性的工作原理。

2万+

被折叠的 条评论
为什么被折叠?



