【问题标题】:How to achieve an smooth height transition when adding new elements to a Panel in GWT?在 GWT 中向 Panel 添加新元素时如何实现平滑的高度过渡?
【发布时间】:2013-10-27 11:28:45
【问题描述】:

我有一个 FocusPanel,当点击它时,它会向自身添加新的元素/小部件,使其高度增加。请注意,FocusPanel 的 css 中的高度没有显式变化,高度只是在面板内添加新元素后增加

我希望通过平稳过渡来增加高度,但我不确定如何实现。

我尝试将 css transition: height 2s; 应用于 FocusPanel 以及我添加到其中的所有其他元素/小部件。但这似乎不起作用,根本没有过渡。我认为这是因为高度不会因为我更改 css 属性而增加,而是通过向容器添加更多元素。

以编程方式向面板添加新元素时,实现高度平滑过渡的正确方法是什么?谢谢!

附言。我想要实现的一个很好的例子是 twitter 在单击 twit 时处理面板高度转换的方式。

【问题讨论】:

  • 希望对您有所帮助blog-demo.appspot.com/#animation
  • 恐怕您需要在应用程序中以编程方式控制高度。因此,每当您从面板中添加或删除元素时,您都会计算新的高度并为尺寸变化设置动画。为了实现动画,您可以使用几个框架:GQuery、GWT Animation 或 HTML 过渡。
  • 顺便说一句,您可以查看 twitter 是如何做到的。只需打开 FireBug(或其他 Web 开发工具),看看 twitter 面板的参数会发生什么。身高有变化吗?还是他们有其他技巧可以做到这一点?

标签: css gwt css-transitions uibinder gwtp


【解决方案1】:

CSS 动画只有在将高度设置为固定值时才有效。

一种方法是创建您自己的面板实现,并覆盖 add 方法,以便它负责计算高度并在动画时间之前和之后设置它。

正如@fascynacja 在其评论中指出的那样,由于不同的原因,我会选择gwtquery 来做这件事,但主要的一个原因是它是一个用 gwt 开发的轻量级库,它允许你用很少的东西做很多事情代码行。

这里有一个使用 gquery 动画执行所需操作的面板示例。

import static com.google.gwt.query.client.GQuery.*;
[...]

// Create your own implementation of a panel
public static class MyFlowPanel extends FlowPanel {

  // The GQuery object for this panel
  GQuery $this = $(this);

  // Override the add method so as each time it is called, we run an animation
  // You can do the same with the remove method.
  @Override
  public void add(Widget w) {
    // Compute the actual height
    int hInitial = $this.height();
    // Set height to auto before adding the new child.
    $this.height("auto");
    // Add the new widget to panel
    super.add(w);
    // Compute the new height
    int hFinal = $this.height();

    // Use Gquery to .animate the panel from the old to the new height
    // You could replace this with css3 transitions
    $this.height(hInitial)
         .stop(true)
         .animate("height: " + hFinal, 2000);
  };
};

public void onModuleLoad() {
  // Create your panel, and use it as usual in GWT
  final FlowPanel myFlowPanel = new MyFlowPanel();
  RootPanel.get().add(myFlowPanel);
  // Set some css properties to your panel. You could set these in your style-sheet.
  $(myFlowPanel).css($$("border: 1px solid grey; border-radius: 8px; background: #F5FFFA; width: 500px; padding: 8px"));

  // Add 10 labels to the panel in periodes of 1000 ms
  Scheduler.get().scheduleFixedPeriod(new RepeatingCommand() {
    int c = 10;
    public boolean execute() {
      if (c-- > 0) {
        myFlowPanel.add(new Label(c + " Lorem ipsum dolor sit amet, consectetur adipiscing elit."));
        return true;
      }
      return false;
    }
  }, 1000);
}

【讨论】:

  • 感谢托卡约。我理解你解释的依据。覆盖 add 方法对我来说似乎是一个很好的解决方案。我会在这周晚些时候试试。我也会考虑使用 GQuery。顺便说一句,你不认为使用 GWT/GQuery 动画的 CPU 效率低于 CSS 的转换吗?
  • 当然,它的效率较低,但取决于您的瓶颈在哪里。如果您想在处理数据时为某些内容设置动画,请不要这样做,因为动画不会是流畅的。但通常做一些像twitter这样的事情并不重要。
  • 明白,谢谢。关于您使用 gquery 的建议的另一个后续评论。如果理解正确,gquery 提供了两个主要优点——除了内置的花哨效果——: 1)与使用普通 GWT 相比,代码行数更少。 2)我不需要持有我的小部件的实例来访问它们。我想,这减少了客户端的内存占用? (虽然我不知道 GWT 如何将实例转换为 JS……也许它使用了自己的 css 选择器框架?)。我还缺少其他东西吗?
猜你喜欢
  • 2022-08-20
  • 2016-10-03
  • 2022-07-20
  • 2017-05-28
  • 2020-09-17
  • 2020-12-14
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
相关资源
最近更新 更多