使用纯CSS实现3D旋转效果的示例代码

Kefira ·
更新时间:2024-09-21
· 882 次阅读

主要使用CSS中的 preserve-3d 、perspective 属性实现3D效果

效果

HTML代码

<body> <div class="box"> <div class="face front"> <h2>前面</h2> </div> <div class="face back"> <h2>背面</h2> </div> </div> </body>

为了演示效果,将元素居中,body的css设置

*{ margin: 0; padding: 0; } body{ width: 100%; height: 100vh; display: flex; justify-content: center; align-items: center; background: #333; }

box 属性设置

.box{ width: 300px; height: 400px; transform-style: preserve-3d; position: relative; } .face{ position: absolute; left: 0; top: 0; width: 100%; height: 100%; transform-style: preserve-3d; display: flex; justify-content: center; align-items: center; backface-visibility: hidden; transition: 2s ease-in-out; transform: perspective(500px) rotateY(0deg); }

前面属性设置

.face.front{ background: #ff0; }

背面属性设置,在Y轴旋转 180度,先不显示

.face.back{ background: #3bc2ff; color: #fff; transform: perspective(500px) rotateY(180deg); }

设置悬浮的动画效果

.box:hover .face.front{ transform: perspective(500px) rotateY(180deg); } .box:hover .face.back{ transform: perspective(500px) rotateY(360deg); }

设置文字的悬浮效果

.box .face h2{ font-size: 4em; text-transform: uppercase; transform: perspective(500px) translateZ(50px); }



3d旋转 3d CSS 示例

需要 登录 后方可回复, 如果你还没有账号请 注册新账号