【CSS】3d平移、旋转、缩放

3d变换:平移、旋转、缩放

坐标
2d: x y
3d: x y z

  • x轴称为左右+向右 -向左
  • y轴称为上下+向下 -向上
  • z轴称为前后+向前 -向后

3d平移

语法:transform
translatez() +向前 -向后
translate3d(x,y,z)

3d环境

  • 3d环境:加给父元素以及以上的元素
    • 默认值是2d环境
    • transform-style: preserve-3d;

3d眼镜

  • 3d眼镜:加给父元素以及以上的元素
    • 默认值是none
    • 透视 景深
    • 推荐写:1000px-2000px
    • perspective: 1000px;

设置元素的透视效果

transform:perspective()

  • 设置给当前元素
  • 里面只能写数值
 <style>
    body {
      height: 600px;
      /* 3d环境 */
      transform-style: preserve-3d;
      /* 3d眼镜 */
      perspective: 1000px;
    }

    div {
      width: 300px;
      height: 300px;
      background-color: cyan;
      position: fixed;
      left: 30%;
      top: 30%;
      transition: all 5s linear;
      font-size: 40px;
    }

    body:hover div {
      transform: translateZ(200px);
      transform: translateZ(-200px);
      transform: translate3d(100px, 100px, -300px);
    }
  </style>
  <div>文本文本</div>

3d旋转

transform

  • rotate()
  • rotateZ()
  • rotateX()
  • rotateY()

rotate3d(x,y,z,3odeg)

  • x,y,z 写 0-1之间
  • 0表示不旋转
  • 1表示旋转
  • 也可以写小数,不推荐
<style>
    body {
      height: 600px;
      /* 3d环境 */
      transform-style: preserve-3d;
      /* 3d眼镜 */
      perspective: 1000px;
    }

    div {
      width: 300px;
      height: 300px;
      background-color: cyan;
      position: fixed;
      left: 30%;
      top: 30%;
      transition: all 5s linear;
      font-size: 40px;
    }

    body:hover div {
      /* 
      X:+里 -外
      Y:+右 -左
      Z:+顺 -逆
        rotate()=rotateZ()
      */
      transform: rotateX(360deg);
      transform: rotateX(-360deg);
      transform: rotateY(360deg);
      transform: rotateY(-360deg);
      transform: rotateZ(360deg);
      transform: rotateZ(-360deg);
      transform: rotate(360deg);
      transform: rotate(-360deg);
      transform: rotate3d(1, 0, 0, 90deg);
    }
  </style>

3D缩放

transform:

  • scalez()
  • scale3d(x,y,z)

注意: 单独写没有用,必须结合其他变形函数一起使用,而且要写在其他变形函数后面

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    li {
      list-style: none;
    }

    body {
      height: 600px;
    }

    /* ul 立方体 */
    ul {
      width: 300px;
      height: 300px;
      position: fixed;
      left: 50%;
      top: 50%;
      margin-left: -150px;
      margin-top: -150px;
      transform-style: preserve-3d;
      /* perspective: 1000px; */
      transition: all 10s;
    }

    /* li 6个面 */
    ul li {
      width: 300px;
      height: 300px;
      font-size: 40px;
      color: #fff;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0.5;
    }

    /* 前 */
    ul li:nth-child(1) {
      background-color: red;
      transform: translateZ(150px);
    }

    /* 上 */
    ul li:nth-child(2) {
      background-color: orange;
      transform: translateY(-150px) rotateX(90deg);
    }

    /* 下 */
    ul li:nth-child(3) {
      background-color: yellow;
      transform: translateY(150px) rotateX(-90deg);
    }

    /* 左 */
    ul li:nth-child(4) {
      background-color: green;
      transform: translateX(-150px) rotateY(-90deg);
    }

    /* 右 */
    ul li:nth-child(5) {
      background-color: cyan;
      transform: translateX(150px) rotateY(90deg);
    }

    /* 后 */
    ul li:nth-child(6) {
      background-color: purple;
      transform: translateZ(-150px) rotateY(180deg);
    }

    /* 最后,旋转起来看效果 */
    body:hover ul {
      transform: rotate3d(0, 1, 0, 360deg) scaleZ(2);
      transform: rotate3d(0, 1, 0, 360deg) scaleZ(0.1);
      transform: rotate3d(0, 1, 0, 360deg) scale3d(0.2, 1.2, 0.5);
    }
  </style>
</head>
<ul>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
</ul>

💡代码执行结果
请添加图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芒果Cake

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值