美好时光 如何在lit中动态设计风格? 我的主要目标是根据用户在输入元素中的输入来更改元素的颜色。
最佳答案
在lit css标签函数中不能使用${}
!
但是您可以选择元素然后更改样式
import {html, css, LitElement} from 'lit';
import {customElement, property, query} from 'lit/decorators.js';
@customElement('dynamic-style')
export class DynamicStyle extends LitElement {
static styles = css`
label {
color: #023047;
}
`;
@property()
color: string;
@query('input') input: HTMLSelectElement | undefined;
@query('label') label: HTMLSelectElement | undefined;
render() {
return html`
<label for="color-input">HEX color: </label>
<input id="color-input" placeholder="#023047" />
`;
}
firstUpdated() {
this.input.addEventListener('input', () => {
this.label.style.color = this.input.value;
});
}
}
关于css - 如何在lit中动态设计风格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72250535/