This repository has been archived by the owner on Jul 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
85 lines (69 loc) · 1.87 KB
/
scripts.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
83
84
85
// Navigation:
$(function() {
// Highlight active tab:
$('.nav-menu').on('click', '.nav-item', function(event) {
$('.nav-item').removeClass('active');
$(this).addClass('active');
var linked = $(this.hash);
if (linked.length) {
event.preventDefault();
$('html, body').animate({
scrollTop: linked.offset().top,
duration: 800
});
history.replaceState({}, '', this.hash);
}
});
$('.logo-nav').click(function() {
$('html, body').animate({
scrollTop: 0,
duration: 800
});
})
});
// From https://davidwalsh.name/javascript-debounce-function
function debounce(func, wait) {
var timeout;
return function executedFunction() {
var context = this;
var args = arguments;
var later = function() {
timeout = null;
func.apply(context, args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// As you scroll tab highlighting:
$(function() {
var menuItems = $('.nav-menu .nav-item');
// Get sections on the page:
var scrollItems = menuItems.map(function() {
var section = $($(this).attr('href'));
if (section.length) {
return section;
}
});
var topMenuHeight = $('.navbar').outerHeight();
var lastId;
function updateScrolledToTab() {
// Container scroll position:
var fromTop = $(this).scrollTop() + topMenuHeight;
var current = scrollItems.map(function() {
if ($(this).offset().top < fromTop) {
return this;
}
});
var last = current[current.length - 1];
var id = last && last.length ? last[0].id : '';
if (id !== lastId) {
lastId = id;
menuItems.removeClass('active');
menuItems.filter("[href='#" + id + "']").addClass('active');
history.replaceState({}, '', '#' + id);
}
}
updateScrolledToTab();
$(window).scroll(debounce(updateScrolledToTab, 50));
});