如何在jhipster中添加滚动顶部按钮?

时间:2018-08-31 05:28:27

标签: jhipster

如何在Jhipster中创建“滚动顶部”或“返回顶部”按钮。

当我要访问导航栏时,我想要一个快速操作来避免向上滚动所有页面。

1 个答案:

答案 0 :(得分:0)

这是在页面侧面使用“转到顶部”按钮的方法。 当您不在顶部时,该按钮可见。

src/main/webapp/app/layouts/scroll-top/scroll-top.component.ts

ngOnInit() {
  // When the user scrolls down 20px from the top of the document, show the button
  window.addEventListener('scroll', this.scroll, true);
}

scroll = (): void => {
  // handle your scroll here
  // notice the 'odd' function assignment to a class field
  // this is used to be able to remove the event listener
  // console.log('scroll', document.body.scrollTop, document.documentElement.scrollTop);
  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
    document.getElementById('goTopBtn').style.display = 'block';
  } else {
    document.getElementById('goTopBtn').style.display = 'none';
  }
};

// When the user clicks on the button, scroll to the top of the document
goTop() {
  document.body.scrollTop = 0; // For Safari
  document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}

src/main/webapp/app/layouts/scroll-top/scroll-top.component.html

    <button (click)="goTop()" id="goTopBtn" title="Go to top" class="btn btn-secondary">Top</button>

src/main/webapp/app/layouts/scroll-top/scroll-top.component.scss (仅定位,样式在带有引导程序类的html中)

#goTopBtn {
    display: none;
    /* Hidden by default */
    position: fixed;
    /* Fixed/sticky position */
    bottom: 20px;
    /* Place the button at the bottom of the page */
    right: 30px;
    /* Place the button 30px from the right */
    z-index: 99;
    /* Make sure it does not overlap */
}

src/main/webapp/app/layouts/main/main.component.html (在navbar之后添加)

<jhi-scroll-top></jhi-scroll-top>

src/main/webapp/app/app.module.ts (在ScrollTopComponent中添加declarations

declarations: [JhiMainComponent, NavbarComponent, ErrorComponent, PageRibbonComponent, ActiveMenuDirective, FooterComponent, ScrollTopComponent],

左待办事项:

  • I18N / Glyphicon。
  • 检查响应性和浏览器兼容性

谢谢