代码之家  ›  专栏  ›  技术社区  ›  antony.trupe

GWT锚标记未锚定

  •  1
  • antony.trupe  · 技术社区  · 16 年前

    为什么浏览器不滚动到锚?
    网址: 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);
    

    是因为锚定是在页面加载完成后创建的,所以浏览器在试图滚动到锚定时看不到它吗?

    3 回复  |  直到 16 年前
        1
  •  1
  •   o.k.w    16 年前

    试试这个:

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

    是的,您是对的,您的锚定是在页面加载后创建/插入的,所以……

        2
  •  1
  •   Justin Fagnani    16 年前

    您可以尝试使用element.scrollintoview(),它不仅可以滚动窗口,还可以滚动包含元素的DOM层次结构中的任何可滚动容器。

        3
  •  1
  •   antony.trupe    16 年前

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

    public class Foo extends Widget
    {
      Foo(){
      }
    
      @Override
      protected void onLoad(){
        super.onLoad();
        getElement().scrollIntoView();
      }
    }
    
    推荐文章