Skip to content

Commit

Permalink
Scroll sidebar to middle instead of top. (rust-lang#1161)
Browse files Browse the repository at this point in the history
* Fix: Scroll sidebar to current active section (rust-lang#1067)

* Clean: Some code related to PR rust-lang#1052

* Change `scrollIntoViewIfNeeded` with `scrollIntoView`

* Don't use onload event for sidebar scroll to reduce flickering.

Co-authored-by: 李鸿章 <[email protected]>
  • Loading branch information
ehuss and lihz6 authored Mar 24, 2020
1 parent 7947260 commit 9e77cb6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/theme/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ function playpen_text(playpen) {
(function sidebar() {
var html = document.querySelector("html");
var sidebar = document.getElementById("sidebar");
var sidebarScrollBox = document.querySelector(".sidebar-scrollbox");
var sidebarLinks = document.querySelectorAll('#sidebar a');
var sidebarToggleButton = document.getElementById("sidebar-toggle");
var sidebarResizeHandle = document.getElementById("sidebar-resize-handle");
Expand Down Expand Up @@ -508,7 +507,8 @@ function playpen_text(playpen) {
// Scroll sidebar to current active section
var activeSection = document.getElementById("sidebar").querySelector(".active");
if (activeSection) {
sidebarScrollBox.scrollTop = activeSection.offsetTop;
// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
activeSection.scrollIntoView({ block: 'center' });
}
})();

Expand Down
2 changes: 1 addition & 1 deletion src/theme/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
</script>

<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<div id="sidebar-scrollbox" class="sidebar-scrollbox">
<div class="sidebar-scrollbox">
{{#toc}}{{/toc}}
</div>
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
Expand Down

0 comments on commit 9e77cb6

Please sign in to comment.