-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
155 lines (135 loc) · 4.78 KB
/
app.js
File metadata and controls
155 lines (135 loc) · 4.78 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
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
const sections = document.querySelectorAll('.section'); //select the section
const sectBtns = document.querySelectorAll('.controls'); // selects the buttons
const sectBtn = document.querySelectorAll('.control'); // selects a button
const fadeInUp = document.querySelectorAll('.fade-in-up');
const fadeIn = document.querySelectorAll('.fade-in');
const allSections = document.querySelector(".main-content");
function PageTransition() {
// Button click active class
for (let i = 0; i < sectBtn.length; i++) {
sectBtn[i].addEventListener('click', (event) => {
let currentBtn = document.querySelector(".active-btn");
currentBtn.classList.remove('active-btn');
event.target.classList.add('active-btn');
})
}
// activate sections
allSections.addEventListener('click', (event) => {
const id = event.target.dataset.id;
// remove selected from the other buttons
if (id) {
// finds the container that contains the id, in this case the section
const element = document.getElementById(id);
element.classList.add('active');
}
})
}
function updateActiveButton(sectionId) {
// Iterate through buttons and remove all the class token active-btn, then add the active section if it matches the section id
sectBtn.forEach((button) => {
button.classList.remove('active-btn'); // removes the highlight on all other buttons
if (button.dataset.id === sectionId) {
button.classList.add('active-btn'); // adds the highlight on currently viewing section
}
});
}
function addActiveClass(section) {
const fadeInUp = section.querySelectorAll('.fade-in-up');
const fadeIn = section.querySelectorAll('.fade-in');
fadeInUp.forEach((item) => {
item.classList.add('active');
});
fadeIn.forEach((item) => {
item.classList.add('active');
});
}
function sectionObserver() {
// IntersectionObserver API to track the current visible section in the webpage and do something with it
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const sectionId = entry.target.id; // Get the ID of the visible section
const section = entry.target;
updateActiveButton(sectionId); // Update the active button
addActiveClass(section); // Add active class on the visible section to use the animation
}
});
}, {threshold: 0.10}); // Trigger when 20% of the section is visible
// Start observing all sections
sections.forEach((section) => {
observer.observe(section);
});
}
function darkenSections(sectionId) {
// Get the section element by ID
sections.forEach((section) => {
sectionId = section.id;
if (sectionId == 'home' || sectionId == 'contact') {
section.style.backgroundColor = ''
}
else {
// Add background color to the visible section
section.style.backgroundColor = 'rgb(196, 195, 180)';
}
});
}
function lightenSections() {
// Get the section element by ID
sections.forEach((section) => {
// Resets the backgroundColor to its default
section.style.backgroundColor = '';
}
);
}
function sectionObserverDarken() {
// Separate observer for darkenSections, higher threshold to trigger the function when the section is almost visible
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const sectionId = entry.target.id; // Get the ID of the visible section
if (sectionId == 'portfolio') {
darkenSections(sectionId); // Darken the navigation bar after scrolling to portfolio section
}
}
});
}, {threshold: 0.14}); // ADJUST THIS VALUE TO LOWER WHEN ADDING MORE PORTFOLIO
// Start observing all sections
sections.forEach((section) => {
observer.observe(section);
});
}
function sectionObserverLighten() {
// Separate observer for lightenSections, lower threshold to trigger the function as soon as the section is visible
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const sectionId = entry.target.id; // Get the ID of the visible section
if (sectionId == 'home' || sectionId == 'contact') {
lightenSections(); // Lighten the navigation bar scrolling on the home and footnote sections NEED FIX FOR FOOTNOTE (NONE YET)
}
}
});
}, {threshold: 0.08}); // ADJUST THIS VALUE TO LOWER WHEN ADDING MORE PORTFOLIO
// Start observing all sections
sections.forEach((section) => {
observer.observe(section);
});
}
function toggleSidebar() {
const sidebar = document.querySelector('.sidebar');
if (sidebar.classList.contains('visible')) {
sidebar.classList.add('hidden');
sidebar.classList.remove('visible');
}
else {
sidebar.classList.add('visible');
sidebar.classList.remove('hidden');
}
}
function init() {
PageTransition();
sectionObserver();
sectionObserverDarken();
sectionObserverLighten();
}
init();