Skip to content

Commit 78c4b14

Browse files
committed
Refactor sidebar scroll handling and add auto-scroll when user accesses a section on the sidebar that is not visible in the current Scroll Position.
1 parent ba368bf commit 78c4b14

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

WebApplication2/Site.Master

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,38 @@
1010
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/boxicons@latest/css/boxicons.min.css">
1111
<link rel="stylesheet" href="https://unpkg.com/boxicons@latest/css/boxicons.min.css">
1212
<link rel="stylesheet" type="text/css" href="~/Styles/site.css" />
13-
<script>
14-
document.addEventListener("DOMContentLoaded", function() {
15-
var sidebar = document.getElementById("sidebar");
16-
if (sidebar) {
17-
// Restore scroll position
18-
var scrollPosition = sessionStorage.getItem("sidebarScrollPosition");
19-
if (scrollPosition) {
20-
sidebar.scrollTop = scrollPosition;
21-
}
22-
23-
// Save scroll position
24-
sidebar.addEventListener("scroll", function () {
25-
sessionStorage.setItem("sidebarScrollPosition", sidebar.scrollTop);
26-
});
13+
<script>
14+
document.addEventListener("DOMContentLoaded", function () {
15+
var sidebar = document.getElementById("sidebar");
16+
if (sidebar) {
17+
// Restore scroll position
18+
var scrollPosition = sessionStorage.getItem("sidebarScrollPosition");
19+
if (scrollPosition) {
20+
sidebar.scrollTop = scrollPosition;
2721
}
28-
});
2922

30-
// Function to clear scroll position
31-
function clearScrollPosition() {
32-
sessionStorage.removeItem("sidebarScrollPosition");
23+
// Save scroll position
24+
sidebar.addEventListener("scroll", function () {
25+
sessionStorage.setItem("sidebarScrollPosition", sidebar.scrollTop);
26+
});
27+
28+
// Scroll to the active button if not visible
29+
var activeButton = sidebar.querySelector("li.active a");
30+
if (activeButton) {
31+
var rect = activeButton.getBoundingClientRect();
32+
var sidebarRect = sidebar.getBoundingClientRect();
33+
if (rect.top < sidebarRect.top || rect.bottom > sidebarRect.bottom) {
34+
activeButton.scrollIntoView({ behavior: "smooth", block: "center" });
35+
}
36+
}
3337
}
34-
</script>
38+
});
39+
40+
// Function to clear scroll position
41+
function clearScrollPosition() {
42+
sessionStorage.removeItem("sidebarScrollPosition");
43+
}
44+
</script>
3545
</head>
3646
<body>
3747
<form id="form1" runat="server">

0 commit comments

Comments
 (0)