-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (49 loc) · 1.67 KB
/
index.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
'use strict';
var $navLinks = {};
var $sections = {};
var sectionIdToNavLink = {};
$(document).ready(function() {
// init modal
$('.modal').modal();
// cache the navigation links
$navLinks = $('.nav-link');
// cache (in reversed order) the sections
$sections = $($('.section-heading').get().reverse());
// map each section id to their corresponding navigation link
sectionIdToNavLink = {};
$sections.each(function() {
var id = $(this).attr('id');
sectionIdToNavLink[id] = $('[data-nav-link="' + id + '"]');
});
$(window).scroll( highlightNavigation );
});
function highlightNavigation() {
// nav is hidden on small screens
if ($(window).width() < 993) {
return;
}
// get the current vertical position of the scroll bar
var scrollPosition = $(window).scrollTop();
// iterate the sections
$sections.each(function() {
var currentSection = this;
// get the position of the section
var sectionTop = currentSection.getBoundingClientRect().top;
// if the user has scrolled over the top of the section
if (scrollPosition >= sectionTop) {
// get the section id
var id = currentSection.id;
// get the corresponding navigation link
var $navLink = sectionIdToNavLink[id];
// if the link is not active
if (!$navLink.hasClass('active')) {
// remove .active class from all the links
$navLinks.removeClass('active');
// add .active class to the current link
$navLink.addClass('active');
}
// we have found our section, so we return false to exit the each loop
return false;
}
});
}