此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

matrix()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

CSS 函数 matrix() 指定了一个由指定的 6 个值组成的 2D 变换矩阵。这种矩阵的常量值是隐含的,而不是由参数传递的;其他的参数是以列优先的顺序描述的。

matrix(a, b, c, d, tx, ty)matrix3d(a, b, 0, 0, c, d, 0, 0, 0, 0, 1, 0, tx, ty, 0, 1) 的简写。

备注: 直到 Firefox 16, Gecko 才接受对于 txty<length> 值。

语法

css
matrix(a, b, c, d, tx, ty)

参数值

a b c d

描述线性变换的 <number>

tx ty

描述如何应用这个变换的 <number>

2 下的笛卡尔坐标 ℝℙ2 齐次坐标 3下的笛卡尔坐标 ℝℙ3下的齐次坐标
ac bd


a c tx
b d ty
0 0 1

a c tx
b d ty
0 0 1
ac0txbd0ty00100001
[a b c d tx ty]

这些值表示以下函数:matrix(scaleX(), skewY(), skewX(), scaleY(), translateX(), translateY())

示例

HTML

html
<div>正常</div>
<div class="changed">改变后</div>

CSS

css
div {
  width: 80px;
  height: 80px;
  background-color: skyblue;
}

.changed {
  transform: matrix(1, 2, -1, 1, 80, 80);
  background-color: pink;
}

结果

规范

Specification
CSS Transforms Module Level 1
# funcdef-transform-matrix