diff --git a/crates/mdbook-html/front-end/templates/toc.js.hbs b/crates/mdbook-html/front-end/templates/toc.js.hbs
index 132da23cd3..1a9f751ccf 100644
--- a/crates/mdbook-html/front-end/templates/toc.js.hbs
+++ b/crates/mdbook-html/front-end/templates/toc.js.hbs
@@ -40,14 +40,22 @@ class MDBookSidebarScrollbox extends HTMLElement {
// Track and set sidebar scroll position
this.addEventListener('click', e => {
if (e.target.tagName === 'A') {
- sessionStorage.setItem('sidebar-scroll', this.scrollTop);
+ const clientRect = e.target.getBoundingClientRect();
+ const sidebarRect = this.getBoundingClientRect();
+ sessionStorage.setItem('sidebar-scroll-offset', clientRect.top - sidebarRect.top);
}
}, { passive: true });
- const sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
- sessionStorage.removeItem('sidebar-scroll');
- if (sidebarScrollTop) {
+ const sidebarScrollOffset = sessionStorage.getItem('sidebar-scroll-offset');
+ sessionStorage.removeItem('sidebar-scroll-offset');
+ if (sidebarScrollOffset !== null) {
// preserve sidebar scroll position when navigating via links within sidebar
- this.scrollTop = sidebarScrollTop;
+ const activeSection = this.querySelector('.active');
+ if (activeSection) {
+ const clientRect = activeSection.getBoundingClientRect();
+ const sidebarRect = this.getBoundingClientRect();
+ const currentOffset = clientRect.top - sidebarRect.top;
+ this.scrollTop += currentOffset - parseFloat(sidebarScrollOffset);
+ }
} else {
// scroll sidebar to current active section when navigating via
// 'next/previous chapter' buttons
diff --git a/tests/gui/books/sidebar-scroll/book.toml b/tests/gui/books/sidebar-scroll/book.toml
new file mode 100644
index 0000000000..794554f2b5
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/book.toml
@@ -0,0 +1,3 @@
+[book]
+title = "sidebar-scroll"
+language = "en"
diff --git a/tests/gui/books/sidebar-scroll/src/SUMMARY.md b/tests/gui/books/sidebar-scroll/src/SUMMARY.md
new file mode 100644
index 0000000000..6b040522d6
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/SUMMARY.md
@@ -0,0 +1,102 @@
+# Summary
+
+- [Chapter 1](./chapter_1.md)
+- [Chapter 2](./chapter_2.md)
+- [Chapter 3](./chapter_3.md)
+- [Chapter 4](./chapter_4.md)
+- [Chapter 5](./chapter_5.md)
+- [Chapter 6](./chapter_6.md)
+- [Chapter 7](./chapter_7.md)
+- [Chapter 8](./chapter_8.md)
+- [Chapter 9](./chapter_9.md)
+- [Chapter 10](./chapter_10.md)
+- [Chapter 11](./chapter_11.md)
+- [Chapter 12](./chapter_12.md)
+- [Chapter 13](./chapter_13.md)
+- [Chapter 14](./chapter_14.md)
+- [Chapter 15](./chapter_15.md)
+- [Chapter 16](./chapter_16.md)
+- [Chapter 17](./chapter_17.md)
+- [Chapter 18](./chapter_18.md)
+- [Chapter 19](./chapter_19.md)
+- [Chapter 20](./chapter_20.md)
+- [Chapter 21](./chapter_21.md)
+- [Chapter 22](./chapter_22.md)
+- [Chapter 23](./chapter_23.md)
+- [Chapter 24](./chapter_24.md)
+- [Chapter 25](./chapter_25.md)
+- [Chapter 26](./chapter_26.md)
+- [Chapter 27](./chapter_27.md)
+- [Chapter 28](./chapter_28.md)
+- [Chapter 29](./chapter_29.md)
+- [Chapter 30](./chapter_30.md)
+- [Chapter 31](./chapter_31.md)
+- [Chapter 32](./chapter_32.md)
+- [Chapter 33](./chapter_33.md)
+- [Chapter 34](./chapter_34.md)
+- [Chapter 35](./chapter_35.md)
+- [Chapter 36](./chapter_36.md)
+- [Chapter 37](./chapter_37.md)
+- [Chapter 38](./chapter_38.md)
+- [Chapter 39](./chapter_39.md)
+- [Chapter 40](./chapter_40.md)
+- [Chapter 41](./chapter_41.md)
+- [Chapter 42](./chapter_42.md)
+- [Chapter 43](./chapter_43.md)
+- [Chapter 44](./chapter_44.md)
+- [Chapter 45](./chapter_45.md)
+- [Chapter 46](./chapter_46.md)
+- [Chapter 47](./chapter_47.md)
+- [Chapter 48](./chapter_48.md)
+- [Chapter 49](./chapter_49.md)
+- [Chapter 50](./chapter_50.md)
+- [Chapter 51](./chapter_51.md)
+- [Chapter 52](./chapter_52.md)
+- [Chapter 53](./chapter_53.md)
+- [Chapter 54](./chapter_54.md)
+- [Chapter 55](./chapter_55.md)
+- [Chapter 56](./chapter_56.md)
+- [Chapter 57](./chapter_57.md)
+- [Chapter 58](./chapter_58.md)
+- [Chapter 59](./chapter_59.md)
+- [Chapter 60](./chapter_60.md)
+- [Chapter 61](./chapter_61.md)
+- [Chapter 62](./chapter_62.md)
+- [Chapter 63](./chapter_63.md)
+- [Chapter 64](./chapter_64.md)
+- [Chapter 65](./chapter_65.md)
+- [Chapter 66](./chapter_66.md)
+- [Chapter 67](./chapter_67.md)
+- [Chapter 68](./chapter_68.md)
+- [Chapter 69](./chapter_69.md)
+- [Chapter 70](./chapter_70.md)
+- [Chapter 71](./chapter_71.md)
+- [Chapter 72](./chapter_72.md)
+- [Chapter 73](./chapter_73.md)
+- [Chapter 74](./chapter_74.md)
+- [Chapter 75](./chapter_75.md)
+- [Chapter 76](./chapter_76.md)
+- [Chapter 77](./chapter_77.md)
+- [Chapter 78](./chapter_78.md)
+- [Chapter 79](./chapter_79.md)
+- [Chapter 80](./chapter_80.md)
+- [Chapter 81](./chapter_81.md)
+- [Chapter 82](./chapter_82.md)
+- [Chapter 83](./chapter_83.md)
+- [Chapter 84](./chapter_84.md)
+- [Chapter 85](./chapter_85.md)
+- [Chapter 86](./chapter_86.md)
+- [Chapter 87](./chapter_87.md)
+- [Chapter 88](./chapter_88.md)
+- [Chapter 89](./chapter_89.md)
+- [Chapter 90](./chapter_90.md)
+- [Chapter 91](./chapter_91.md)
+- [Chapter 92](./chapter_92.md)
+- [Chapter 93](./chapter_93.md)
+- [Chapter 94](./chapter_94.md)
+- [Chapter 95](./chapter_95.md)
+- [Chapter 96](./chapter_96.md)
+- [Chapter 97](./chapter_97.md)
+- [Chapter 98](./chapter_98.md)
+- [Chapter 99](./chapter_99.md)
+- [Chapter 100](./chapter_100.md)
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_1.md b/tests/gui/books/sidebar-scroll/src/chapter_1.md
new file mode 100644
index 0000000000..a967fa0bbc
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_1.md
@@ -0,0 +1,3 @@
+# Chapter 1
+
+## This has a single heading
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_10.md b/tests/gui/books/sidebar-scroll/src/chapter_10.md
new file mode 100644
index 0000000000..1782a9abd0
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_10.md
@@ -0,0 +1,28 @@
+# Chapter 10
+
+## Heading A
+## Heading B
+## Heading C
+## Heading D
+## Heading E
+## Heading F
+## Heading G
+## Heading H
+## Heading I
+## Heading J
+## Heading K
+## Heading L
+## Heading M
+## Heading N
+## Heading O
+## Heading P
+## Heading Q
+## Heading R
+## Heading S
+## Heading T
+## Heading U
+## Heading V
+## Heading W
+## Heading X
+## Heading Y
+## Heading Z
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_100.md b/tests/gui/books/sidebar-scroll/src/chapter_100.md
new file mode 100644
index 0000000000..539cf9d890
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_100.md
@@ -0,0 +1 @@
+# Chapter 100
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_11.md b/tests/gui/books/sidebar-scroll/src/chapter_11.md
new file mode 100644
index 0000000000..b1460153b6
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_11.md
@@ -0,0 +1 @@
+# Chapter 11
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_12.md b/tests/gui/books/sidebar-scroll/src/chapter_12.md
new file mode 100644
index 0000000000..f94a17c658
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_12.md
@@ -0,0 +1 @@
+# Chapter 12
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_13.md b/tests/gui/books/sidebar-scroll/src/chapter_13.md
new file mode 100644
index 0000000000..71cdd1332f
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_13.md
@@ -0,0 +1 @@
+# Chapter 13
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_14.md b/tests/gui/books/sidebar-scroll/src/chapter_14.md
new file mode 100644
index 0000000000..106b886cd0
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_14.md
@@ -0,0 +1 @@
+# Chapter 14
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_15.md b/tests/gui/books/sidebar-scroll/src/chapter_15.md
new file mode 100644
index 0000000000..363dc09552
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_15.md
@@ -0,0 +1 @@
+# Chapter 15
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_16.md b/tests/gui/books/sidebar-scroll/src/chapter_16.md
new file mode 100644
index 0000000000..0100dcd51b
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_16.md
@@ -0,0 +1 @@
+# Chapter 16
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_17.md b/tests/gui/books/sidebar-scroll/src/chapter_17.md
new file mode 100644
index 0000000000..517bed75f1
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_17.md
@@ -0,0 +1 @@
+# Chapter 17
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_18.md b/tests/gui/books/sidebar-scroll/src/chapter_18.md
new file mode 100644
index 0000000000..8d9aa8d11a
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_18.md
@@ -0,0 +1 @@
+# Chapter 18
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_19.md b/tests/gui/books/sidebar-scroll/src/chapter_19.md
new file mode 100644
index 0000000000..9dc3a8e141
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_19.md
@@ -0,0 +1 @@
+# Chapter 19
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_2.md b/tests/gui/books/sidebar-scroll/src/chapter_2.md
new file mode 100644
index 0000000000..7ebb5961e3
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_2.md
@@ -0,0 +1 @@
+# Chapter 2
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_20.md b/tests/gui/books/sidebar-scroll/src/chapter_20.md
new file mode 100644
index 0000000000..07c0529ae1
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_20.md
@@ -0,0 +1 @@
+# Chapter 20
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_21.md b/tests/gui/books/sidebar-scroll/src/chapter_21.md
new file mode 100644
index 0000000000..787f188ddd
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_21.md
@@ -0,0 +1 @@
+# Chapter 21
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_22.md b/tests/gui/books/sidebar-scroll/src/chapter_22.md
new file mode 100644
index 0000000000..991d6f6c96
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_22.md
@@ -0,0 +1 @@
+# Chapter 22
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_23.md b/tests/gui/books/sidebar-scroll/src/chapter_23.md
new file mode 100644
index 0000000000..eb52bc07ee
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_23.md
@@ -0,0 +1 @@
+# Chapter 23
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_24.md b/tests/gui/books/sidebar-scroll/src/chapter_24.md
new file mode 100644
index 0000000000..30f939655b
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_24.md
@@ -0,0 +1 @@
+# Chapter 24
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_25.md b/tests/gui/books/sidebar-scroll/src/chapter_25.md
new file mode 100644
index 0000000000..87c3904a8f
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_25.md
@@ -0,0 +1 @@
+# Chapter 25
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_26.md b/tests/gui/books/sidebar-scroll/src/chapter_26.md
new file mode 100644
index 0000000000..7455e38fad
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_26.md
@@ -0,0 +1 @@
+# Chapter 26
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_27.md b/tests/gui/books/sidebar-scroll/src/chapter_27.md
new file mode 100644
index 0000000000..7d1f1ccfc3
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_27.md
@@ -0,0 +1 @@
+# Chapter 27
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_28.md b/tests/gui/books/sidebar-scroll/src/chapter_28.md
new file mode 100644
index 0000000000..227da59048
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_28.md
@@ -0,0 +1 @@
+# Chapter 28
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_29.md b/tests/gui/books/sidebar-scroll/src/chapter_29.md
new file mode 100644
index 0000000000..25c7537878
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_29.md
@@ -0,0 +1 @@
+# Chapter 29
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_3.md b/tests/gui/books/sidebar-scroll/src/chapter_3.md
new file mode 100644
index 0000000000..10285d6150
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_3.md
@@ -0,0 +1 @@
+# Chapter 3
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_30.md b/tests/gui/books/sidebar-scroll/src/chapter_30.md
new file mode 100644
index 0000000000..d9227ccdfd
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_30.md
@@ -0,0 +1 @@
+# Chapter 30
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_31.md b/tests/gui/books/sidebar-scroll/src/chapter_31.md
new file mode 100644
index 0000000000..5b2acacc8e
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_31.md
@@ -0,0 +1 @@
+# Chapter 31
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_32.md b/tests/gui/books/sidebar-scroll/src/chapter_32.md
new file mode 100644
index 0000000000..a780a8d3fa
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_32.md
@@ -0,0 +1 @@
+# Chapter 32
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_33.md b/tests/gui/books/sidebar-scroll/src/chapter_33.md
new file mode 100644
index 0000000000..6990c08181
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_33.md
@@ -0,0 +1 @@
+# Chapter 33
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_34.md b/tests/gui/books/sidebar-scroll/src/chapter_34.md
new file mode 100644
index 0000000000..5428767af3
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_34.md
@@ -0,0 +1 @@
+# Chapter 34
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_35.md b/tests/gui/books/sidebar-scroll/src/chapter_35.md
new file mode 100644
index 0000000000..ac576b92d5
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_35.md
@@ -0,0 +1,28 @@
+# Chapter 35
+
+## Heading A
+## Heading B
+## Heading C
+## Heading D
+## Heading E
+## Heading F
+## Heading G
+## Heading H
+## Heading I
+## Heading J
+## Heading K
+## Heading L
+## Heading M
+## Heading N
+## Heading O
+## Heading P
+## Heading Q
+## Heading R
+## Heading S
+## Heading T
+## Heading U
+## Heading V
+## Heading W
+## Heading X
+## Heading Y
+## Heading Z
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_36.md b/tests/gui/books/sidebar-scroll/src/chapter_36.md
new file mode 100644
index 0000000000..ce6dae66e0
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_36.md
@@ -0,0 +1 @@
+# Chapter 36
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_37.md b/tests/gui/books/sidebar-scroll/src/chapter_37.md
new file mode 100644
index 0000000000..f9a17af18b
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_37.md
@@ -0,0 +1 @@
+# Chapter 37
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_38.md b/tests/gui/books/sidebar-scroll/src/chapter_38.md
new file mode 100644
index 0000000000..a4d305bf10
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_38.md
@@ -0,0 +1 @@
+# Chapter 38
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_39.md b/tests/gui/books/sidebar-scroll/src/chapter_39.md
new file mode 100644
index 0000000000..c7ce7c9f1a
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_39.md
@@ -0,0 +1 @@
+# Chapter 39
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_4.md b/tests/gui/books/sidebar-scroll/src/chapter_4.md
new file mode 100644
index 0000000000..335fe90a69
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_4.md
@@ -0,0 +1 @@
+# Chapter 4
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_40.md b/tests/gui/books/sidebar-scroll/src/chapter_40.md
new file mode 100644
index 0000000000..9755a0ac91
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_40.md
@@ -0,0 +1 @@
+# Chapter 40
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_41.md b/tests/gui/books/sidebar-scroll/src/chapter_41.md
new file mode 100644
index 0000000000..64da80508a
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_41.md
@@ -0,0 +1 @@
+# Chapter 41
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_42.md b/tests/gui/books/sidebar-scroll/src/chapter_42.md
new file mode 100644
index 0000000000..2fada5e866
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_42.md
@@ -0,0 +1 @@
+# Chapter 42
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_43.md b/tests/gui/books/sidebar-scroll/src/chapter_43.md
new file mode 100644
index 0000000000..ed72a8ed58
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_43.md
@@ -0,0 +1 @@
+# Chapter 43
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_44.md b/tests/gui/books/sidebar-scroll/src/chapter_44.md
new file mode 100644
index 0000000000..9672a10e34
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_44.md
@@ -0,0 +1 @@
+# Chapter 44
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_45.md b/tests/gui/books/sidebar-scroll/src/chapter_45.md
new file mode 100644
index 0000000000..a38c5f08df
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_45.md
@@ -0,0 +1 @@
+# Chapter 45
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_46.md b/tests/gui/books/sidebar-scroll/src/chapter_46.md
new file mode 100644
index 0000000000..3a8cb25778
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_46.md
@@ -0,0 +1 @@
+# Chapter 46
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_47.md b/tests/gui/books/sidebar-scroll/src/chapter_47.md
new file mode 100644
index 0000000000..d09aac1ad9
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_47.md
@@ -0,0 +1 @@
+# Chapter 47
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_48.md b/tests/gui/books/sidebar-scroll/src/chapter_48.md
new file mode 100644
index 0000000000..d7335334cf
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_48.md
@@ -0,0 +1 @@
+# Chapter 48
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_49.md b/tests/gui/books/sidebar-scroll/src/chapter_49.md
new file mode 100644
index 0000000000..4b4e0a0f37
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_49.md
@@ -0,0 +1 @@
+# Chapter 49
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_5.md b/tests/gui/books/sidebar-scroll/src/chapter_5.md
new file mode 100644
index 0000000000..ca74340133
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_5.md
@@ -0,0 +1 @@
+# Chapter 5
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_50.md b/tests/gui/books/sidebar-scroll/src/chapter_50.md
new file mode 100644
index 0000000000..03c8b2a011
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_50.md
@@ -0,0 +1 @@
+# Chapter 50
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_51.md b/tests/gui/books/sidebar-scroll/src/chapter_51.md
new file mode 100644
index 0000000000..ee21cb7689
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_51.md
@@ -0,0 +1 @@
+# Chapter 51
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_52.md b/tests/gui/books/sidebar-scroll/src/chapter_52.md
new file mode 100644
index 0000000000..154b47d7aa
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_52.md
@@ -0,0 +1 @@
+# Chapter 52
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_53.md b/tests/gui/books/sidebar-scroll/src/chapter_53.md
new file mode 100644
index 0000000000..38f1b033f6
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_53.md
@@ -0,0 +1 @@
+# Chapter 53
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_54.md b/tests/gui/books/sidebar-scroll/src/chapter_54.md
new file mode 100644
index 0000000000..d0c1db4385
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_54.md
@@ -0,0 +1 @@
+# Chapter 54
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_55.md b/tests/gui/books/sidebar-scroll/src/chapter_55.md
new file mode 100644
index 0000000000..5110a173b7
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_55.md
@@ -0,0 +1 @@
+# Chapter 55
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_56.md b/tests/gui/books/sidebar-scroll/src/chapter_56.md
new file mode 100644
index 0000000000..37f1c9bee6
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_56.md
@@ -0,0 +1 @@
+# Chapter 56
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_57.md b/tests/gui/books/sidebar-scroll/src/chapter_57.md
new file mode 100644
index 0000000000..ad451b8481
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_57.md
@@ -0,0 +1 @@
+# Chapter 57
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_58.md b/tests/gui/books/sidebar-scroll/src/chapter_58.md
new file mode 100644
index 0000000000..4cd1e6783b
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_58.md
@@ -0,0 +1 @@
+# Chapter 58
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_59.md b/tests/gui/books/sidebar-scroll/src/chapter_59.md
new file mode 100644
index 0000000000..196170f1dd
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_59.md
@@ -0,0 +1 @@
+# Chapter 59
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_6.md b/tests/gui/books/sidebar-scroll/src/chapter_6.md
new file mode 100644
index 0000000000..69ed9d36d8
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_6.md
@@ -0,0 +1 @@
+# Chapter 6
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_60.md b/tests/gui/books/sidebar-scroll/src/chapter_60.md
new file mode 100644
index 0000000000..0363273ef8
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_60.md
@@ -0,0 +1 @@
+# Chapter 60
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_61.md b/tests/gui/books/sidebar-scroll/src/chapter_61.md
new file mode 100644
index 0000000000..f6f3a9ae81
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_61.md
@@ -0,0 +1 @@
+# Chapter 61
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_62.md b/tests/gui/books/sidebar-scroll/src/chapter_62.md
new file mode 100644
index 0000000000..35f632e3d6
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_62.md
@@ -0,0 +1 @@
+# Chapter 62
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_63.md b/tests/gui/books/sidebar-scroll/src/chapter_63.md
new file mode 100644
index 0000000000..092d8d4c4d
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_63.md
@@ -0,0 +1 @@
+# Chapter 63
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_64.md b/tests/gui/books/sidebar-scroll/src/chapter_64.md
new file mode 100644
index 0000000000..0356c56a6e
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_64.md
@@ -0,0 +1 @@
+# Chapter 64
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_65.md b/tests/gui/books/sidebar-scroll/src/chapter_65.md
new file mode 100644
index 0000000000..115d9d3b55
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_65.md
@@ -0,0 +1 @@
+# Chapter 65
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_66.md b/tests/gui/books/sidebar-scroll/src/chapter_66.md
new file mode 100644
index 0000000000..987bb2da16
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_66.md
@@ -0,0 +1 @@
+# Chapter 66
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_67.md b/tests/gui/books/sidebar-scroll/src/chapter_67.md
new file mode 100644
index 0000000000..dfdfa25051
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_67.md
@@ -0,0 +1 @@
+# Chapter 67
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_68.md b/tests/gui/books/sidebar-scroll/src/chapter_68.md
new file mode 100644
index 0000000000..f19dbc82d1
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_68.md
@@ -0,0 +1 @@
+# Chapter 68
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_69.md b/tests/gui/books/sidebar-scroll/src/chapter_69.md
new file mode 100644
index 0000000000..ced33c35fd
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_69.md
@@ -0,0 +1 @@
+# Chapter 69
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_7.md b/tests/gui/books/sidebar-scroll/src/chapter_7.md
new file mode 100644
index 0000000000..5f01a3ed0e
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_7.md
@@ -0,0 +1 @@
+# Chapter 7
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_70.md b/tests/gui/books/sidebar-scroll/src/chapter_70.md
new file mode 100644
index 0000000000..dc98b12f8c
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_70.md
@@ -0,0 +1 @@
+# Chapter 70
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_71.md b/tests/gui/books/sidebar-scroll/src/chapter_71.md
new file mode 100644
index 0000000000..275e01d173
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_71.md
@@ -0,0 +1 @@
+# Chapter 71
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_72.md b/tests/gui/books/sidebar-scroll/src/chapter_72.md
new file mode 100644
index 0000000000..6e71daacd1
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_72.md
@@ -0,0 +1 @@
+# Chapter 72
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_73.md b/tests/gui/books/sidebar-scroll/src/chapter_73.md
new file mode 100644
index 0000000000..bf0fdabcd2
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_73.md
@@ -0,0 +1 @@
+# Chapter 73
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_74.md b/tests/gui/books/sidebar-scroll/src/chapter_74.md
new file mode 100644
index 0000000000..58d0e8a82f
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_74.md
@@ -0,0 +1 @@
+# Chapter 74
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_75.md b/tests/gui/books/sidebar-scroll/src/chapter_75.md
new file mode 100644
index 0000000000..a4eac49b3c
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_75.md
@@ -0,0 +1 @@
+# Chapter 75
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_76.md b/tests/gui/books/sidebar-scroll/src/chapter_76.md
new file mode 100644
index 0000000000..303a67e479
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_76.md
@@ -0,0 +1 @@
+# Chapter 76
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_77.md b/tests/gui/books/sidebar-scroll/src/chapter_77.md
new file mode 100644
index 0000000000..531ce5b282
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_77.md
@@ -0,0 +1 @@
+# Chapter 77
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_78.md b/tests/gui/books/sidebar-scroll/src/chapter_78.md
new file mode 100644
index 0000000000..e4557c80cf
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_78.md
@@ -0,0 +1 @@
+# Chapter 78
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_79.md b/tests/gui/books/sidebar-scroll/src/chapter_79.md
new file mode 100644
index 0000000000..67921217bb
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_79.md
@@ -0,0 +1 @@
+# Chapter 79
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_8.md b/tests/gui/books/sidebar-scroll/src/chapter_8.md
new file mode 100644
index 0000000000..7c0cc60cc7
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_8.md
@@ -0,0 +1 @@
+# Chapter 8
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_80.md b/tests/gui/books/sidebar-scroll/src/chapter_80.md
new file mode 100644
index 0000000000..36791e7fb8
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_80.md
@@ -0,0 +1 @@
+# Chapter 80
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_81.md b/tests/gui/books/sidebar-scroll/src/chapter_81.md
new file mode 100644
index 0000000000..402a60a301
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_81.md
@@ -0,0 +1 @@
+# Chapter 81
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_82.md b/tests/gui/books/sidebar-scroll/src/chapter_82.md
new file mode 100644
index 0000000000..f16a7cabcc
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_82.md
@@ -0,0 +1 @@
+# Chapter 82
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_83.md b/tests/gui/books/sidebar-scroll/src/chapter_83.md
new file mode 100644
index 0000000000..a4c2e2360b
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_83.md
@@ -0,0 +1 @@
+# Chapter 83
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_84.md b/tests/gui/books/sidebar-scroll/src/chapter_84.md
new file mode 100644
index 0000000000..ad85ccf093
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_84.md
@@ -0,0 +1 @@
+# Chapter 84
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_85.md b/tests/gui/books/sidebar-scroll/src/chapter_85.md
new file mode 100644
index 0000000000..ae34199bdc
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_85.md
@@ -0,0 +1 @@
+# Chapter 85
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_86.md b/tests/gui/books/sidebar-scroll/src/chapter_86.md
new file mode 100644
index 0000000000..b0ec006a51
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_86.md
@@ -0,0 +1 @@
+# Chapter 86
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_87.md b/tests/gui/books/sidebar-scroll/src/chapter_87.md
new file mode 100644
index 0000000000..1f3cfd5aa5
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_87.md
@@ -0,0 +1 @@
+# Chapter 87
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_88.md b/tests/gui/books/sidebar-scroll/src/chapter_88.md
new file mode 100644
index 0000000000..1ba9c418ff
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_88.md
@@ -0,0 +1 @@
+# Chapter 88
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_89.md b/tests/gui/books/sidebar-scroll/src/chapter_89.md
new file mode 100644
index 0000000000..cb75ae072a
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_89.md
@@ -0,0 +1 @@
+# Chapter 89
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_9.md b/tests/gui/books/sidebar-scroll/src/chapter_9.md
new file mode 100644
index 0000000000..a92c65b200
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_9.md
@@ -0,0 +1 @@
+# Chapter 9
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_90.md b/tests/gui/books/sidebar-scroll/src/chapter_90.md
new file mode 100644
index 0000000000..18285bc2bf
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_90.md
@@ -0,0 +1 @@
+# Chapter 90
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_91.md b/tests/gui/books/sidebar-scroll/src/chapter_91.md
new file mode 100644
index 0000000000..821bf3f126
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_91.md
@@ -0,0 +1 @@
+# Chapter 91
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_92.md b/tests/gui/books/sidebar-scroll/src/chapter_92.md
new file mode 100644
index 0000000000..64f1d3dd8a
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_92.md
@@ -0,0 +1 @@
+# Chapter 92
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_93.md b/tests/gui/books/sidebar-scroll/src/chapter_93.md
new file mode 100644
index 0000000000..f3670c6bb3
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_93.md
@@ -0,0 +1 @@
+# Chapter 93
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_94.md b/tests/gui/books/sidebar-scroll/src/chapter_94.md
new file mode 100644
index 0000000000..405cbf069b
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_94.md
@@ -0,0 +1 @@
+# Chapter 94
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_95.md b/tests/gui/books/sidebar-scroll/src/chapter_95.md
new file mode 100644
index 0000000000..b34e0515ae
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_95.md
@@ -0,0 +1 @@
+# Chapter 95
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_96.md b/tests/gui/books/sidebar-scroll/src/chapter_96.md
new file mode 100644
index 0000000000..5ec0088295
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_96.md
@@ -0,0 +1 @@
+# Chapter 96
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_97.md b/tests/gui/books/sidebar-scroll/src/chapter_97.md
new file mode 100644
index 0000000000..a8e826b272
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_97.md
@@ -0,0 +1 @@
+# Chapter 97
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_98.md b/tests/gui/books/sidebar-scroll/src/chapter_98.md
new file mode 100644
index 0000000000..7a544ee33a
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_98.md
@@ -0,0 +1 @@
+# Chapter 98
diff --git a/tests/gui/books/sidebar-scroll/src/chapter_99.md b/tests/gui/books/sidebar-scroll/src/chapter_99.md
new file mode 100644
index 0000000000..1f8b5c59df
--- /dev/null
+++ b/tests/gui/books/sidebar-scroll/src/chapter_99.md
@@ -0,0 +1 @@
+# Chapter 99
diff --git a/tests/gui/sidebar-scroll.goml b/tests/gui/sidebar-scroll.goml
new file mode 100644
index 0000000000..3a388e81bf
--- /dev/null
+++ b/tests/gui/sidebar-scroll.goml
@@ -0,0 +1,65 @@
+// This GUI test checks the sidebar scroll position when navigating.
+
+store-value: (windowHeight, 900)
+set-window-size: (1200, |windowHeight|)
+go-to: |DOC_PATH| + "sidebar-scroll/index.html"
+assert-property: ("mdbook-sidebar-scrollbox", {"scrollTop": 0})
+
+click: ".chapter a[href='chapter_2.html']"
+assert-text: ("title", "Chapter 2 - sidebar-scroll")
+assert-property: ("mdbook-sidebar-scrollbox", {"scrollTop": 0})
+
+click: ".chapter a[href='chapter_10.html']"
+assert-text: ("title", "Chapter 10 - sidebar-scroll")
+assert-property: ("mdbook-sidebar-scrollbox", {"scrollTop": 0})
+
+// Check that heading nav of chapter 10 pushes 11 off the bottom.
+store-position: (".chapter a[href='chapter_11.html']", {"y": chapter_y})
+assert: |chapter_y| > |windowHeight|
+
+// Clicking 11 should scroll back up because it is not possible for it to stay
+// in position since there are only a few lines above.
+click: ".chapter a[href='chapter_11.html']"
+assert-text: ("title", "Chapter 11 - sidebar-scroll")
+assert-property: ("mdbook-sidebar-scrollbox", {"scrollTop": 0})
+
+// Scroll down a little, and click on a chapter that can stay in place when
+// clicked.
+scroll-element-to: ("mdbook-sidebar-scrollbox", (0, 230))
+store-property: ("mdbook-sidebar-scrollbox", {"scrollTop": sidebarScrollTop})
+assert: |sidebarScrollTop| > 0
+click: ".chapter a[href='chapter_35.html']"
+assert-text: ("title", "Chapter 35 - sidebar-scroll")
+assert-property: ("mdbook-sidebar-scrollbox", {"scrollTop": |sidebarScrollTop|})
+
+// Go to the next chapter, and verify that it scrolls to the middle.
+press-key: "ArrowRight"
+assert-text: ("title", "Chapter 36 - sidebar-scroll")
+// The active link should be roughly in the middle of the screen.
+store-position: (".chapter a.active", {"y": active_y})
+// Approximate check just in case the browser has slight rendering differences.
+assert: |active_y| > 400
+assert: |active_y| < 450
+
+// Go to something near the top, it shouldn't scroll.
+store-property: ("mdbook-sidebar-scrollbox", {"scrollTop": sidebarScrollTop})
+click: ".chapter a[href='chapter_24.html']"
+assert-text: ("title", "Chapter 24 - sidebar-scroll")
+assert-property: ("mdbook-sidebar-scrollbox", {"scrollTop": |sidebarScrollTop|})
+
+// Go to the last chapter, and verify it is scrolled to the bottom.
+go-to: |DOC_PATH| + "sidebar-scroll/chapter_100.html"
+store-property: ("mdbook-sidebar-scrollbox", {"scrollTop": scrollTop, "scrollHeight": scrollHeight, "clientHeight": clientHeight})
+// This needs to be approximate since there is a slight difference.
+assert: |scrollTop| >= |scrollHeight| - |clientHeight| - 1
+
+// Clicking upwards shouldn't scroll.
+store-property: ("mdbook-sidebar-scrollbox", {"scrollTop": sidebarScrollTop})
+click: ".chapter a[href='chapter_97.html']"
+assert-text: ("title", "Chapter 97 - sidebar-scroll")
+assert-property: ("mdbook-sidebar-scrollbox", {"scrollTop": |sidebarScrollTop|})
+
+store-property: ("mdbook-sidebar-scrollbox", {"scrollTop": sidebarScrollTop})
+click: ".chapter a[href='chapter_76.html']"
+assert-text: ("title", "Chapter 76 - sidebar-scroll")
+assert-property: ("mdbook-sidebar-scrollbox", {"scrollTop": |sidebarScrollTop|})