-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
52 lines (44 loc) · 1.4 KB
/
index.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
const form = document.querySelector('.login-form');
const submitBtn = document.querySelector('#submitBtn');
if (form && submitBtn) {
form.addEventListener('submit', (e) => {
e.preventDefault();
if (form.checkValidity()) {
window.location.href = './index.html';
} else {
form.reportValidity();
}
});
}
document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
document.querySelector(this.getAttribute('href')).scrollIntoView({
behavior: 'smooth',
});
});
});
console.log('Test');
const s = document.querySelector('main');
const sections = document.querySelectorAll('section');
const navLinks = document.querySelectorAll('nav>ul>li>a');
navLinks[0].classList.add('active');
s.addEventListener('scroll', () => {
let current = '';
sections.forEach((section) => {
const sectionTop = section.offsetTop - s.offsetTop;
const sectionHeight = section.clientHeight;
console.log(sectionTop, sectionHeight);
if (s.scrollTop >= sectionTop - sectionHeight / 3) {
current = section.getAttribute('id');
}
});
console.log('Current section:', current);
navLinks.forEach((link) => {
link.classList.remove('active');
console.log(link.getAttribute('href'), current);
if (link.getAttribute('href').includes(current)) {
link.classList.add('active');
}
});
});