【发布时间】:2018-08-02 17:24:48
【问题描述】:
我有以下简单的组件:
用法:
<style>
my-element {
--my-bg: green;
--my-text: red;
}
</style>
<my-element myStyling>
<p>Test</p>
</my-element>
组件:
const template = document.createElement('template');
template.innerHTML = `
<style>
:host([myStyling]), :host([myStyling]) {
background-color: var(--my-bg);
color: var(--my-text);
}
</style>
<slot></slot>
Static
`;
class MyElement extends HTMLElement {
constructor() {
super();
// Attach a shadow root to the element.
let shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.appendChild(template.content.cloneNode(true));
}
}
window.customElements.define('my-element', MyElement);
代码输出如下结果:
为什么color: green适用于静态文本和shadow DOM,而背景颜色样式只适用于静态文本?
【问题讨论】:
标签: css web-component shadow-dom