利用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>

此时满足效果图要求

相关推荐
yddddddy2 小时前
css的基本知识
前端·css
昔人'2 小时前
css `lh`单位
前端·css
2501_918126915 小时前
用html5写一个flappybird游戏
css·游戏·html5
孩子 你要相信光5 小时前
css之一个元素可以同时应用多个动画效果
前端·css
小刘鸭地下城8 小时前
优雅表格设计:CSS 美化技巧详解
css
小刘鸭地下城8 小时前
网页深色模式完整实现:从响应式设计到系统主题联动
css
恶猫10 小时前
javascript文本长度检测与自动截取,用于标题长度检测
javascript·css·css3·js·自动检测·文本长度
Hilaku12 小时前
我为什么认为 CSS-in-JS 是一个失败的技术?
前端·css·前端框架
Giant10012 小时前
0 基础也能懂的 Flex 布局教程:3 步搞定网页排版
css
Sherry00712 小时前
【译】掌握 Flexbox 的终极指南:从烤肉串到鸡尾酒香肠的布局哲学
css·面试·flexbox