此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。

View in English Always switch to English

translate3d()

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月⁩.

translate3d() CSS 函式用於在 3D 空間中重新定位元素。其結果為 <transform-function> 資料型別。

嘗試一下

transform: translate3d(0, 0, 0);
transform: translate3d(42px, -62px, -135px);
transform: translate3d(-2.7rem, 0, 1rem);
transform: translate3d(5ch, 0.4in, 5em);
<section class="default-example" id="default-example">
  <div class="transition-all" id="example-element">
    <div class="face front">1</div>
    <div class="face back">2</div>
    <div class="face right">3</div>
    <div class="face left">4</div>
    <div class="face top">5</div>
    <div class="face bottom">6</div>
  </div>
</section>
#default-example {
  background: linear-gradient(skyblue, khaki);
  perspective: 800px;
  perspective-origin: 150% 150%;
}

#example-element {
  width: 100px;
  height: 100px;
  perspective: 550px;
  transform-style: preserve-3d;
}

.face {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  position: absolute;
  backface-visibility: inherit;
  font-size: 60px;
  color: white;
}

.front {
  background: rgb(90 90 90 / 0.7);
  transform: translateZ(50px);
}

.back {
  background: rgb(0 210 0 / 0.7);
  transform: rotateY(180deg) translateZ(50px);
}

.right {
  background: rgb(210 0 0 / 0.7);
  transform: rotateY(90deg) translateZ(50px);
}

.left {
  background: rgb(0 0 210 / 0.7);
  transform: rotateY(-90deg) translateZ(50px);
}

.top {
  background: rgb(210 210 0 / 0.7);
  transform: rotateX(90deg) translateZ(50px);
}

.bottom {
  background: rgb(210 0 210 / 0.7);
  transform: rotateX(-90deg) translateZ(50px);
}

此變形的特徵為一個三維向量 [tx, ty, tz]。其座標定義了元素在各個方向移動的量。

語法

css
translate3d(tx, ty, tz)

tx

為一個代表平移向量 [tx, ty, tz] 橫坐標(水平、x 分量)的 <length><percentage>

ty

為一個代表平移向量 [tx, ty, tz] 縱坐標(垂直、y 分量)的 <length><percentage>

tz

為一個代表平移向量 z 分量的 <length>。它不能是 <percentage> 值;如果是百分比值,則包含該變形的屬性將被視為無效。

ℝℙ^2 中的
笛卡兒座標ℝ^2 齊次座標 ℝ^3 中的笛卡兒座標 ℝℙ^3 中的齊次座標

此變形適用於 3D 空間,無法在平面上表示。

平移在 ℝ^3 中不是線性變形,無法使用笛卡兒座標矩陣表示。
(100tx010ty001tz0001)\left( \begin{array}{cccc} 1 & 0 & 0 & tx \\ 0 & 1 & 0 & ty \\ 0 & 0 & 1 & tz \\ 0 & 0 & 0 & 1 \end{array} \right)

形式語法

<translate3d()> = 
translate3d( <length-percentage> , <length-percentage> , <length> )

<length-percentage> =
<length> |
<percentage>

範例

使用單軸平移

HTML

html
<div>靜態</div>
<div class="moved">移動後</div>
<div>靜態</div>

CSS

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

.moved {
  /* 等同於 perspective(500px) translateX(10px) */
  transform: perspective(500px) translate3d(10px, 0, 0px);
  background-color: pink;
}

結果

結合 z 軸與 x 軸平移

HTML

html
<div>靜態</div>
<div class="moved">移動後</div>
<div>靜態</div>

CSS

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

.moved {
  transform: perspective(500px) translate3d(10px, 0, 100px);
  background-color: pink;
}

結果

規範

Specification
CSS Transforms Module Level 2
# funcdef-translate3d

瀏覽器相容性

參見