Repository navigation
Expand file tree
/
Copy pathscroll.js
More file actions
90 lines (75 loc) · 2.74 KB
/
Copy pathscroll.js
File metadata and controls
90 lines (75 loc) · 2.74 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
document.addEventListener('DOMContentLoaded', () => {
// Create scroll indicators
createScrollIndicators();
// Update active section on scroll
const sections = document.querySelectorAll('.section');
const dots = document.querySelectorAll('.scroll-dot');
// Update navigation on scroll
window.addEventListener('scroll', () => {
let current = '';
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (pageYOffset >= (sectionTop - sectionHeight / 3)) {
current = section.getAttribute('id');
}
});
// Update active dot
dots.forEach(dot => {
dot.classList.remove('active');
if (dot.dataset.section === current) {
dot.classList.add('active');
}
});
// Update URL without reload
history.replaceState(null, null, `#${current}`);
});
});
function createScrollIndicators() {
const sections = document.querySelectorAll('.section');
const indicator = document.createElement('div');
indicator.className = 'scroll-indicator';
sections.forEach(section => {
const dot = document.createElement('div');
dot.className = 'scroll-dot';
dot.dataset.section = section.id;
dot.addEventListener('click', () => {
document.getElementById(section.id).scrollIntoView({
behavior: 'smooth'
});
});
indicator.appendChild(dot);
});
document.body.appendChild(indicator);
}
// Detect scroll direction and handle section changes
let lastScroll = 0;
window.addEventListener('scroll', () => {
const currentScroll = window.pageYOffset;
if (currentScroll > lastScroll) {
// Scrolling down
handleScrollDown();
} else {
// Scrolling up
handleScrollUp();
}
lastScroll = currentScroll;
});
function handleScrollDown() {
const sections = document.querySelectorAll('.section');
sections.forEach(section => {
const rect = section.getBoundingClientRect();
if (rect.top >= 0 && rect.top <= window.innerHeight / 2) {
section.scrollIntoView({ behavior: 'smooth' });
}
});
}
function handleScrollUp() {
const sections = document.querySelectorAll('.section');
sections.forEach(section => {
const rect = section.getBoundingClientRect();
if (rect.bottom >= window.innerHeight / 2 && rect.bottom <= window.innerHeight) {
section.scrollIntoView({ behavior: 'smooth' });
}
});
}