gwt anchor tag not anchoring

夙愿已清 提交于 2019-12-11 08:38:32

问题


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?


回答1:


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.....




回答2:


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.




回答3:


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();
  }
}


来源:https://stackoverflow.com/questions/1586463/gwt-anchor-tag-not-anchoring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!