纯css3来实现3d旋转正方体的效果

在这里插入图片描述

  <style>
        :root, body{
            height: 100%;
        }
        body{
            perspective: 2000px; //景深越大,距离屏幕越远
            transform-style: preserve-3d;
        }
        @keyframes round{
            0%{
                transform: translate(-50% ,-50%) rotate3d(1,1,2,0deg)
            }
    
            100%{
                transform: translate(-50% ,-50%) rotate3d(1,1,2,360deg)   
            }
        }
        .wrapper{
            width: 200px;
            height: 200px;
            position: absolute;
            top:calc(50%);
            left:calc(50%);
            transform:  translate(-50% ,-50%)  ;
            transform-style: preserve-3d;
            animation: round 4s linear infinite;
            /* backface-visibility: hidden; */  //决定背面是否显示
        }
        .wrapper div{
            width: 200px;
            height: 200px;
            font-size: 20px;
            text-align: center;
            line-height: 200px;
            border: 1px solid black;
            box-shadow: 2px 2px 3px yellow,
                    inset 5px 5px 5px #ff4400;
            position: absolute;
      
        }
        .wrapper div:nth-of-type(1){
            transform: rotatey(0deg) translatez(100px) ;
            background-color: rgba(0, 0,200, 0.5); 
        }
        .wrapper div:nth-of-type(2){
            transform: rotateY(90deg) translatez(100px); 
            background-color: rgba(255, 0,255, 0.5)
        }
        .wrapper div:nth-of-type(3){
            transform: rotateY(90deg) translatez(-100px);
            background-color: rgba(0, 100,0, 0.5)
        }
        .wrapper div:nth-of-type(4){
            transform: rotatex(90deg) translatez(100px);
            background-color: rgba(255,,0, 0.5)
        }
        .wrapper div:nth-of-type(5){
            transform: rotatex(90deg) translatez(-100px);
            background-color: rgba(100, 200,0, 0.5)
        }
        .wrapper div:nth-of-type(6){
            transform: rotateX(0deg) translatez(-100px);
            background-color: rgba(0, 200,100, 0.5)
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
    </div>
    <script>
        // var wrapper = document.getElementsByClassName('wrapper')[0];
        //给body身上添加鼠标滑动事件,切换3d旋转轴的中心,实现立体效果
        document.body.onmousemove = function(e){
            console.log(e)
            this.style.perspectiveOrigin = '' + e.pageX + 'px '+ e.pageY +'px';
        }
    </script>
</body>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值