|
10 | 10 | (function() {
|
11 | 11 |
|
12 | 12 | const rootPath = document.getElementById("rustdoc-vars").attributes["data-root-path"].value;
|
| 13 | +let oldScrollPosition = 0; |
13 | 14 |
|
14 | 15 | function createDirEntry(elem, parent, fullPath, hasFoundFile) {
|
15 | 16 | const name = document.createElement("div");
|
@@ -65,10 +66,24 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
|
65 | 66 | function toggleSidebar() {
|
66 | 67 | const child = this.children[0];
|
67 | 68 | if (child.innerText === ">") {
|
| 69 | + if (window.innerWidth < 701) { |
| 70 | + // This is to keep the scroll position on mobile. |
| 71 | + oldScrollPosition = window.scrollY; |
| 72 | + document.body.style.position = "fixed"; |
| 73 | + document.body.style.top = `-${oldScrollPosition}px`; |
| 74 | + } |
68 | 75 | addClass(document.documentElement, "source-sidebar-expanded");
|
69 | 76 | child.innerText = "<";
|
70 | 77 | updateLocalStorage("source-sidebar-show", "true");
|
71 | 78 | } else {
|
| 79 | + if (window.innerWidth < 701) { |
| 80 | + // This is to keep the scroll position on mobile. |
| 81 | + document.body.style.position = ""; |
| 82 | + document.body.style.top = ""; |
| 83 | + // The scroll position is lost when resetting the style, hence why we store it in |
| 84 | + // `oldScroll`. |
| 85 | + window.scrollTo(0, oldScrollPosition); |
| 86 | + } |
72 | 87 | removeClass(document.documentElement, "source-sidebar-expanded");
|
73 | 88 | child.innerText = ">";
|
74 | 89 | updateLocalStorage("source-sidebar-show", "false");
|
|
0 commit comments