This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
styleguide.js
58 lines (50 loc) · 1.96 KB
/
styleguide.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
document.addEventListener('DOMContentLoaded', function() {
// smooth scroll to anchors
document.querySelectorAll("#main-nav a").forEach( function(a){
var el = document.getElementById(a.hash.replace('#',''));
var elPosition = el.getBoundingClientRect();
a.addEventListener('click', function(evn){
evn.preventDefault();
document.querySelectorAll('#main-nav a').forEach( function(a){ a.classList.remove('nav-active') });
document.querySelector('[href="' + a.hash + '"]').classList.add('nav-active');
window.location.hash = a.hash;
window.scroll({
top: elPosition.y,
left: 100,
behavior: 'smooth'
});
})
});
// use intersection observer to detect section
// detect element on the top portion
var options = {
rootMargin: '-45% 0px -35% 0px'
}
// set current element nav class
function callback(entries){
entries.forEach(function(entry){
if (entry.isIntersecting) {
document.querySelectorAll('#main-nav a').forEach( function(a){ a.classList.remove('nav-active') });
document.querySelector('[href="#' + entry.target.id + '"]').classList.add('nav-active');
}
})
}
// create observer
var observer = new IntersectionObserver(callback, options);
// observe all elements
document.querySelectorAll('#sections > div').forEach(function(section){
observer.observe(section);
})
document.getElementById("nav-button").addEventListener('click', function() {
document.getElementById('main-nav').classList.toggle('open');
document.getElementById('nav-button').classList.toggle('open');
document.body.classList.toggle('modal-active');
});
document.querySelectorAll('#main-nav a').forEach( function(a){
a.addEventListener('click', function() {
document.getElementById('main-nav').classList.remove('open');
document.getElementById('nav-button').classList.remove('open');
document.body.classList.remove('modal-active');
})
})
});