css3d制作正方体

使用css3d技术 ,制作一个可以动态动画的正方体模型
效果图:

代码如下:

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <style>
        /* 设置高度宽度100%并且左右居中、上下居中 */
        html,
        body {
            width: 100%;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
        }
/* 设置单面的高宽 */
        .box {
            width: 50px;
            height: 50px;
            /* 3D视距 */
            perspective: 1000px;
        }
/* 设置面里的字体大小居中、绝对定位 */
        .box div {
            font-size: 20px;
            text-align: center;
            width: 100%;
            height: 100%;
            position: absolute;
        }
/* 设置3D动画以及特效旋转 */
        main {
            width: 100%;
            height: 100%;
            transform-style: preserve-3d;
            /* 第一个参数是关键帧的名字、第二个参数是动画的时长、第三个参数是动画从头到尾的速度始终一致、第四个参数是播放的次数infinite无限次 */
            animation: rotate 10s linear infinite;
        }

        .front {
            background: rgba(100, 0, 100, 0.6);
            /* 将div沿Z轴向前移动50px */
            transform: translateZ(25px);
        }

        .back {
            background: rgba(0, 100, 100, 0.5);
            /* 将div沿Z轴向后移动50px */
            transform: translateZ(-25px);
        }

        .left {
            background: rgba(100, 1000, 100, 0.5);
             /* 将div沿Y轴旋转90度,Z轴向后移动50px */
            transform: rotateY(90deg) translateZ(-25px);
        }

        .right {
            background: rgba(1000, 100, 100, 0.5);
            /* 将div沿Y轴旋转90度,Z轴向前移动50px */
            transform: rotateY(90deg) translateZ(25px);
        }

        .top {
            background: rgba(1000, 0, 0, 0.5);
            /* 将div沿X轴旋转90度,Z轴向前移动50px */
            transform: rotateX(90deg) translateZ(25px);
        }

        .bottom {
            background: rgba(0, 0, 1000, 0.5);
            /* 将div沿X轴旋转90度,Z轴向后移动50px */
            transform: rotateX(90deg) translateZ(-25px);
        } 

        @keyframes rotate {
            from {
                /* 动画起始位置 */
                transform: rotateX(0) rotateY(0);
            }

            to {
                /* 动画终点位置 */
                transform: rotateX(360deg) rotateY(360deg);
            }
        }
    </style>
</head>

<body>
    <div class="box">
        <main>
            <div class="front">前</div>
            <div class="back">后</div>
            <div class="left">左</div>
            <div class="right">右</div>
            <div class="top">上</div>
            <div class="bottom">下</div>
            <main>
    </div>
</body>

</html>
相关推荐
Fluxart.ai11 小时前
电商商品图审核怎么自动化?规则引擎、人工复核与发布门禁
java·前端·自动化
why技术12 小时前
AI 写的文章,可能都带着手敲一遍都去不掉的“隐形水印”。
前端·人工智能·后端
愚公搬代码12 小时前
【愚公系列】《Android应用案例开发大全》016-LBS类应用掌上杭州(辅助工具类的开发)
android·前端
CodeSheep12 小时前
又一个华为天才少年,离职了!
前端·后端·程序员
kyriewen12 小时前
面试官说"打开你的AI工具"——我才发现,他考的根本不是写代码
前端·人工智能·面试
IT_陈寒13 小时前
Vue的双向绑定把我坑惨了,原来这个场景不能用
前端·人工智能·后端
hunterandroid14 小时前
[Android 从零到一] Compose LazyColumn 性能优化:key、稳定性与重组治理
android·前端
lichenyang45314 小时前
从一次团队邀请开始:用 React、NestJS 与 Socket.IO 做可靠的实时通知
前端
vtian14 小时前
一篇文章吃透 Monorepo:pnpm + Turborepo + Changesets 全流程实战(含 8 个踩坑)
前端
默_笙14 小时前
❗ 点击计数按钮,为什么"峨眉队"也跟着重新渲染?React.memo 说:我记住了
前端·javascript