How to get the rendered size of a

2019-04-01 00:32发布

问题:

I have a FlowPanel, whose height is fixed (it is actually a percentage of its parent's height, which is fixed).
In this panel, I add several div elements. Using CSS, I set its height as 100% of its parent.
What I want to do is set its width to be equal to its height using javascript.

The issue I have is that I don't know when to run this bit of javascript. I tried adding it to the onLoad() method of my container, but the height is not known yet (getOffsetHeight() returns 0).

I had a look at similar questions (like GWT - Retrieve size of a widget that is not displayed), but they were not exactly the same thing.

回答1:

Defer the call to getOffsetHeight() until the end of the event loop:

@Override
public void onLoad() {
  Scheduler.get().scheduleDeferred(new ScheduledCommand() {
    @Override
    public void execute() {
      // Get and work with the element's offsetHeight.
    }
  }
}


标签: gwt size