-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
138 lines (112 loc) · 4.11 KB
/
script.js
File metadata and controls
138 lines (112 loc) · 4.11 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
129
130
131
132
133
134
135
136
137
138
// Mobile Navigation
const hamburger = document.querySelector('.hamburger');
const navLinks = document.querySelector('.nav-links');
hamburger.addEventListener('click', () => {
navLinks.style.display = navLinks.style.display === 'flex' ? 'none' : 'flex';
hamburger.classList.toggle('active');
});
// Enhanced smooth scrolling function
function smoothScrollTo(target, duration = 800) {
const targetElement = document.querySelector(target);
if (!targetElement) return;
const targetPosition = targetElement.offsetTop;
const startPosition = window.pageYOffset;
const distance = targetPosition - startPosition;
let startTime = null;
function animation(currentTime) {
if (startTime === null) startTime = currentTime;
const timeElapsed = currentTime - startTime;
const progress = Math.min(timeElapsed / duration, 1);
// Easing function for smoother animation
const ease = t => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
window.scrollTo(0, startPosition + distance * ease(progress));
if (timeElapsed < duration) {
requestAnimationFrame(animation);
}
}
requestAnimationFrame(animation);
}
// Apply smooth scrolling to all navigation links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = this.getAttribute('href');
// Close mobile menu if open
if (window.innerWidth <= 768) {
navLinks.style.display = 'none';
hamburger.classList.remove('active');
}
smoothScrollTo(target);
});
});
// Form submission handling
const contactForm = document.getElementById('contact-form');
if (contactForm) {
contactForm.addEventListener('submit', function(e) {
e.preventDefault();
// Get form data
const formData = {
name: document.getElementById('name').value,
email: document.getElementById('email').value,
message: document.getElementById('message').value
};
// Here you would typically send the form data to a server
// For now, we'll just log it and show a success message
console.log('Form submitted:', formData);
// Show success message
alert('Thank you for your message! I will get back to you soon.');
// Clear form
contactForm.reset();
});
}
// Scroll animations
const observerOptions = {
root: null,
rootMargin: '0px',
threshold: 0.1
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate');
observer.unobserve(entry.target);
}
});
}, observerOptions);
// Observe all sections for scroll animations
document.querySelectorAll('section').forEach(section => {
observer.observe(section);
});
// Back to Top Button with smooth scroll
const backToTopButton = document.getElementById('back-to-top');
// Show button when user scrolls down 200px
window.addEventListener('scroll', () => {
if (window.scrollY > 200) {
backToTopButton.classList.add('visible');
} else {
backToTopButton.classList.remove('visible');
}
});
// Smooth scroll to top when button is clicked
backToTopButton.addEventListener('click', () => {
smoothScrollTo('#home');
});
// Add active state to navigation links based on scroll position
window.addEventListener('scroll', () => {
const sections = document.querySelectorAll('section');
const navLinks = document.querySelectorAll('.nav-links a');
let current = '';
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (scrollY >= sectionTop - 60) {
current = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href').slice(1) === current) {
link.classList.add('active');
}
});
});