使用css3属性做3D导航栏

280 阅读1分钟

1.首先是我们的css样式

  <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        ul {
            margin: 100px;
        }
        
        ul li {
            display: inline-block;
            width: 120px;
            height: 35px;
            text-align: center;
            line-height: 35px;
            list-style: none;
            perspective: 500px;
        }
        
        .box {
            position: relative;
            width: 100%;
            height: 100%;
            transform-style: preserve-3d;
            transition: all 1s;
        }
        
        .box:hover {
            transform: rotateX(90deg);
        }
        
        .front,
        .bottom {
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
        }
        
        .front {
            background-color: pink;
            z-index: 1;
            transform: translateZ(17.5px);
        }
        
        .bottom {
            background-color: purple;
            /* 我们如果有移动 或者其他样式,必须先写我们的移动 */
            /* 这个x轴一定是负值 因为它要向前翻 不是向后仰 */
            transform: translateY(17.5px) rotateX(-90deg);
        }
    </style>

2.再是我们的html

<body>
    <ul>
        <li>
            <div class="box">
                <div class="front">我是3D的导航栏</div>
                <div class="bottom">132</div>
            </div>
        </li>
    </ul>
</body>

因为不知道如何上传视频,只能用图片进行显示
刚刚开始时: image.png

正在旋转时: image.png

旋转完后: image.png