-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-browser.js
61 lines (52 loc) · 2.12 KB
/
gatsby-browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Implement Gatsby's Browser APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/browser-apis/
*/
// You can delete this file if you're not using it
require("prismjs/themes/prism-okaidia.css")
require("prismjs/plugins/line-numbers/prism-line-numbers.css")
exports.onRouteUpdate = () => {
const $ = require('jquery');
$(document).ready(function () {
if ($('.sidebar').length) {
/* Anchor Scroll */
$('.sidebar a[href*=\\#]').click(function (e) {
setActive($(this));
var anchor = $(this).attr('href');
if ($(anchor).length > 0) {
$('html, body').animate(
{
scrollTop: $(anchor).offset().top - 140
},
400
);
}
});
/* Set the sidebar item in view as active */
function setActive(elm) {
$('a[href*=\\#]').removeClass('active');
$(elm).addClass('active');
}
/* Detect if element is on the screen */
$.fn.isInViewport = function () {
var elementTop = $(this).offset().top;
var elementBottom = elementTop + $(this).outerHeight();
var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();
return elementBottom > viewportTop && elementTop < viewportBottom;
};
$(window).on('scroll', function () {
/* Auto trigger the sidebar active UX */
$('.docs h1, .docs h2, .docs h3, .docs h4, .docs h5, .docs h6').each(function () {
if ($(this).isInViewport()) {
var id = $(this).attr('id');
// Click functionality already works, so let's reuse it
setActive($('.sidebar a[href*=\\#' + id + ']'));
}
});
/* Auto trigger any navigation in view on page load */
}).scroll();
}
});
}