前端3d动画-----平移 transform: translate3d()

必须加这个属性:transform-style: preserve-3d;

perspective: 900px; 设置了景深才能感到近大远小的感觉

html 复制代码
<!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>
        .box{
            width: 500px;
            height: 500px;
            border: 5px solid black;
            transform-style: preserve-3d;
            /* 
            必加transform-style: preserve-3d; 
            默认是flat:2d;
            */
            position: relative;
            margin: 0 auto;
            /* transform: rotateY(30deg);*/
             
            perspective: 900px;
            /* 设置景深 */
        }
        .center{
            position:absolute;
            width: 200px;
            height: 200px;
            background: red;
            left: 50%;
            top: 50%;
            margin-left: -100px;
            margin-top: -100px;
            transition: all 3s;
        }
        .box:hover .center{
            /* transform: translateZ(800px); */
            transform: translate3d(0,0,200px);
            /* 正值朝前,负值朝后 */
        }

    </style>
</head>
<body>
    <div class="box">
        <div class="center"></div>
    </div>
</body>
</html>

transform: translateZ(80px); 等于transform: translate3d(0,0,80px);,里面的参数对应x,y,z轴

相关推荐
Never_Satisfied3 分钟前
在HTML & CSS中,CSS选中第二个指定类型的子元素的方法
前端·css·html
木斯佳6 分钟前
前端八股文面经大全:字节跳动前端一面(2025-10-09)·面经深度解析
前端·状态模式
Never_Satisfied8 分钟前
在HTML & CSS中,图片嵌入文字方法
前端·css·html
阿林来了8 分钟前
Flutter三方库适配OpenHarmony【flutter_web_auth】— Android 端 Chrome Custom Tabs 实现分析
android·chrome·flutter
huohaiyu6 小时前
从URL到页面的完整解析流程
前端·网络·chrome·url
阿星AI工作室8 小时前
一个简单Demo彻底理解前后端怎么连的丨Figma + Supabase + Vercel
前端·人工智能
aircrushin8 小时前
一拍即传的平替,完全免费的实时照片墙!
前端
鹏北海10 小时前
JSBridge 原理详解
前端
孟健10 小时前
我的网站被黑了:一天灌入 227 万条垃圾数据,AI 写的代码差点让我社死
前端
anOnion11 小时前
构建无障碍组件之Checkbox pattern
前端·html·交互设计