首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

translate()

translate() CSS函数在水平和/或垂直方向上重新定位元素。

这种变换的特点是二维矢量。其坐标定义元素在每个方向上的移动量。

语法

translate()函数被接受一个或两个值。

代码语言:javascript
复制
translate(tx)

translate(tx, ty)

可能值

tx——表示平移矢量的横坐标的<length>值。

ty——表示平移矢量的纵坐标的<length>值。如果未指定,则其默认值为0。例如,translate(2)相当于translate(2, 0)

Cartesian coordinates on ?2

Homogeneous coordinates on ??2

Cartesian coordinates on ?3

Homogeneous coordinates on ??3

A translation is not a linear transform in ?2 and cannot be represented using a matrix in the Cartesian coordinate system.

10tx01ty001

10tx01ty001

100tx010ty00100001

| 1 0 0 1 tx ty |

实例

使用单轴平移

HTML

代码语言:javascript
复制
<div>Static</div>
<div class="moved">Moved</div>
<div>Static</div>

CSS

代码语言:javascript
复制
div {
  width: 60px;
  height: 60px;
  background-color: skyblue;
}

.moved {
  /* Equivalent to translateX(10px) */
  transform: translate(10px);
  background-color: pink;
}

结果

结合y轴和x轴平移

HTML

代码语言:javascript
复制
<div>Static</div>
<div class="moved">Moved</div>
<div>Static</div>

CSS

代码语言:javascript
复制
div {
  width: 60px;
  height: 60px;
  background-color: skyblue;
}

.moved {
  transform: translate(10px, 10px);
  background-color: pink;
}

结果

另见

  • transform
  • <transform-function>

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com