-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
72 lines (63 loc) · 2.12 KB
/
scripts.js
File metadata and controls
72 lines (63 loc) · 2.12 KB
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
const nav = document.querySelector('#menu');
const topOfNav = nav.offsetTop;
const links = document.querySelectorAll('a[data-number]');
const maxLinks = links.length;
const contents = document.querySelectorAll('.content');
function fixNav(){
if(window.scrollY >= topOfNav) {
document.body.classList.add('fixed-nav');
document.body.style.paddingTop = `${nav.offsetHeight}px`;
} else {
document.body.classList.remove('fixed-nav');
document.body.style.paddingTop = 0;
}
}
function clickLink(e){
e.preventDefault();
contentNumber = parseInt(this.dataset.number);
const activeContent = document.querySelector(`.content-${contentNumber}`);
currentClassList = Array.from(activeContent.classList);
if (!currentClassList.includes('off-left')) return // do nothing if current link is clicked
scrollToTop();
const offLeft = range(maxLinks + 1);
offLeft.forEach(number => sendToLeft(number));
activeContent.classList.remove('off-left');
activeContent.classList.remove('off-right');
links.forEach(link => link.classList.remove('active'));
this.classList.add('active');
}
function sendToLeft(number) {
const inactiveContent = document.querySelector(`.content-${number}`);
inactiveContent.classList.add(`off-left`);
}
function range(start, edge, step) {
// If only one number was passed in make it the edge and 0 the start.
if (arguments.length == 1) {
edge = start;
start = 1;
}
// Validate the edge and step numbers.
edge = edge || 1;
step = step || 1;
// Create the array of numbers, stopping before the edge.
for (var ret = []; (edge - start) * step > 0; start += step) {
ret.push(start);
}
return ret;
}
var timeOut;
function scrollToTop() {
if (document.body.scrollTop!=0 || document.documentElement.scrollTop!=0){
window.scrollBy(0,-100);
timeOut=setTimeout('scrollToTop()',2);
}
else clearTimeout(timeOut);
}
window.addEventListener('scroll', fixNav);
links.forEach(link => link.addEventListener('click', clickLink));
// Menu click should
// 1. Stop default event
// 2. Translate X off left or right
// 3. Set display none
// 4. Set display on
// 5. Translate X back to 0