Skip to content

Commit 77f3366

Browse files
authored
[TASK] Improve scrolling behavior & fix header position (#927)
Ensured scrolling to main sections moves to the top of the page. Adjusted anchor scrolling so subsections are positioned correctly. Fixed the header to the top while scrolling. Added scroll-margin-top to prevent content from being hidden under the header. Resolves #845 Resolves #843
1 parent 6fa584b commit 77f3366

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
document.addEventListener("DOMContentLoaded", function () {
2+
function adjustScrollMargin() {
3+
const header = document.querySelector("header");
4+
const headerHeight = header ? header.offsetHeight : 80; // Default fallback
5+
6+
document.querySelectorAll(".section").forEach(section => {
7+
section.style.scrollMarginTop = `${headerHeight + 10}px`; // Extra space for visibility
8+
});
9+
}
10+
11+
function scrollToAnchor() {
12+
const hash = window.location.hash.substring(1); // Get anchor without #
13+
if (!hash) return;
14+
15+
const target = document.getElementById(hash);
16+
if (target) {
17+
// Check if the target is the first main section (h1)
18+
const isFirstSection = target.closest(".section")?.querySelector("h1") !== null;
19+
20+
if (isFirstSection) {
21+
window.scrollTo({
22+
top: 0,
23+
behavior: "smooth"
24+
});
25+
} else {
26+
setTimeout(() => {
27+
target.scrollIntoView({ behavior: "smooth", block: "start" });
28+
}, 50); // Delay ensures styles apply
29+
}
30+
}
31+
}
32+
33+
// Adjust scroll margin on load
34+
adjustScrollMargin();
35+
36+
// Handle initial anchor scrolling
37+
setTimeout(scrollToAnchor, 100); // Slight delay for rendering
38+
39+
// Handle clicks on internal links
40+
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
41+
anchor.addEventListener("click", function (e) {
42+
e.preventDefault();
43+
const targetId = this.getAttribute("href").substring(1);
44+
history.pushState(null, null, `#${targetId}`); // Update URL without reload
45+
scrollToAnchor();
46+
});
47+
});
48+
49+
// Adjust when resizing (e.g., switching between desktop & mobile)
50+
window.addEventListener("resize", adjustScrollMargin);
51+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
header {
2+
position: sticky;
3+
top: 0;
4+
width: 100%;
5+
z-index: 1000; /* Ensures it's above other content */
6+
background-color: white; /* Adjust as needed */
7+
}
8+
9+
.section {
10+
scroll-margin-top: 114px; /* Adjust to match the header height */
11+
}

packages/typo3-docs-theme/assets/sass/theme.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464

6565
// Page structure
6666
@import 'layout/breadcrumb';
67+
@import 'layout/header';
6768
@import 'layout/footer';
6869
@import 'layout/logo';
6970
@import 'layout/navigation/main_navigation';

packages/typo3-docs-theme/resources/public/css/theme.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25853,6 +25853,18 @@ dl.field-list > dt:after {
2585325853
}
2585425854
}
2585525855

25856+
header {
25857+
position: sticky;
25858+
top: 0;
25859+
width: 100%;
25860+
z-index: 1000; /* Ensures it's above other content */
25861+
background-color: white; /* Adjust as needed */
25862+
}
25863+
25864+
.section {
25865+
scroll-margin-top: 114px; /* Adjust to match the header height */
25866+
}
25867+
2585625868
@media (min-width: 768px) {
2585725869
.footer-main {
2585825870
margin: calc(40px / 2 * -1);

packages/typo3-docs-theme/resources/public/js/theme.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)