前端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轴

相关推荐
巴勒个啦1 小时前
Vue 3.6 Vapor Mode 实战:我把一个 Vue3 项目的渲染性能提升了 4 倍
前端·angular.js
不简说1 小时前
# JS 代码技巧 vol.7 — 20 个浏览器 API 实战,自带 API 能干的事别自己封装
前端·javascript·面试
CodexDave1 小时前
数据库连接池耗尽:排查顺序与三层兜底
服务器·前端·数据库·git·云原生·容器·kubernetes
Hilaku1 小时前
为什么 Cloudflare 能做到毫秒级冷启动,而其它云厂商却不行?
前端·javascript·程序员
2301_794461571 小时前
JavaScript 基础知识点详解
前端·javascript·html
HjhIron1 小时前
在浏览器中跑DeepSeek-R1:用React+WebGPU实现端侧AI推理
前端·ai编程
早期的虫儿有鸟吃2 小时前
vue2--Vuex 模块化
开发语言·前端·javascript
C++、Java和Python的菜鸟2 小时前
第5章 后端Web基础 (MySQL基础)
前端·mysql·adb
门前大桥下.2 小时前
HTML-01我的第一个网页
前端·html
datagear2 小时前
DataGear 6.0.0 新特性之本地图表
数据分析·html·数据可视化