gwt 锚标记未锚定

发布于 2024-08-08 08:07:47 字数 417 浏览 2 评论 0原文

为什么浏览器不滚动到锚点?
url:http://localhost:8080/index.html#myAnchor3

this.anchor1.setName("myAnchor1");
this.add(this.anchor1);
this.anchor2.setName("myAnchor2");
this.add(this.anchor2);
this.anchor3.setName("myAnchor3");
this.add(this.anchor3);

是否因为锚点是在页面加载完成后创建的,所以浏览器在尝试滚动到该锚点时看不到该锚点?

Why does the browser not scroll to the anchor?
url:http://localhost:8080/index.html#myAnchor3

this.anchor1.setName("myAnchor1");
this.add(this.anchor1);
this.anchor2.setName("myAnchor2");
this.add(this.anchor2);
this.anchor3.setName("myAnchor3");
this.add(this.anchor3);

Is it because the anchor is created after the page has finished loading, so the browser doesn't see the anchor when it tries to scroll to it?

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

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

发布评论

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

评论(3

故人如初 2024-08-15 08:07:47

试试这个:

this.anchor.setName("myAnchor");
this.add(this.anchor);
location.hash = '#myAnchor';

是的,你是对的,你的锚点是在页面加载后创建/插入的,所以......

Try this:

this.anchor.setName("myAnchor");
this.add(this.anchor);
location.hash = '#myAnchor';

And yes, you are right, your anchor was created/inserted after the page load, so well.....

时间海 2024-08-15 08:07:47

您可以尝试使用 Element.scrollIntoView(),它不仅会滚动窗口,还会滚动 DOM 层次结构中保存元素的任何可滚动容器。

You could try using Element.scrollIntoView(), which not only will scroll the window, but any scrollable container in the DOM hierarchy that holds the element.

乞讨 2024-08-15 08:07:47

必须重写 onLoad 方法,并在那里调用scrollIntoView,否则它会尝试滚动到尚未添加到 DOM 的对象。

public class Foo extends Widget
{
  Foo(){
  }

  @Override
  protected void onLoad(){
    super.onLoad();
    getElement().scrollIntoView();
  }
}

Had to override the onLoad method, and call scrollIntoView there, otherwise it was trying to scroll to an object that wasn't added to the DOM yet.

public class Foo extends Widget
{
  Foo(){
  }

  @Override
  protected void onLoad(){
    super.onLoad();
    getElement().scrollIntoView();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文