用3d实现立方体效果

这段代码展示了如何使用HTML、CSS实现一个3D立体旋转的盒子效果,包括不同颜色的子元素在鼠标悬停时的旋转和景深变换。通过CSS的transform属性、perspective属性和transition属性,创建了平滑的3D转换和动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 300px;
            height: 300px;
            margin: 100px auto;
            border: 1px solid #000;
            position: relative;
            /* 景深效果 */
            perspective: 12000px;
            /* 设置屏幕观察点 */
            perspective-origin: 50% 50%;
            /* 3d效果 */
            transform-style: preserve-3d;
            /* 过渡 */
            transition: all 5s;
        }

        .box>div {
            width: 100px;
            height: 100px;
            position: absolute;
            /* 背景透明度 */
            opacity: 0.5;
            font-weight: 900;
            font-size: 50px;
            line-height: 100px;
            text-align: center;
        }

        .box1 {
            left: 0;
            top: 100px;
            background-color: #f00;
            transform-origin: right;
            transform: rotateY(90deg);
        }

        .box2 {
            left: 100px;
            top: 0;
            background-color: yellow;
            transform-origin: bottom;
            transform: rotateX(-90deg);
        }

        .box3 {
            left: 100px;
            top: 100px;
            background-color: blue;
        }

        .box4 {
            left: 100px;
            top: 100px;
            background-color: green;
            transform: translateZ(100px);
        }

        .box5 {
            left: 100px;
            top: 200px;
            background-color: aqua;
            transform-origin: top;
            transform: rotateX(90deg);

        }

        .box6 {
            left: 200px;
            top: 100px;
            background-color: purple;
            transform-origin: left;
            transform: rotateY(-90deg);
        }

        .box:hover {
            transform: rotateX(180deg) rotateY(360deg);
            /* transform: rotateY(180deg); */
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
        <div class="box4"></div>
        <div class="box5"></div>
        <div class="box6"></div>
    </div>

</body>

</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值