【问题标题】:LitElement how to find out width of an elementLitElement如何找出元素的宽度
【发布时间】:2021-02-12 00:30:30
【问题描述】:

如何在 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>
    `;
  }
}

【问题讨论】:

标签: javascript shadow-dom lit-element


【解决方案1】:

您可以使用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);
  }
}

【讨论】:

    猜你喜欢
    • 2014-01-13
    • 2014-01-03
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    相关资源
    最近更新 更多