第十讲_css2d转换

1. 移动

translate() :参照元素原位置,在X轴和Y轴方向上移动。

<html>
  <style>
    .container1:hover {
     
      width: 200px;
      height: 200px;
      background-color: red;
      /* 在X轴方向移动50px */
      transform: translateX(50px);
    }
    .container1 {
     
      width: 200px;
      height: 200px;
      background-color: red;
    }
    .container2:hover {
     
      width: 200px;
      height: 200px;
      background-color: green;
      /* 在Y轴方向移动50px */
      transform: translateY(50px);
    }
    .container2 {
     
      width: 200px;
      height: 200px;
      background-color: green;
    }
    .container3:hover {
     
      width: 200px;
      height: 200px;
      background-color: blue;
      /* 在Y轴方向移动50px */
      transform: translate(50px, 50px);
    }
    .container3 {
     
      width: 200px;
      height: 200px;
      background-color: blue;
    }
  </style>

  <div class="container1"></div>
  <div class="container2"></div>
  <div class="container3"></div>
</html>

在设置移动量时,除了用像素外,还可以使用百分比。X轴上的百分比是相对于了自身元素的宽,Y轴上的百分比是相对于自身元素的高。

2. 旋转

rotateZ():根据指定角度旋转元素。2D旋转就是绕着Z轴旋转。

<html>
  <style>
    .container1:hover {
     
      width: 200px;
      height: 200px;
      background-color: red;
      /* 旋转20deg */
      transform: rotateZ(20deg)
    }
    .container1 {
     
      width: 200px;
      height: 200px;
      background-color: red;
    }
  </style>

  <div class="container1"></div>
</html>

3. 缩放

scale() :根据指定的倍数在X轴和Y轴缩放元素。

<html>
  <style>
    .container1:hover {
     
      width: 200px;
      height: 200px;
      background-color: red;
      /* X轴上缩小为原来的0.5倍 */
      transform: scaleX(0.5)
    }
    .container1 {
     
      width: 200px;
      height: 200px;
      background-color: red;
    }
    .container2:hover {
     
      width: 200px;
      height: 200px;
      background-color: green;
      /* Y轴上缩小为原来的0.5倍 */
      transform: scaleY(0.5)
    }
    .container2 {
     
      width: 200px;
      height: 200px;
      background-color: green;
    }
    .container3:hover {
     
      width: 200px;
      height: 200px;
      background-color: blue;
      /* 在X轴和Y轴上放大为原来的1.5倍 */
      transform: scale(1.5, 1.5)
    }
    .container3 {
     
      width: 200px;
      height: 200px;
      background-color: blue;
    }
  </style>

  <div class="container1"></div>
  <div class="container2"></div>
  <div class="container3"></div>
</html>

4. 组合转换

<html>
  <style>
    .container1:hover {
     
      width: 200px;
      height: 200px;
      background-color: red;
      /* 先移动,再旋转*/
      /* 注意:旋转后坐标系也会旋转  */
      transform: translate(50px, 50px) rotateZ(30deg);
    }
    .container1 {
     
      width: 200px;
      height: 200px;
      background-color: red;
    }
  </style>

  <div class="container1"></div>
</html>

5. 设置原点

上述的变化都是基于默认的原点,默认的原点是元素的中心点。
transform-origin 设置原点在X轴的位置和Y轴的位置。

<html>
  <style>
    .container1:hover {
     
      width: 200px;
      height: 200px;
      background-color: red;
      /* 旋转20deg */
      transform: rotateZ(20deg);
      /* 设置原点在元素的左上角,元素绕着左上角旋转 */
      transform-origin: left top;
    }
    .container1 {
     
      width: 200px;
      height: 200px;
      background-color: red;
    }
    .container2:hover {
     
      width: 200px;
      height: 200px;
      background-color: green;
      /* 旋转20deg */
      transform: rotateZ(20deg);
      /* 设置原点在元素的左下角 */
      transform-origin: 0 200px;
    }
    .container2 {
     
      width: 200px;
      height: 200px;
      background-color: green;
    }
    .container3:hover {
     
      width: 200px;
      height: 200px;
      background-color: blue;
      /* 旋转20deg */
      transform: rotateZ(20deg);
      /* 设置原点在元素的中心点,也是默认值 */
      transform-origin: 50% 50%;
    }
    .container3 {
     
      width: 200px;
      height: 200px;
      background-color: blue;
    }
  </style>

  <div class="container1"></div>
  <div class="container2"></div>
  <div class="container3"></div>
</html>

ps:上述所有代码执行后,需要鼠标悬停在元素上查看转换的效果。

相关推荐

  1. _css2d转换

    2024-01-13 20:14:02       63 阅读
  2. css 2D转换

    2024-01-13 20:14:02       61 阅读
  3. css 2D转换

    2024-01-13 20:14:02       44 阅读
  4. _css媒体查询

    2024-01-13 20:14:02       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-13 20:14:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-13 20:14:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-13 20:14:02       82 阅读
  4. Python语言-面向对象

    2024-01-13 20:14:02       91 阅读

热门阅读

  1. Rust 宏的使用

    2024-01-13 20:14:02       62 阅读
  2. 一体机旅游景区污水处理设备工艺说明

    2024-01-13 20:14:02       68 阅读
  3. SpringBoot ObjectMapper 返回json 指定字段排序

    2024-01-13 20:14:02       72 阅读
  4. docker配置nginx

    2024-01-13 20:14:02       54 阅读
  5. 问题在于qsize是常量

    2024-01-13 20:14:02       58 阅读
  6. 程序员大学毕业后如何发展?

    2024-01-13 20:14:02       66 阅读
  7. C/C++利用指针输出二维数组元素

    2024-01-13 20:14:02       62 阅读
  8. Pinia store如何做模块化

    2024-01-13 20:14:02       58 阅读
  9. 学习记录之JVM

    2024-01-13 20:14:02       66 阅读
  10. 《清醒思考的艺术》读书笔记

    2024-01-13 20:14:02       48 阅读
  11. 三国杀移动版武将台词大全-神

    2024-01-13 20:14:02       64 阅读
  12. ios 推流 拉流

    2024-01-13 20:14:02       73 阅读