基于CSS3制作专属可自由旋转的立方体

一、代码区域

1.1 css3代码区域

css 复制代码
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        /* 1) 定义动画 */
        @keyframes loop {
            0% {
                transform: rotateX(348deg) rotateY(67deg) rotateZ(95deg);
            }

            50% {
                transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
            }


            100% {
                transform: rotateX(348deg) rotateY(67deg) rotateZ(95deg);
            }
        }

        ul {
            /* 父元素 */
            width: 200px;
            height: 200px;
            /* background-color: #ccc; */
            margin: 200px auto;
            position: relative;
            /* 设置成3d容器 */
            transform-style: preserve-3d;
            /* 设置角度旋转 */
            /* transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg); */
            transform: rotateX(348deg) rotateY(67deg) rotateZ(95deg);

            /* 2) 使用动画 */
            animation: loop 8s linear infinite;
        }

        ul li {
            width: 200px;
            height: 200px;
            position: absolute;
            top: 0;
            left: 0;
            text-align: center;
            line-height: 200px;
            font-size: 20px;
            user-select: none;
        }

        ul li:nth-child(1) {
            background-color: rgba(255, 0, 0, .5);
            /* 围绕X旋转90° 再位移盒子尺寸的一半 */
            transform: rotateX(90deg) translateZ(100px);
        }

        ul li:nth-child(2) {
            background-color: rgba(255, 255, 0, .5);
            transform: rotateX(90deg) translateZ(-100px);
        }

        ul li:nth-child(3) {
            background-color: rgba(255, 0, 255, .5);
            transform: rotateY(90deg) translateZ(100px);
        }

        ul li:nth-child(4) {
            background-color: rgba(0, 255, 0, .5);
            transform: rotateY(90deg) translateZ(-100px);
        }

        ul li:nth-child(5) {
            background-color: rgba(0, 0, 255, .5);
            transform: translateZ(100px);
        }

        ul li:nth-child(6) {
            background-color: rgba(0, 255, 255, .5);
            transform: translateZ(-100px);
        }
    </style>

1.2 html代码区域

html 复制代码
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
    </ul>

二、代码效果展示

三、个人总结:

该功能的实现主要利用了css3中的动画,过渡属性,转换属性以及类名选择器,其中旋转的参数大家可自行修改,便于更好地理解该功能的实现。

注: 博主每天记录自己所学,如有写的不好之处,希望您能不吝赐教,给我一些关于这个项目的意见和建议。各位的宝贵意见将对我产生深远的影响,我将认真倾听并尽力改进。谢谢各位~~

相关推荐
万少4 小时前
HarmonyOS官方模板集成创新活动-流蓝卡片
前端·harmonyos
-To be number.wan6 小时前
C++ 赋值运算符重载:深拷贝 vs 浅拷贝的生死线!
前端·c++
噢,我明白了6 小时前
JavaScript 中处理时间格式的核心方式
前端·javascript
纸上的彩虹7 小时前
半年一百个页面,重构系统也重构了我对前端工作的理解
前端·程序员·架构
be or not to be8 小时前
深入理解 CSS 浮动布局(float)
前端·css
LYFlied8 小时前
【每日算法】LeetCode 1143. 最长公共子序列
前端·算法·leetcode·职场和发展·动态规划
老华带你飞8 小时前
农产品销售管理|基于java + vue农产品销售管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
小徐_23338 小时前
2025 前端开源三年,npm 发包卡我半天
前端·npm·github
GIS之路9 小时前
GIS 数据转换:使用 GDAL 将 Shp 转换为 GeoJSON 数据
前端
JIngJaneIL9 小时前
基于springboot + vue房屋租赁管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端