Skip to content

Commit 4006ee5

Browse files
committed
Smooth scrolling.
1 parent db79eb4 commit 4006ee5

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

src/website/editor/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { tokenEditor } from './instances.js';
2+
import { isWideScreen } from '../utils.js';
23
import {
34
algorithmSelect,
45
publicKeyTextArea,
@@ -28,7 +29,7 @@ export function copyTokenLink() {
2829
}
2930

3031
export function fixEditorHeight() {
31-
if(window.matchMedia('(min-width: 768px)').matches) {
32+
if(isWideScreen()) {
3233
editorElement.style.height = `${decodedElement.offsetHeight}px`;
3334
}
3435
}

src/website/index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import {
77
useDefaultToken
88
} from './editor';
99
import { setupJwtCounter } from './counter.js';
10-
import { getParameterByName } from './utils.js';
10+
import { getParameterByName, smoothScrollTo } from './utils.js';
1111
import {
1212
publicKeyTextArea,
1313
codeElements,
14-
debuggerSection
14+
debuggerSection,
15+
menuScrollableLinks
1516
} from './dom-elements.js';
1617

1718
import hljs from 'highlight.js';
@@ -70,8 +71,21 @@ function setupHighlighting() {
7071
}
7172

7273
function setupSmoothScrolling() {
73-
//TODO
74-
console.log('TODO: smooth scrolling');
74+
Array.prototype.forEach.call(menuScrollableLinks, scrollable => {
75+
scrollable.addEventListener('click', event => {
76+
event.preventDefault();
77+
78+
const start = scrollable.href.indexOf('#');
79+
if(start === -1) {
80+
console.error('<a> element with .scrollto set and bad link: ',
81+
scrollable.href);
82+
return;
83+
}
84+
85+
const id = scrollable.href.substr(start + 1);
86+
smoothScrollTo(document.getElementById(id));
87+
});
88+
});
7589
}
7690

7791
// Initialization

src/website/utils.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { navbarElement } from './dom-elements.js';
2+
13
export function safeLocalStorageSetItem(key, value) {
24
try {
35
localStorage.setItem(key, value);
@@ -103,3 +105,27 @@ export function isInViewport(elem) {
103105
bounding.right <= (window.innerWidth || document.documentElement.clientWidth)
104106
);
105107
};
108+
109+
export function isWideScreen() {
110+
return window.matchMedia('(min-width: 768px)').matches;
111+
}
112+
113+
// This is the only function that really requires jQuery, other than some
114+
// of the dependencies. Consider this when adding code that depends on
115+
// jQuery somewhere else.
116+
export function smoothScrollTo(element) {
117+
// TODO: don't use jQuery
118+
119+
const navHeight = $(navbarElement).height();
120+
const targetElement = $(element);
121+
122+
if (isWideScreen()) {
123+
$('html, body').animate({
124+
scrollTop: targetElement.offset().top - navHeight
125+
}, 500);
126+
} else {
127+
$('html, body').animate({
128+
scrollTop: targetElement.offset().top
129+
}, 500);
130+
}
131+
}

stylus/app.styl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ a
5656
&:hover
5757
color: darken(#00b9f1, 50%)
5858

59+
p
60+
margin 30px 0 30px 0
61+
5962
.container
6063
width 100%
6164
max-width 1200px

0 commit comments

Comments
 (0)