大网数据广告活动
Web 标准与前端开发

CSSRotate: CSSRotate() constructor

· 52CE 我爱测

The **`CSSRotate()`** constructor creates a new CSSRotate object representing the transform-function/rotate value of the individual transform property in CSS.

CSS Typed Object Model API

The CSSRotate() constructor creates a new CSSRotate object representing the transform-function/rotate value of the individual transform property in CSS.

This can be specified as either a 2D rotation by a particular angle, or as a 3D rotation by an angle around a particular axis.

Syntax

new CSSRotate(angle)
new CSSRotate(x, y, z, angle)

Parameters

  • CSSRotate.angle
    • : A value for the angle of rotation of the CSSRotate object to be constructed. This must be a CSSNumericValue.
  • CSSRotate.x
    • : A number or CSSNumericValue value indicating the x-coordinate of the rotation axis vector of the CSSRotate object to be constructed. Only used, and required, when constructing a 3D rotation; the 2-argument form implies a rotation axis of (0, 0, 1) (the z-axis).
  • CSSRotate.y
    • : A number or CSSNumericValue value indicating the y-coordinate of the rotation axis vector of the CSSRotate object to be constructed. Only used, and required, when constructing a 3D rotation.
  • CSSRotate.z
    • : A number or CSSNumericValue value indicating the z-coordinate of the rotation axis vector of the CSSRotate object to be constructed. Only used, and required, when constructing a 3D rotation.

Exceptions

  • TypeError
    • : Raised if the value of the angle property is not an <angle> value or CSSRotate.x, CSSRotate.y, CSSRotate.z are not <number> values.

Examples

Constructing a 2D rotation

The 2-argument form takes only an angle, and implies a rotation around the z-axis (equivalent to rotate3d(0, 0, 1, angle)):

const rotate2D = new CSSRotate(CSS.deg(45));

console.log(rotate2D.is2D); // true
console.log(rotate2D.toString()); // "rotate(45deg)"

Constructing a 3D rotation

The 4-argument form takes the rotation axis coordinates followed by the angle:

const rotate3D = new CSSRotate(1, 1, 0, CSS.deg(45));

console.log(rotate3D.is2D); // false
console.log(rotate3D.toString()); // "rotate3d(1, 1, 0,45deg)"

Handling an invalid angle

try {
  const rotate = new CSSRotate(CSS.px(45));
} catch (e) {
  console.log(e); // TypeError: px is not an angle
}

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。