-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-browser.js
72 lines (63 loc) · 1.87 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
62
63
64
65
66
67
68
69
70
71
72
/**
* HINT: gatsby-browser.js is loaded at the root of your site
*/
import {anchorate} from 'anchorate'
import 'normalize.css';
import './src/layout/app.scss';
export function onRouteUpdate () {
anchorate({
scroller: function (element) {
if (!element) {
return false;
}
element.scrollIntoView({behavior: 'smooth', block: 'start'});
return true;
}
})
};
// cf. https://github.com/cferdinandi/smooth-scroll
export function onClientEntry () {
enableSmoothScolling();
loadDeferedStyles();
};
// cf. https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery
function loadDeferedStyles() {
var loadDeferredStyles = function () {
var addStylesNode = document.getElementById("deferred-styles");
var replacement = document.createElement("div");
replacement.innerHTML = addStylesNode.textContent;
document.body.appendChild(replacement);
addStylesNode.parentElement.removeChild(addStylesNode);
};
var raf = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
if (raf) {
raf(function () {
window.setTimeout(loadDeferredStyles, 0);
});
} else {
window.addEventListener('load', loadDeferredStyles);
}
}
function enableSmoothScolling() {
document.addEventListener("DOMContentLoaded", function (event) {
var hash = window.location.hash;
// do some scrolling if page was opened with hash-link
if (hash) {
window.setTimeout(
function () {
new SmoothScroll().animateScroll(
document.querySelector(hash),
null,
{offset: 50}
)
}, 100);
}
// Find all Links an add smoothscrolling with a little offset
new SmoothScroll('a[href*="#"]', {
offset: function (anchor, toggle) {
return 50;
},
});
});
}