[英]LitElement how to find out width of an element
如何在 Lit-Element 中找到 HTML 元素的宽度,我无法找到如何使用 @query
import { LitElement, html, query } from 'lit-element';
class MyElement extends LitElement {
@query('#first')
first;
render() {
return html`
<div id="first">I want to know the size of this div</div>
<div id="second">and set size to this div</div>
`;
}
}
您可以使用window.getComputedStyle()函数来获取任何元素的宽度和任何其他 CSS 属性。
import { LitElement, html, query } from 'lit-element';
class MyElement extends LitElement {
render() {
return html`
<div id="first">I want to know the size of this div</div>
<div id="second">and set size to this div</div>
`;
}
updated() {
const elem = this.shadowRoot.querySelector('#first');
this._width = getComputedStyle(elem).getPropertyValue('width');
console.log(this._width);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.