-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-browser.js
82 lines (75 loc) · 2.69 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
73
74
75
76
77
78
79
80
81
82
//Used for using jQuery/Scripts
exports.onRouteUpdate = () => {
const $ = require('jquery');
$(document).ready(function () {
// NAV POSITION
var navPos = $('#masthead').position().top;
var lastPos = 0;
$(window).on('scroll', function () {
var pos = $(window).scrollTop();
if (pos >= navPos + $('nav').height() && lastPos < pos) {
$('#masthead').addClass('fixed');
}
if (pos < navPos && lastPos > pos) {
$('#masthead').removeClass('fixed');
}
lastPos = pos;
});
// SCROLL ANIMATIONS
$.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;
};
function onScrollInit() {
$('.waypoint').each(function () {
var elem = $(this),
animationClass = elem.attr('data-animation'),
animationDelay = elem.attr('data-delay');
elem.css({
'-webkit-animation-delay': animationDelay,
'-moz-animation-delay': animationDelay,
'animation-delay': animationDelay
});
if (elem.isInViewport()) {
elem.addClass('animated').addClass(animationClass);
} else {
elem.removeClass('animated').removeClass(animationClass);
}
});
}
$(window).on('scroll', function () {
onScrollInit();
});
setTimeout(function () {
onScrollInit();
}, 300);
/* Anchor Scroll */
$('a[href*=\\#]').click(function (e) {
//e.preventDefault();
var anchor = $(this).attr('href').replace(/[/]/g, '');
if ($(anchor).length > 0) {
$('html, body').animate(
{
scrollTop: $(anchor).offset().top - 60
},
400
);
}
});
/* Scroll to anchor if pages loaded with a hash */
setTimeout(function () {
if (window.location.hash) {
var hash = window.location.hash;
$('html, body').animate(
{
scrollTop: $(hash).offset().top - 60
},
400
);
}
}, 500);
})
}