diff options
| author | Guillaume Gomez <guillaume.gomez@huawei.com> | 2022-06-29 23:21:26 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume.gomez@huawei.com> | 2022-06-30 16:08:45 +0200 |
| commit | 72f6322f8a50288a4bf96c9b20935f666a889621 (patch) | |
| tree | b607ce17368eb187fc6f5a89d0e116ac884b308a /src/librustdoc/html/static/js/source-script.js | |
| parent | 277b77a5de79076fda48d8952618fae99a8cd7a3 (diff) | |
| download | rust-72f6322f8a50288a4bf96c9b20935f666a889621.tar.gz rust-72f6322f8a50288a4bf96c9b20935f666a889621.zip | |
Fix scroll when source sidebar is open on mobile
Diffstat (limited to 'src/librustdoc/html/static/js/source-script.js')
| -rw-r--r-- | src/librustdoc/html/static/js/source-script.js | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/librustdoc/html/static/js/source-script.js b/src/librustdoc/html/static/js/source-script.js index 290c29d3141..acb1d8d7b5c 100644 --- a/src/librustdoc/html/static/js/source-script.js +++ b/src/librustdoc/html/static/js/source-script.js @@ -10,6 +10,7 @@ (function() { const rootPath = document.getElementById("rustdoc-vars").attributes["data-root-path"].value; +let oldScrollPosition = 0; function createDirEntry(elem, parent, fullPath, hasFoundFile) { const name = document.createElement("div"); @@ -65,10 +66,24 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) { function toggleSidebar() { const child = this.children[0]; if (child.innerText === ">") { + if (window.innerWidth < 701) { + // This is to keep the scroll position on mobile. + oldScrollPosition = window.scrollY; + document.body.style.position = "fixed"; + document.body.style.top = `-${oldScrollPosition}px`; + } addClass(document.documentElement, "source-sidebar-expanded"); child.innerText = "<"; updateLocalStorage("source-sidebar-show", "true"); } else { + if (window.innerWidth < 701) { + // This is to keep the scroll position on mobile. + document.body.style.position = ""; + document.body.style.top = ""; + // The scroll position is lost when resetting the style, hence why we store it in + // `oldScroll`. + window.scrollTo(0, oldScrollPosition); + } removeClass(document.documentElement, "source-sidebar-expanded"); child.innerText = ">"; updateLocalStorage("source-sidebar-show", "false"); |
