CSSImageValue
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
CSS 类型对象模型 API 的 CSSImageValue
接口表示取用图像的属性(例如 background-image
、list-style-image
或 border-image-source
)的值。
CSSImageValue 对象表示涉及 URL(如 url()
或 image()
,但不包括 linear-gradient()
或 element()
)的 <image>
。
实例属性
无。
实例方法
继承 CSSStyleValue
的方法。
示例
创建元素
html
<button>魔杖</button>
我们添加了一些 CSS,包括请求二进制文件的背景图片:
css
button {
display: inline-block;
min-height: 100px;
min-width: 100px;
background: no-repeat 5% center url(magic-wand.png) aqua;
}
我们获取元素的样式映射。然后,我们从样式映射中获取(get())背景图片(background-image),并将其字符串化:
js
// 获得元素
const button = document.querySelector("button");
// 使用 computedStyleMap() 读取所有已计算的样式
const allComputedStyles = button.computedStyleMap();
// 返回 CSSImageValue 示例
console.log(allComputedStyles.get("background-image"));
console.log(allComputedStyles.get("background-image").toString());
规范
Specification |
---|
CSS Typed OM Level 1 # imagevalue-objects |