利用css制作“旋转正方体”

任务

首先制作一个正方体,然后当鼠标悬停在正方体上时,正方体旋转。

效果图

思路

正方体是由六个面组成,因此我们可以首先将六个相同大小的面叠在一起,然后通过对这几个面经过不同的旋转和位移,组成一个正方体,再添加动画效果。

xml 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>01.html</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 200px;
            height: 200px;
            margin: 100px auto;
            position: relative;
            /* 3D效果实现 */
            perspective: 300px;
        }
        /* 让这六个面叠加在一起,都在div中 */
        div p{
            width: 200px;
            height: 200px;
            position: absolute;
            top: 0;
            left: 0;
        }
        div p:nth-child(1){
            /* 前 */
            background-color: rgba(255, 0, 0, 0.5);
            transform: translateZ(100px);
        }
        div p:nth-child(2){
            /* 后 */
            background-color: rgba(167, 154, 154, 0.5);
            transform: translateZ(-100px);

        }
        div p:nth-child(3){
            /* 左 */
            background-color: rgba(208, 255, 0, 0.5);
            transform: rotateY(90deg) translateZ(-100px);
        }
        div p:nth-child(4){
            /* 右 */
            background-color: rgba(0, 255, 0, 0.5);
            transform: rotateY(-90deg) translateZ(-100px);
        }
        div p:nth-child(5){
            /* 上 */
            background-color: rgba(0, 255, 200, 0.5);
            transform: rotateX(90deg) translateZ(100px);
        }
        div p:nth-child(6){
            /* 下 */
            background-color: rgba(0, 140, 255, 0.5);
            transform: rotateX(90deg) translateZ(-100px);
        }
    </style>
</head>

<body>
    <div>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
    </div>
</body>

</html>

此时效果图如下

然后在添加动画效果

xml 复制代码
<body>
    <!-- 再加一个舞台,让整个box盒子带着里面的p旋转 -->
    <section>
        <div>
            <p></p>
            <p></p>
            <p></p>
            <p></p>
            <p></p>
            <p></p>
        </div>
    </section>
</body>
xml 复制代码
<style>
        *{
            margin: 0;
            padding: 0;
        }
        section{
            width: 200px;
            height: 200px;
            margin: 100px auto;
            /* 调整效果显的更加正方体 */
            perspective: 10000px;
        }
        /* 即是section的演员,又是p的舞台 */
        div{
            width: 200px;   
            height: 200px;
            /* 调整效果显的更加正方体 */
            perspective: 10000px;
            position: relative;
            /* 设置变形类型,保留它内部的3D效果 */
            /* 这个盒子又是舞台,又是演员,这个box整体带着里面的p旋转 */
            transform-style: preserve-3d;
            
            transition: transform 5s linear 0s;
        }
        section:hover div{
            transform: rotateX(360deg) rotateY(360deg);
        }
        ... ...
</style>

此时满足效果图要求

相关推荐
一只爱打拳的程序猿40 分钟前
【SpringBoot】实现登录功能
javascript·css·spring boot·mybatis·html5
程序员buddha2 小时前
ThinkPHP8.0+MySQL8.0搭建简单实用电子证书查询系统
javascript·css·mysql·php·layui·jquery·html5
高志小鹏鹏3 小时前
Tailwind CSS都更新到4.0了,你还在抵触吗?
前端·css·postcss
Mswanga6 小时前
探秘 CSS 盒子模型:构建网页布局的基石
前端·css
I will.8746 小时前
如何使用 CSS 实现黑色遮罩效果
前端·javascript·css
前端Hardy8 小时前
HTML&CSS :用 CSS 遮罩,让产品展示图 “高级感爆棚”
css·html
前端Hardy8 小时前
HTML&CSS :1 分钟学会,飞虫变色特效卡片
css·html
前端Hardy8 小时前
HTML&CSS&JS:必学!用动态导航栏,让网页颜值飙升 10 倍
css·html
王哈哈嘻嘻噜噜9 小时前
CSS简介以及导入形式
前端·css
祈澈菇凉9 小时前
如何在 React 中使用 CSS-in-JS?
javascript·css·react.js