Skip to content

Commit

Permalink
Add custom implemention for centered scrolling position
Browse files Browse the repository at this point in the history
`scrollIntoView({behaviour: smooth, block: center})` is only an experimental DOM feature and not supported by many browsers.
  • Loading branch information
straight-shoota committed Dec 22, 2017
1 parent c402827 commit 9b83d32
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/compiler/crystal/tools/doc/html/js/_navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ Navigator = function(sidebar, searchInput, list, leaveSearchScope){
self.moveTimeout = setTimeout(go, 800);*/
}

function scrollCenter(element) {
var rect = element.getBoundingClientRect();
var middle = sidebar.clientHeight / 2;
sidebar.scrollTop += rect.top + rect.height / 2 - middle;
}

var move = this.move = function(upwards){
if(!this.current){
this.highlightFirst();
Expand All @@ -56,7 +62,7 @@ Navigator = function(sidebar, searchInput, list, leaveSearchScope){
var next = upwards ? this.current.previousElementSibling : this.current.nextElementSibling;
if(next && next.classList) {
this.highlight(next);
next.scrollIntoView({behaviour: 'smooth', block: 'center'});
scrollCenter(next);
return true;
}
return false;
Expand Down

0 comments on commit 9b83d32

Please sign in to comment.