简体   繁体   中英

LitElement how to find out width of an element

How can find the width of an HTML element in Lit-Element know, I'm unable to find to how to use @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>
    `;
  }
}

You can use window.getComputedStyle() function to get the width and any other CSS property of any element.

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);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM