CSSNumericValue: type() 方法

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

type() 方法是 CSSNumericValue 接口的一部分,它返回 CSSNumericValue 的类型,类型包括 angleflexfrequencylengthresolutionpercentpercentHinttime 之一。

语法

js
type()

参数

无。

返回值

一个 CSSNumericType 字典,包含以下属性:

  • length
  • angle
  • 时间
  • frequency
  • resolution
  • flex
  • percent
  • percentHint

对于除了 percentHint 之外的每个属性,其值是一个整数,表示该单位的幂次。例如,calc(1px * 1em) 的数值将返回 { length: 2 }

percentHint 属性是一个字符串,指示百分比所应用的数值类型。字符串值与类型属性相同:"length""angle""time""frequency""resolution""flex""percent"。它表示该类型实际包含一个百分比,但该百分比最终会解析为提示的基础类型,因此在类型中已被替换为它。

异常

无。

示例

js
let mathSum = CSS.px("23")
  .sub(CSS.percent("4"))
  .sub(CSS.cm("3"))
  .sub(CSS.in("9"));
// Returns an object with the structure: {length: 1, percentHint: "length"}
let cssNumericType = mathSum.type();

规范

规范
CSS 类型化 OM Level 1
# dom-cssnumericvalue-type

浏览器兼容性