admin管理员组文章数量:1193728
Is there a way to add a css :hover effect on webcompoenents (not from the parent).
I have following example code
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>
`;
}
}
The :host::selection
property works as expected. But the :host:hover
does not seem to have any effect.
I know there are workarounds to this problem like:
- wrapping everything inside a
div
- applying the
:hover
effect at the parent stylesheet
But I wanted to know if I am just missing something (since this does not seem too complex) for the sake of cleaner code.
Is there a way to add a css :hover effect on webcompoenents (not from the parent).
I have following example code
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>
`;
}
}
The :host::selection
property works as expected. But the :host:hover
does not seem to have any effect.
I know there are workarounds to this problem like:
- wrapping everything inside a
div
- applying the
:hover
effect at the parent stylesheet
But I wanted to know if I am just missing something (since this does not seem too complex) for the sake of cleaner code.
Share Improve this question edited Nov 19, 2018 at 20:04 MaximilianMairinger asked Nov 17, 2018 at 12:40 MaximilianMairingerMaximilianMairinger 2,4243 gold badges17 silver badges38 bronze badges1 Answer
Reset to default 29For the sake of cleaner code, use instead the :host()
syntax since :hover
is a pseudo-class applied to the Custom Element. It is then selectable with:
:host(:hover) { ... }
Example:
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>`
}
})
<a-b>Hello World</a-b>
本文标签: javascriptWebcomponents hosthoverStack Overflow
版权声明:本文标题:javascript - Webcomponents :host:hover - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738433303a2086528.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论