【问题标题】:How to get the rendered size of a <div>如何获取 <div> 的渲染大小
【发布时间】:2011-11-05 04:15:03
【问题描述】:

我有一个FlowPanel,它的高度是固定的(它实际上是其父级高度的百分比,它是固定的)。
在这个面板中,我添加了几个 div 元素。使用 CSS,我将其高度设置为其父级的 100%。
我想要做的是使用javascript将其宽度设置为等于其高度。

我的问题是我不知道何时运行这段 javascript。我尝试将其添加到容器的onLoad() 方法中,但高度未知(getOffsetHeight() 返回 0)。

我看过类似的问题(如GWT - Retrieve size of a widget that is not displayed),但它们并不完全相同。

【问题讨论】:

    标签: gwt size


    【解决方案1】:

    推迟对getOffsetHeight()的调用,直到事件循环结束:

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

    【讨论】:

    • 效果很好。我对调度程序一无所知,看起来我会不时使用它。非常感谢这个:)
    • @Jason Terk 我无法在 GWTP 中正确完成这项工作:stackoverflow.com/questions/27108858/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    相关资源
    最近更新 更多