CSS Typed Object Model API - Web APIs 编辑

The CSS Typed Object Model API simplifies CSS property manipulation by exposing CSS values as typed JavaScript objects rather than strings. This not only simplifies CSS manipulation, but also lessens the negative impact on performance as compared to HTMLElement.style.

Generally, CSS values can be read and written in JavaScript as strings, which can be slow and cumbersome. CSS Typed Object Model API provides interfaces to interact with underlying values, by representing them with specialized JS objects that can be manipulated and understood more easily and more reliably than string parsing and concatenation. This is easier for authors (for example, numeric values are reflected with actual JS numbers, and have unit-aware mathematical operations defined for them). It is also generally faster, as values can be directly manipulated and then cheaply translated back into underlying values without having to both build and parse strings of CSS.

CSS Typed OM both allows for the performant manipulation of values assigned to CSS properties while enabling maintainable code that is both more understand and easier to write.

Interfaces

CSSStyleValue

The CSSStyleValue interface of the CSS Typed Object Model API is the base class of all CSS values accessible through the Typed OM API. An instance of this class may be used anywhere a string is expected.

 CSSStyleValue.parse(property, cssText)  
The parse() method of the CSSStyleValue interface allows a CSSNumericValue to be constructed from a CSS string. It sets a specific CSS property to the specified values and returns the first value as a CSSStyleValue object.
 CSSStyleValue.parseAll()
The parseAll() method of the CSSStyleValue interface sets all occurrences of a specific CSS property to the specified value and returns an array of CSSStyleValue objects, each containing one of the supplied values.

StylePropertyMap

The StylePropertyMap interface of the CSS Typed Object Model API provides a representation of a CSS declaration block that is an alternative to CSSStyleDeclaration.

StylePropertyMap.set()
Method of StylePropertyMap interface that changes the CSS declaration with the given property to the value given.
StylePropertyMap.append()
Method that adds a new CSS declaration to the StylePropertyMap with the given property and value.
StylePropertyMap.delete()
Method that removes the CSS declaration with the given property from the StylePropertyMap.
StylePropertyMap.clear()
Method that removes all declarations in the StylePropertyMap.

CSSUnparsedValue

The CSSUnparsedValue interface of the CSS Typed Object Model API represents property values that reference custom properties. It consists of a list of string fragments and variable references.

CSSUnparsedValue.CSSUnparsedValue() constructor
Creates a new CSSUnparsedValue object which represents property values that reference custom properties.
CSSUnparsedValue.entries()
Method returning an array of a given object's own enumerable property [key, value] pairs in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
CSSUnparsedValue.forEach()
Method executing a provided function once for each element of the CSSUnparsedValue.
CSSUnparsedValue.keys()
Method returning a new Array Iterator object that contains the keys for each index in the array.

CSSKeywordValue Serialization

The CSSKeywordValue interface of the CSS Typed Object Model API creates an object to represent CSS keywords and other identifiers.

CSSKeywordValue.CSSKeywordValue() constructor
Constructor creates a new CSSKeywordValue.CSSKeywordValue() object which represents CSS keywords and other identifiers.
CSSKeywordValue.value()
Property of the CSSKeywordValue interface returning or setting the value of the CSSKeywordValue.

CSSStyleValue Interfaces

CSSStyleValue is the base class through which all CSS values are expressed. Subclasses include:

CSSImageValue objects
An interface representing values for properties that take an image, for example background-image, list-style-image, or border-image-source.
CSSKeywordValue
An interface which creates an object to represent CSS keywords and other identifiers. When used where a string is expected, it will return the value of CSSKeyword.value.
CSSNumericValue
An interface representing operations that all numeric values can perform, including:
CSSPositionValue
Represents values for properties that take a position, for example object-position.
CSSTransformValue

An interface representing a list of transform list values. They "contain" one or more CSSTransformComponents, which represent individual transform function values.

CSSUnparsedValue
Represents property values that reference custom properties. It consists of a list of string fragments and variable references.

Specifications

SpecificationStatusComment
CSS Typed OM Level 1Working DraftInitial definition.

Browser compatibility

See individual interfaces

See also

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
    列表为空,暂无数据

    词条统计

    浏览:181 次

    字数:12041

    最后编辑:7年前

    编辑次数:0 次

      我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
      原文