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 September 2015 .
CSS
內的**
transform-origin
**屬性可以設定元素變化的原點。
嘗試一下
transform-origin: center;
transform-origin: top left;
transform-origin: 50px 50px;
/* 3D rotation with z-axis origin */
transform-origin: bottom right 60px;
<section id="default-example">
<div id="example-container">
<div id="example-element">Rotate me!</div>
alt=""
id="crosshair"
src="/shared-assets/images/examples/crosshair.svg"
width="24px" />
<div id="static-element"></div>
</section>
@keyframes rotate {
from {
transform: rotate(0);
transform: rotate(30deg);
@keyframes rotate3d {
from {
transform: rotate3d(0);
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()
函數的原點為旋轉中心。 (This property is applied by first translating the element by the negated value of the property, then applying the element's transform, then translating by the property value.)
語法
語法
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
屬性可以使用多次,每一次都代表著一個偏移量。若未設定偏移量,則重置為其對應的初始值。
If two or more values are defined and either no value is a keyword, or the only used keyword is center
, then the first value represents the horizontal offset and the second represents the vertical offset.
One-value syntax:
The value must be a <length>
, a <percentage>
, or one of the keywords left
, center
, right
, top
or bottom
.
Two-value syntax:
One value must be a <length>
, a <percentage>
, or one of the keywords left
, center
, and right
.
The other value must be a <length>
, a <percentage>
, or one of the keywords top
, center
, and bottom
.
Three-value syntax:
The first two values are the same as for the two-value syntax.
The third value must be a <length>
. It always represents the Z offset.
Values
Values
- x-offset
- offset-keyword
- y-offset
- x-offset-keyword
- y-offset-keyword
- z-offset
Is a
<length>
or a
<percentage>
describing how far from the left edge of the box the origin of the transform is set.
Is one of the
left
,
right
,
top
,
bottom
, or
center
keyword describing the corresponding offset.
Is a
<length>
or a
<percentage>
describing how far from the top edge of the box the origin of the transform is set.
Is one of the
left
,
right
, or
center
keyword describing how far from the left edge of the box the origin of the transform is set.
Is one of the
top
,
bottom
, or
center
keyword describing how far from the top edge of the box the origin of the transform is set.
Is a
<length>
(and never a
<percentage>
which would make the statement invalid) describing how far from the user eye the z=0 origin is set.
The keywords are convenience shorthands and match the following
<percentage>
values:
Keyword
Value
Formal syntax
Formal syntax
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>
Examples
Examples