|
31 | 31 | </body>
|
32 | 32 |
|
33 | 33 | <script>
|
34 |
| - var doc_body = document.getElementById("rustdoc_body_wrapper"); |
| 34 | + // Reset the scroll offset on browsers that don't support |
| 35 | + // scroll-padding-top (Desktop & Mobile Safari): |
| 36 | + const maybeFixupViewPortPosition = function() { |
35 | 37 | if (window.location.hash) {
|
36 |
| - var notFirefox = typeof InstallTrigger === 'undefined'; |
37 |
| - if (notFirefox) { |
38 |
| - var hash = window.location.hash; |
39 |
| - window.location.hash = ""; |
40 |
| - setTimeout(function () { |
41 |
| - window.location.hash = hash; |
42 |
| - doc_body.focus(); |
43 |
| - }, 1); |
44 |
| - } |
45 |
| - } else { |
46 |
| - doc_body.focus(); |
| 38 | + const anchorElement = document.getElementById(window.location.hash.substr(1)); |
| 39 | + const navBarHeight = document.getElementsByClassName("nav-container-rustdoc")[0].offsetHeight; |
| 40 | + if (anchorElement && |
| 41 | + anchorElement.getBoundingClientRect().top <= navBarHeight && |
| 42 | + Math.abs(anchorElement.getBoundingClientRect().top) >= 0) { |
| 43 | + // It's just overlapped by the nav bar. This can't be a coincidence, scroll it into view: |
| 44 | + requestAnimationFrame(function() { |
| 45 | + scrollBy(0, -navBarHeight); |
| 46 | + }); |
| 47 | + } |
47 | 48 | }
|
| 49 | + }; |
| 50 | + window.addEventListener("hashchange", maybeFixupViewPortPosition, false); |
| 51 | + // Fix up the scroll position if this was a direct visit to the page |
| 52 | + // (not back/forward navigation): |
| 53 | + if (window.performance) { |
| 54 | + const navEntries = window.performance.getEntriesByType('navigation'); |
| 55 | + const usedBack = navEntries.length > 0 && navEntries[0].type === 'back_forward' || |
| 56 | + (window.performance.navigation && |
| 57 | + window.performance.navigation.type == window.performance.navigation.TYPE_BACK_FORWARD); |
| 58 | + if (!usedBack && window.location.hash) { |
| 59 | + window.addEventListener("scroll", maybeFixupViewPortPosition, {"once": true}); |
| 60 | + } |
| 61 | + } |
48 | 62 | </script>
|
49 | 63 |
|
50 | 64 | </html>
|
0 commit comments