Skip to content

Commit

Permalink
fix: scroll to top osm post (#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiclasNorin authored Feb 13, 2024
1 parent 8aad038 commit f7d9481
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions source/js/openstreetmap/showPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,28 @@ class ShowPost {
private scrollToTop() {
if (!this.sidebar) return;
const rect = this.sidebar.getBoundingClientRect();
let offset = 0;
const topPos = window.pageYOffset || document.documentElement.scrollTop;

if (document.querySelector('.c-header--sticky')) {
const header = document.querySelector('.c-header--sticky');
if (!header) return;
const headerRect = header.getBoundingClientRect();
offset = headerRect.height ?? 100;
}
const offset = this.getStickyHeaderWithMostHeight();

const target = rect.top + topPos - offset;

window.scrollTo({
top: target,
})
}

private getStickyHeaderWithMostHeight() {
let offset = 0;
[...document.querySelectorAll('.c-header--sticky')].forEach(header => {
const headerRect = header.getBoundingClientRect();
if (headerRect.height > offset) {
offset = headerRect.height;
}
});

return offset;
}

private handleBackButton() {
if (!this.sidebar) return;

Expand Down

0 comments on commit f7d9481

Please sign in to comment.