-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
185 lines (142 loc) · 5.06 KB
/
main.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
178
179
180
181
182
183
184
185
'use strict'
// Make navbar transparent when it is on the top
const navbar = document.querySelector('#navbar');
const navbarHeight = navbar.getBoundingClientRect().height;
const navbarName = document.querySelector('.navbar__logo__name');
document.addEventListener('scroll', () => {
if (window.scrollY > (navbarHeight - 20)) {
navbar.classList.add('navbar--dark');
navbarMenu.classList.add('navbar--dark');
navbarName.classList.add('navbar--dark');
} else {
navbar.classList.remove('navbar--dark');
navbarMenu.classList.remove('navbar--dark');
navbarName.classList.remove('navbar--dark');
}
navbarMenu.classList.remove('open');
});
//Handle scrolling when tapping on the navbar menu
const navbarMenu = document.querySelector('.navbar__menu');
navbarMenu.addEventListener('click', (e) => {
const target =e.target
const link = e.target.dataset.link;
if (link == null) {
return;
}
navbarMenu.classList.remove('open');
scrollIntoView(link);
selectNavItem(target)
});
//navbar toggle button for small screen
const navbarToggleBtn = document.querySelector('.navbar__toggle-btn');
navbarToggleBtn.addEventListener('click', () => {
navbarMenu.classList.toggle('open');
})
//handle click on "contact me" button on home
const about = document.querySelector("#about");
const aboutHeight = about.getBoundingClientRect().height;
const contactBtn = document.querySelector('.home__contact');
contactBtn.addEventListener('click', () => {
scrollIntoView('#contact');
})
//Make home slowly fade to transparent as the window scrolls down
const home = document.querySelector('.home__container');
const homeHeight = home.getBoundingClientRect().height;
document.addEventListener('scroll', () => {
home.style.opacity = (1 - window.scrollY / homeHeight);
})
//Show "arrow up" button when scrolling down
const arrowUp = document.querySelector('.arrow-up');
document.addEventListener('scroll', () => {
if (window.scrollY > homeHeight / 2) {
arrowUp.classList.add('visible');
} else {
arrowUp.classList.remove('visible');
}
});
arrowUp.addEventListener('click', () => {
scrollIntoView('#home');
});
//Projects
const workBtnContainer = document.querySelector('.work__categories');
const projectContainer = document.querySelector('.work__projects');
const projects = document.querySelectorAll('.project');
workBtnContainer.addEventListener('click', (e) => {
const filter = e.target.dataset.filter || e.target.parentNode.dataset.filter;
if (filter == null) {
return;
}
//Remove selection form the previous item and select the new one
const active = document.querySelector('.category__btn.selected');
active.classList.remove('selected');
const target = e.target.nodeName === 'BUTTON' ? e.target : e.target.parentNode;
// console.log(e.target.nodeName); nodeName은 무엇인지 확인차.!
target.classList.add('selected');
projectContainer.classList.add('anime-out');
setTimeout(() => {
projects.forEach((project) => {
if (filter === '*' || filter === project.dataset.type) {
project.classList.remove('invisble');
} else {
project.classList.add('invisble');
}
});
projectContainer.classList.remove('anime-out');
}, 200);
})
//1. 모든 섹션 요소들을 가지고온다
//2. IntersectionObserver 를 이용해서 모드 섹션들을 관찰하낟
//3. 보여지는 섹션에 해당하는 메뉴 아이탬을 활성화 시킨다.
const sectionIds = [
'#home',
'#about',
'#skill',
'#work',
'#testimonials',
'#contact',
];
const sections = sectionIds.map(id => document.querySelector(id));
const navItems = sectionIds.map(id => document.querySelector(`[data-link="${id}"]`));
let selectedNavIndex;
let selectedNavItem = navItems[0];
function selectNavItem(selected) {
selectedNavItem.classList.remove('active');
selectedNavItem = selected
selectedNavItem.classList.add('active');
}
//scroll function
function scrollIntoView(selector) {
const scrollTo = document.querySelector(selector);
scrollTo.scrollIntoView({ behavior: 'smooth' });
selectNavItem(navItems[sectionIds.indexOf(selector)]);
}
const observerOptions = {
root: null,
rootMargin: '0px',
threshold: 0.3,
}
const callback = (entries, observer) => {
entries.forEach(entry => {
if (!entry.isIntersecting && entry.intersectionRatio > 0) {
const index = sectionIds.indexOf(`#${entry.target.id}`);
if (entry.boundingClientRect.y < 0) {
selectedNavIndex = index + 1;
} else {
selectedNavIndex = index - 1;
}
// selectNavItem(navItems[selectedNavIndex]);
}
});
}
const observer = new IntersectionObserver(callback, observerOptions);
sections.forEach(section => observer.observe(section));
window.addEventListener('wheel', () => {
// console.log(window.scrollY+window.innerHeight);
// console.log(document.body.clientHeight);
if (window.scrollY === 0) {
selectedNavIndex = 0;
} else if ((Math.round(window.scrollY) + window.innerHeight) === document.body.clientHeight) {
selectedNavIndex = navItems.length - 1;
}
selectNavItem(navItems[selectedNavIndex]);
});