-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmooth-scroll.js
More file actions
87 lines (73 loc) · 3.24 KB
/
Copy pathsmooth-scroll.js
File metadata and controls
87 lines (73 loc) · 3.24 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
/**
* Simple smooth scrolling functionality
*/
document.addEventListener('DOMContentLoaded', function() {
// Initialize basic smooth scrolling between sections
initSmoothScrolling();
});
/**
* Basic smooth scrolling with header offset
*/
function initSmoothScrolling() {
// Get all links that should trigger smooth scrolling
const scrollLinks = document.querySelectorAll('a[href^="#"]:not([href="#"])');
// Apply smooth scrolling to each link
scrollLinks.forEach(link => {
link.addEventListener('click', function(e) {
// If the click is inside the iframe, do nothing and let the iframe handle it
if (e.target.closest('#page-preview')) {
return;
}
e.preventDefault();
// Get the target element
const targetId = this.getAttribute('href');
const target = document.querySelector(targetId);
if (target) {
// Close mobile menu if it's open
const mobileMenu = document.querySelector('.nav-links.active');
if (mobileMenu) {
mobileMenu.classList.remove('active');
document.querySelector('.hamburger').classList.remove('active');
}
// Calculate scroll position (with offset for header)
const headerHeight = document.querySelector('header').offsetHeight;
const targetPosition = target.getBoundingClientRect().top + window.pageYOffset;
const offsetPosition = targetPosition - headerHeight - 20; // Extra 20px for padding
// Simple smooth scrolling
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
// Update URL hash
setTimeout(() => {
window.location.hash = targetId;
}, 800);
}
});
});
// Add smooth scroll to navigation buttons
const nextSectionButtons = document.querySelectorAll('.next-section-btn, .scroll-indicator');
nextSectionButtons.forEach(button => {
button.addEventListener('click', function() {
// Find the parent section
const currentSection = this.closest('section');
if (!currentSection) return;
// Find the next section
let nextSection = currentSection.nextElementSibling;
// Skip non-section elements
while (nextSection && !nextSection.matches('section')) {
nextSection = nextSection.nextElementSibling;
}
// Scroll to the next section if it exists
if (nextSection) {
const headerHeight = document.querySelector('header').offsetHeight;
const targetPosition = nextSection.getBoundingClientRect().top + window.pageYOffset;
const offsetPosition = targetPosition - headerHeight;
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
}
});
});
}