-
Notifications
You must be signed in to change notification settings - Fork 248
/
fixed-responsive-nav.js
177 lines (142 loc) · 5.1 KB
/
fixed-responsive-nav.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* =============================================
*
* FIXED RESPONSIVE NAV
*
* (c) 2014 @adtileHQ
* http://www.adtile.me
* http://twitter.com/adtilehq
*
* Free to use under the MIT License.
*
* ============================================= */
(function () {
"use strict";
// Feature test to rule out some older browsers
if ("querySelector" in document && "addEventListener" in window) {
// forEach method, that passes back the stuff we need
var forEach = function (array, callback, scope) {
for (var i = 0; i < array.length; i++) {
callback.call(scope, i, array[i]);
}
};
// Attach FastClick to remove the 300ms tap delay
FastClick.attach(document.body);
// Init smooth scrolling
smoothScroll.init();
// Init Responsive Nav
var navigation = responsiveNav(".nav-collapse", {
// Close the navigation when it's tapped
closeOnNavClick: true
});
// Create a Mask
var mask = document.createElement("div");
mask.className = "mask";
// Append the mask inside <body>
document.body.appendChild(mask);
// Disable mask transitions on Android to boost performance
if (navigator.userAgent.match(/Android/i) !== null) {
document.documentElement.className += " android";
}
// Find navigation links and save a reference to them
var nav = document.querySelector(".nav-collapse ul"),
links = nav.querySelectorAll("[data-scroll]");
// "content" will store all the location cordinates
var content;
// Set up an array of locations which we store in "content"
var setupLocations = function () {
content = [];
forEach(links, function (i, el) {
var href = links[i].getAttribute("href").replace("#", "");
content.push(document.getElementById(href).offsetTop + 200);
});
};
// call locations set up once
setupLocations();
// Re-calculate locations on window resize and orientation change
window.addEventListener("resize", function () {
setupLocations();
}, false);
// Highlight active link on the navigation
var selectActiveMenuItem = function (i) {
forEach(links, function (i, el) {
links[i].parentNode.className = links[i].parentNode.className.replace(/[\s]{0,}active/, "");
});
links[i].parentNode.className += links[i].parentNode.className ? " active" : "active";
};
// Highlight active link when scrolling
var wasNavigationTapped = false;
window.addEventListener("scroll", function () {
// Determine viewport and body size
var top = window.pageYOffset,
body = document.body,
html = document.documentElement,
viewport = window.innerHeight,
bodyheight = Math.max(
body.scrollHeight,
body.offsetHeight,
html.clientHeight,
html.scrollHeight,
html.offsetHeight
);
// For each content link, when it's in viewport, highlight it
if (!wasNavigationTapped) {
forEach(content, function (i, loc) {
if ((loc > top && (loc < top + 300 || (top + viewport) >= bodyheight))) {
selectActiveMenuItem(i);
}
});
}
}, false);
// Close navigation when tapping the mask under it
mask.addEventListener("click", function (e) {
e.preventDefault();
navigation.close();
}, false);
// Clear wasNavigationTapped check after scrolling
var clearTapCheck = function () {
setTimeout(function () {
wasNavigationTapped = false;
}, 500);
};
// Select the right navigation item when tapping the logo
document.querySelector(".logo").addEventListener("click", function (e) {
e.preventDefault();
wasNavigationTapped = true;
// Select first navigation item
selectActiveMenuItem(0);
// Close navigation
navigation.close();
// Remove hash from the URL if pushState is supported
if (history.pushState) {
history.pushState("", document.title, window.location.pathname);
}
// Clear wasNavigationTapped check
clearTapCheck();
}, false);
// When a navigation item is tapped, select it and begin scrolling
forEach(links, function (i, el) {
links[i].addEventListener("click", function (e) {
e.preventDefault();
wasNavigationTapped = true;
// Select right navigation item (we are passing which one to select "i")
selectActiveMenuItem(i);
// Show the URL of the section on the address bar
var thisID = this.getAttribute("href").replace("#", ""),
pane = document.getElementById(thisID);
// If the URL isn't "#home", change it
if (thisID !== "home") {
pane.removeAttribute("id");
location.hash = "#" + thisID;
pane.setAttribute("id", thisID);
// If the URL is "#home", remove hash from the URL
} else {
if (history.pushState) {
history.pushState("", document.title, window.location.pathname);
}
}
// Clear wasNavigationTapped check
clearTapCheck();
}, false);
});
}
})();