使用 gwt 和 ui-binder 更改悬停时的图像

发布于 2024-12-18 14:13:49 字数 939 浏览 5 评论 0原文

我正在使用声明性/ui-binder 方法将图像添加到页面。这与 GWT 提供的 ImageBundle 函数结合使用。

我想做的是将鼠标悬停在图像上时更改图像。我的问题是:最好的方法是什么?我当前的方法首先是最好的方法吗?

我的代码看起来类似于:

<ui:with field='res' type='path.to.my.app.AppResources' />
...
<g:HorizontalPanel ui:field='horizPanel' >
    <g:Image ui:field='image1' resource='{res.image1}'/>
</g:HorizontalPanel>

然后通过 AbstractImagePrototype 将其绑定到 ImageBundle 类中。

然后,在我的主处理程序中,我有类似的内容:

@UiHandler("image1")
public void onMouseOver(MouseOverEvent event)
{
    /* What do I do here? */
}

说当用户将鼠标悬停在 image1 上时,我想用 image2 替换 image1 (并在指针离开图像时将 image1 放回去)。我要替换 image1 对象吗?我是否对该图像使用 setUrl 函数?我是否创建一个全新的图像,并使用水平面板的添加/删除功能来添加它?这看起来效率极低。我什至不需要ImageBundle吗?我可以通过 添加图像,然后使用 CSS 和悬停属性来交换图像?

一些指导会很好。 GWT 文档在这方面严重缺乏。谢谢。

i am using the declarative/ui-binder method of adding images to a page. this is in combination with using the ImageBundle functions that GWT provides.

what i would like to do is change the image out when i hover over the image. my questions are: what are the best way to do this, and is my current method the best method in the first place?

my code looks something similar to:

<ui:with field='res' type='path.to.my.app.AppResources' />
...
<g:HorizontalPanel ui:field='horizPanel' >
    <g:Image ui:field='image1' resource='{res.image1}'/>
</g:HorizontalPanel>

that is then tied into an ImageBundle class via the AbstractImagePrototype.

then, in my main handler, i have something like:

@UiHandler("image1")
public void onMouseOver(MouseOverEvent event)
{
    /* What do I do here? */
}

say i want to replace image1 with image2 when the user hovers over image1 (and put image1 back when the pointer leaves the image). do i replace the image1 object? do i use the setUrl function for that image? do i create a whole new image, and use the add/remove functions for the horizontal panel to add it on? that seems awfully inefficient. do i not even need an ImageBundle; can i add images via something like <g:Image .... url='path/to/image1.png' /> and then use CSS and the hover attribute to swap out the image?

some guidance would be great. the GWT documentation is seriously lacking in this area. thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〗斷ホ乔殘χμё〖 2024-12-25 14:13:49

PushButton 非常适合这种行为。您可以比 :hover 更进一步 - 您可以为按钮的不同面指定任意 html 和小部件。

有关 uibinder 中的示例,请参阅 UiBinder 中的 gwt PushButton

这种方法会产生更多的开销,因为它确实注册了鼠标处理程序并设置了整个小部件 - 如果您确实只需要翻转图像而不需要任何其他事件处理,则可以使用 :hover css 选择器(也许使用 @sprite?)可能是最好的。

PushButton is good for this kind of behavior. You can go father than :hover - you can specify arbitrary html and widgets for different faces of the buttons.

See gwt pushButton in UiBinder for an example in uibinder.

There will be more overhead in this approach, since it does register mouse handlers and set up a whole widget - if you really only need the rollover image and not any other event handling, a :hover css selector (maybe with a @sprite?) is probably best.

月野兔 2024-12-25 14:13:49

在这里使用鼠标处理程序似乎有点开销。我将使用 css 和悬停选择器,

.foo {
    background: url('path/to/image1.png');
    /* height, width, etc. */
}

.foo:hover {
    background: url('path/to/image2.png');
    /* ... */
}

然后使用呈现 div 元素(例如 SimplePanel)而不是图像的小部件,并将样式 foo 设置为 stylePrimaryName

<g:HorizontalPanel ui:field='horizPanel' >
    <g:SimplePanel ui:field='image1' stylePrimaryName='foo'/>
</g:HorizontalPanel>

Using mouse handlers here seems a little overhead. I would use css and the hover selector

.foo {
    background: url('path/to/image1.png');
    /* height, width, etc. */
}

.foo:hover {
    background: url('path/to/image2.png');
    /* ... */
}

then use a widget that renders a div element (e.g. SimplePanel) instead of an image and set the style foo as stylePrimaryName

<g:HorizontalPanel ui:field='horizPanel' >
    <g:SimplePanel ui:field='image1' stylePrimaryName='foo'/>
</g:HorizontalPanel>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文