Вот мой способ иметь кнопку «перейти наверх» на боковой части страницы.
Кнопка видна, когда вы не наверху.
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],
Left TODO:
- I18N / Glyphicon.
- Проверка отзывчивости и совместимости браузера
Спасибо