Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
128 lines (100 loc) · 4.8 KB
/
Copy pathscript.js
File metadata and controls
128 lines (100 loc) · 4.8 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
let scrolledToAbout = false; // Flag to prevent repeated scrolling
let isNavButtonClick = false; // Flag to check if the scroll was triggered by a nav button or menu link click
let isMenuClick = false; // Flag to differentiate menu click from manual scroll
// Scroll event listener for automatic scrolling when reaching 30% of the profile section
window.addEventListener('scroll', function() {
// If the scroll was triggered by a nav button or menu click, don't auto-scroll
if (isNavButtonClick || isMenuClick) return;
const profile = document.getElementById('profile');
const about = document.getElementById('about');
if (!profile || !about) return; // Ensure sections exist
// Get the scroll trigger point (30% of profile's height)
const scrollTrigger = profile.offsetTop + (profile.offsetHeight * 0.3);
// Check if the user scrolled past the trigger point
if (window.scrollY >= scrollTrigger && !scrolledToAbout) {
scrolledToAbout = true; // Prevent repeated execution
about.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
// Reset flag if user scrolls back up
if (window.scrollY < profile.offsetTop) {
scrolledToAbout = false;
}
});
// Event listener for desktop and mobile navigation links
const navLinks = document.querySelectorAll('.nav-links a, .menu-links a');
navLinks.forEach(link => {
link.addEventListener('click', function(e) {
// Prevent default anchor click behavior to ensure smooth scrolling
e.preventDefault();
// Set flag to true when a navigation link is clicked
isNavButtonClick = true;
// Reset scrolledToAbout flag to prevent auto scroll behavior
scrolledToAbout = true;
// Get the target section
const targetId = this.getAttribute('href').substring(1); // Get the target section ID (e.g., 'about')
const targetSection = document.getElementById(targetId);
if (targetSection) {
targetSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
// Reset the isNavButtonClick flag after the scroll completes
setTimeout(() => {
isNavButtonClick = false; // Reset flag after scroll completes
// If the user navigated to the About section, set scrolledToAbout to true
if (targetId === "about") {
scrolledToAbout = true;
}
}, 500); // Timeout duration should match scroll duration
}
});
});
// Mobile-specific functionality for hamburger menu
function toggleMenu() {
const menu = document.querySelector(".menu-links");
const icon = document.querySelector(".hamburger-icon");
menu.classList.toggle("open");
icon.classList.toggle("open");
}
function handleMenuClick(event) {
// Prevent smooth scrolling for menu links by setting the flag
isMenuClick = true;
toggleMenu(); // Close the menu after clicking a link
}
// Scroll event listener for mobile and desktop
window.addEventListener('scroll', function () {
const profile = document.getElementById('profile');
const about = document.getElementById('about');
if (!profile || !about) return; // Ensure sections exist
// Get the scroll trigger point (30% of profile's height)
const scrollTrigger = profile.offsetTop + (profile.offsetHeight * 0.3);
// Check if the user scrolled past the trigger point
if (window.scrollY >= scrollTrigger && !isMenuClick && !scrolledToAbout) {
about.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
// Reset flag if user scrolls manually and goes back up
if (window.scrollY < profile.offsetTop) {
isMenuClick = false;
}
});
//-----------------------------------------------------------------------------------
//down arrow work
const downArrow = document.getElementById('down-arrow');
const aboutSection = document.getElementById('about');
downArrow.addEventListener('click', () => {
aboutSection.scrollIntoView({ behavior: 'smooth' });
});
let scrolledToContact = false; // Flag to prevent repeated scrolling
window.addEventListener('scroll', function() {
const projects = document.getElementById('projects');
const contact = document.getElementById('contact');
if (!projects || !contact) return; // Ensure sections exist
// Get the scroll trigger point (30% of projects's height)
const scrollTrigger = projects.offsetTop + (projects.offsetHeight * 0.3);
// Check if the user scrolled past the trigger point
if (window.scrollY >= scrollTrigger && !scrolledToContact) {
scrolledToContact = true; // Prevent repeated execution
contact.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
// Reset flag if user scrolls back up
if (window.scrollY < projects.offsetTop) {
scrolledToContact = false;
}
});