622 字
3 分钟

3D盒子翻转案例练习

欢迎来到我的博客!#

使用 Markdown 格式编写。

今天学到了什么#

完成了一个内容凸起的盒子翻转案例,主要有一个要点就是父盒子不要写 overflow: hidden;

裁剪发生在父元素的可视区域:子元素做了 3D 投影后,仍会被最近的 overflow 非 visible 的祖先按投影后的轮廓裁掉。也就是说,文字即使沿 Z 轴前移,超过父元素平面边缘时会被裁剪,失去“探出边缘”的视觉线索。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(to bottom, skyblue, purple);
}
.card {
width: 300px;
height: 400px;
position: relative;
/* perspective: 500px; */
}
.card .front::before {
content: "";
position: absolute;
width: 100%;
height: 80px;
top: 55%;
left: 0;
backface-visibility: hidden;
background-color: rgba(0, 0, 0, 0.45);
transform: translate3d(0%, -50%, 20px);
}
.card .front,
.card .back {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
backface-visibility: hidden;
border-radius: 15px;
/* overflow: hidden; */
/* 裁剪发生在父元素的可视区域:子元素做了 3D 投影后,仍会被最近的 overflow 非 visible 的祖先按投影后的轮廓裁掉。也就是说,文字即使沿 Z 轴前移,超过父元素平面边缘时会被裁剪,失去“探出边缘”的视觉线索。 */
/* 开启3D空间 */
transform-style: preserve-3d;
transition: all 3s ease;
/* perspective: 500px; */
}
.card .front {
background: url(../素材/练习案例/05-王者翻转框/封面.png);
background-size: cover;
z-index: 2;
}
.card .front p {
font-size: 15px;
position: absolute;
width: 100%;
top: 55%;
/* left: 50%; */
text-align: center;
transform: translate3d(0%, -50%, 60px);
color: #fff;
backface-visibility: hidden;
}
.card:hover .front {
transform: rotateY(-180deg);
}
.card .back {
background: #191e25;
color: #fff;
transform: rotateY(180deg);
padding: 20px;
}
.card .back .hero {
width: 250px;
border-bottom: 2px solid #b49758;
color: #fff;
text-align: left;
padding-bottom: 15px;
}
.card .back .hero h2 {
font-size: 20px;
/* 取消加粗 */
font-weight: normal;
margin-bottom: 10px;
}
.card .back .hero span {
background-color: #c09f5a;
padding: 2px 5px;
border-radius: 2px;
color: #191d20;
font-weight: bold;
font-size: 12px;
}
.card .back .desc {
text-indent: 2em;
font-size: 18px;
line-height: 30px;
padding: 15px 5px;
text-align: left;
letter-spacing: 2px;
}
.card:hover .back {
transform: rotateY(0deg);
}
.threeD {
transform: translateZ(60px);
}
</style>
</head>
<body>
<div class="card">
<div class="front">
<p>快来和妲己一起玩耍吧~</p>
</div>
<div class="back">
<div class="hero threeD">
<h2>妲己</h2>
<div>
<span>法师</span>
<span>刺客</span>
<span>辅助</span>
</div>
</div>
<div class="desc threeD">
妲己,原型是商纣王宠妃妲己。职业法师,拥有先手控制敌人,技能范围广、射程够长,而且在后期超高爆发的特长。主流玩法是蹲草丛,打完就跑,二技能先手控制,然后接大招,最后一技能伤害最大化。
</div>
</div>
</div>
</body>
</html>

赞助支持

如果这篇文章对你有帮助,欢迎赞助支持!

赞助
最后更新于 2025-12-17,距今已过 18 天

部分内容可能已过时

目录