Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. CSS:层叠样式表
  3. CSS 参考
  4. Properties
  5. transform-origin

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

View in EnglishAlways switch to English

transform-origin

Baseline Widely available

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

transform-origin CSS 属性让你更改一个元素变形的原点。

尝试一下

transform-origin: center;
transform-origin: top left;
transform-origin: 50px 50px;
/* 3D rotation with z-axis origin */transform-origin: bottom right 60px;
<section>  <div>    <div>Rotate me!</div>    <img      alt=""           src="/shared-assets/images/examples/crosshair.svg"      width="24px" />    <div></div>  </div></section>
@keyframes rotate {  from {    transform: rotate(0);  }  to {    transform: rotate(30deg);  }}@keyframes rotate3d {  from {    transform: rotate3d(0);  }  to {    transform: rotate3d(1, 2, 0, 60deg);  }}#example-container {  width: 160px;  height: 160px;  position: relative;}#example-element {  width: 100%;  height: 100%;  display: flex;  position: absolute;  align-items: center;  justify-content: center;  background: #f7ebee;  color: #000000;  font-size: 1.2rem;  text-transform: uppercase;}#example-element.rotate {  animation: rotate 1s forwards;}#example-element.rotate3d {  animation: rotate3d 1s forwards;}#crosshair {  width: 24px;  height: 24px;  opacity: 0;  position: absolute;}#static-element {  width: 100%;  height: 100%;  position: absolute;  border: dotted 3px #ff1100;}
"use strict";window.addEventListener("load", () => {  function update() {    const selected = document.querySelector(".selected");    /* Restart the animation           https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Animations/Tips */    el.className = "";    window.requestAnimationFrame(() => {      window.requestAnimationFrame(() => {        el.className =          el.style.transformOrigin.split(" ")[2] === "60px"            ? "rotate3d"            : "rotate";      });    });    const transformOrigin = getComputedStyle(el).transformOrigin;    const pos = transformOrigin.split(/\s+/);    crosshair.style.left = `calc(${pos[0]} - 12px)`;    crosshair.style.top = `calc(${pos[1]} - 12px)`;  }  const crosshair = document.getElementById("crosshair");  const el = document.getElementById("example-element");  const observer = new MutationObserver(() => {    update();  });  observer.observe(el, {    attributes: true,    attributeFilter: ["style"],  });  update();  crosshair.style.opacity = "1";});

转换起点是应用转换的点。例如,rotate()函数的转换原点是旋转中心。(这个属性的应用原理是先用这个属性的赋值转换该元素,进行变形,然后再用这个属性的值把元素转换回去)

默认的转换原点是center

语法

css
/* One-value syntax */transform-origin: 2px;transform-origin: bottom;/* x-offset | y-offset */transform-origin: 3cm 2px;/* x-offset-keyword | y-offset */transform-origin: left 2px;/* x-offset-keyword | y-offset-keyword */transform-origin: right top;/* y-offset-keyword | x-offset-keyword */transform-origin: top right;/* x-offset | y-offset | z-offset */transform-origin: 2px 30% 10px;/* x-offset-keyword | y-offset | z-offset */transform-origin: left 5px -3px;/* x-offset-keyword | y-offset-keyword | z-offset */transform-origin: right bottom 2cm;/* y-offset-keyword | x-offset-keyword | z-offset */transform-origin: bottom right 2cm;/* Global values */transform-origin: inherit;transform-origin: initial;transform-origin: unset;

transform-origin属性可以使用一个,两个或三个值来指定,其中每个值都表示一个偏移量。没有明确定义的偏移将重置为其对应的初始值

如果定义了两个或更多值并且没有值的关键字,或者唯一使用的关键字是center,则第一个值表示水平偏移量,第二个值表示垂直偏移量。

  • 一个值:

  • 两个值:

  • 三个值:

    • 前两个值和只有两个值时的用法相同。
    • 第三个值必须是<length>。它始终代表 Z 轴偏移量。

x-offset

定义变形中心距离盒模型的左侧的<length><percentage>偏移值。

offset-keyword

leftrighttopbottomcenter 中之一,定义相对应的变形中心偏移。

y-offset

定义变形中心距离盒模型的顶的<length><percentage>偏移值。

x-offset-keyword

leftrightcenter 中之一,定义相对应的变形中心偏移。

y-offset-keyword

topbottomcenter 中之一,定义相对应的变形中心偏移。

z-offset

定义变形中心距离用户视线(z=0 处)的<length>(不能是<percentage>)偏移值。

关键字是方便的简写方法,等同于以下<percentage>值:

keywordvalue
left0%
center50%
right100%
top0%
bottom100%

形式语法

transform-origin =
[left|center|right|top|bottom|<length-percentage>]|
[left|center|right|<length-percentage>][top|center|bottom|<length-percentage>]<length>?|
[[center|left|right]&&[center|top|bottom]]<length>?

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

示例

CodeSample

transform: none;

html
<div>&nbsp;</div>
css
.box1 {margin: 0.5em;width: 3em;height: 3em;border: solid 1px;background-color: palegreen;transform: none;-webkit-transform: none;}

transform: rotate(30deg);

html
<div>&nbsp;</div>
css
.box2 {margin: 0.5em;width: 3em;height: 3em;border: solid 1px;background-color: palegreen;transform: rotate(30deg);-webkit-transform: rotate(30deg);}

transform: rotate(30deg);
transform-origin: 0 0;

html
<div>&nbsp;</div>
css
.box3 {margin: 0.5em;width: 3em;height: 3em;border: solid 1px;background-color: palegreen;transform-origin: 0 0;-webkit-transform-origin: 0 0;transform: rotate(30deg);-webkit-transform: rotate(30deg);}

transform: rotate(30deg);
transform-origin: 100% 100%;

html
<div>&nbsp;</div>
css
.box4 {margin: 0.5em;width: 3em;height: 3em;border: solid 1px;background-color: palegreen;transform-origin: 100% 100%;-webkit-transform-origin: 100% 100%;transform: rotate(30deg);-webkit-transform: rotate(30deg);}

transform: rotate(30deg);
transform-origin: -1em -3em;

html
<div>&nbsp;</div>
css
.box5 {margin: 0.5em;width: 3em;height: 3em;border: solid 1px;background-color: palegreen;transform-origin: -1em -3em;-webkit-transform-origin: -1em -3em;transform: rotate(30deg);-webkit-transform: rotate(30deg);}

transform: scale(1.7);

html
<div>&nbsp;</div>
css
.box6 {margin: 0.5em;width: 3em;height: 3em;border: solid 1px;background-color: palegreen;transform: scale(1.7);-webkit-transform: scale(1.7);}

transform: scale(1.7);
transform-origin: 0 0;

html
<div>&nbsp;</div>
css
.box7 {margin: 0.5em;width: 3em;height: 3em;border: solid 1px;background-color: palegreen;transform: scale(1.7);-webkit-transform: scale(1.7);transform-origin: 0 0;-webkit-transform-origin: 0 0;}

transform: scale(1.7);
transform-origin: 100% -30%;

html
<div>&nbsp;</div>
css
.box8 {margin: 0.5em;width: 3em;height: 3em;border: solid 1px;background-color: palegreen;transform: scale(1.7);-webkit-transform: scale(1.7);transform-origin: 100% -30%;-webkit-transform-origin: 100% -30%;}

transform: skewX(50deg);
transform-origin: 100% -30%;

html
<div>&nbsp;</div>
css
.box9 {margin: 0.5em;width: 3em;height: 3em;border: solid 1px;background-color: palegreen;transform: skewX(50deg);-webkit-transform: skewX(50deg);transform-origin: 100% -30%;-webkit-transform-origin: 100% -30%;}

transform: skewY(50deg);
transform-origin: 100% -30%;

html
<div>&nbsp;</div>
css
.box10 {margin: 0.5em;width: 3em;height: 3em;border: solid 1px;background-color: palegreen;transform: skewY(50deg);-webkit-transform: skewY(50deg);transform-origin: 100% -30%;-webkit-transform-origin: 100% -30%;}

规范

Specification
CSS Transforms Module Level 1
# transform-origin-property
初始值50% 50% 0
适用元素可变换元素
是否是继承属性
Percentagesrefer to the size of bounding box
计算值对于<length> 则为绝对值,否则为百分比值
动画类型simple list of length, percentage, or calc

浏览器兼容性

参见

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp