zzr*_*zrv 4 html javascript css web-component shadow-dom
有没有办法在webcompoenents上添加css:hover效果(不是来自父级)。
我有以下示例代码
window.customElements.define('a-b', class extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'}).innerHTML = `
<style>
:host {
display: block;
height: 100px;
width: 100px;
background: red;
}
:host:hover {
background: blue;
}
:host::selection {
background: rgba(0, 0, 0, 0.15);
}
</style>
<slot></slot>
`;
}
}
Run Code Online (Sandbox Code Playgroud)
该:host::selection物业按预期工作。但是:host:hover似乎没有任何作用。
我知道有解决此问题的方法,例如:
div:hover在父样式表上应用效果但是我想知道我是否只是为了简化代码而错过了一些东西(因为这似乎不太复杂)。
为了使代码更简洁,请改用:host()语法,因为这:hover是应用于自定义元素的伪类。然后可以选择:
:host(:hover) { ... }
Run Code Online (Sandbox Code Playgroud)
例:
:host(:hover) { ... }
Run Code Online (Sandbox Code Playgroud)
window.customElements.define('a-b', class extends HTMLElement {
constructor() {
super()
this.attachShadow({mode: 'open'})
.innerHTML = `
<style>
:host(:hover) {
color: white;
background-color: blue;
}
</style>
<slot></slot>`
}
})Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
405 次 |
| 最近记录: |