-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
209 lines (177 loc) · 6.39 KB
/
main.js
File metadata and controls
209 lines (177 loc) · 6.39 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const initScrollAnimations = () => {
const sections = document.querySelectorAll('.section');
const observerOptions = {
root: null,
rootMargin: '0px',
threshold: 0.1
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
}
});
}, observerOptions);
sections.forEach(section => {
observer.observe(section);
});
};
const initAboutImageAnimation = () => {
const aboutImage = document.querySelector('.about-intro-image img');
if (!aboutImage) return;
let ticking = false;
const updateImagePosition = () => {
const scrolled = window.pageYOffset;
const imageContainer = document.querySelector('.about-intro-image');
if (imageContainer) {
const rect = imageContainer.getBoundingClientRect();
const containerTop = rect.top + scrolled;
const scrollPercent = (scrolled - containerTop + window.innerHeight) / (window.innerHeight + rect.height);
if (scrollPercent >= 0 && scrollPercent <= 1) {
const movement = (scrollPercent - 0.5) * 40;
aboutImage.style.transform = `translateY(${movement}px)`;
}
}
ticking = false;
};
const requestTick = () => {
if (!ticking) {
window.requestAnimationFrame(updateImagePosition);
ticking = true;
}
};
window.addEventListener('scroll', requestTick, { passive: true });
updateImagePosition();
};
const initSmoothScroll = () => {
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
const href = this.getAttribute('href');
if (href !== '#' && document.querySelector(href)) {
e.preventDefault();
const target = document.querySelector(href);
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
};
const initMobileMenu = () => {
const mobileMenuToggle = document.querySelector('.mobile-menu-toggle');
const mobileCloseBtn = document.querySelector('.mobile-close-btn');
const navLinks = document.querySelector('.nav-links');
if (mobileMenuToggle) {
let overlay = document.querySelector('.mobile-overlay');
if (!overlay) {
overlay = document.createElement('div');
overlay.className = 'mobile-overlay';
overlay.setAttribute('aria-hidden', 'true');
document.body.appendChild(overlay);
}
const closeMenu = () => {
navLinks.classList.remove('active');
overlay.classList.remove('active');
document.body.classList.remove('menu-open');
mobileMenuToggle.setAttribute('aria-expanded', 'false');
};
const openMenu = () => {
navLinks.classList.add('active');
overlay.classList.add('active');
document.body.classList.add('menu-open');
mobileMenuToggle.setAttribute('aria-expanded', 'true');
};
mobileMenuToggle.addEventListener('click', () => {
const isOpen = navLinks.classList.contains('active');
if (isOpen) {
closeMenu();
} else {
openMenu();
}
});
if (mobileCloseBtn) {
mobileCloseBtn.addEventListener('click', closeMenu);
}
overlay.addEventListener('click', closeMenu);
navLinks.querySelectorAll('.nav-link').forEach(link => {
link.addEventListener('click', closeMenu);
});
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && navLinks.classList.contains('active')) {
closeMenu();
mobileMenuToggle.focus();
}
});
}
};
const setActiveNavLink = () => {
const currentPage = window.location.pathname;
const navLinks = document.querySelectorAll('.nav-link');
navLinks.forEach(link => {
const linkPath = new URL(link.href).pathname;
if (linkPath === currentPage || (currentPage === '/' && linkPath === '/index.html')) {
link.classList.add('active');
}
});
};
const initFAQ = () => {
const faqQuestions = document.querySelectorAll('.faq-question');
faqQuestions.forEach(question => {
question.addEventListener('click', () => {
const faqItem = question.parentElement;
const isActive = faqItem.classList.contains('active');
document.querySelectorAll('.faq-item').forEach(item => {
item.classList.remove('active');
});
if (!isActive) {
faqItem.classList.add('active');
}
});
});
};
const initRecipeShareButtons = () => {
const recipeActionButtons = document.querySelectorAll('.recipe-action-btn');
recipeActionButtons.forEach(button => {
const buttonText = button.textContent.trim().toLowerCase();
if (buttonText.includes('pin')) {
button.addEventListener('click', () => {
const pageUrl = encodeURIComponent(window.location.href);
const title = encodeURIComponent(document.querySelector('.recipe-title')?.textContent || 'Check out this recipe!');
const image = encodeURIComponent(document.querySelector('.recipe-intro-image img')?.src || '');
const pinterestUrl = `https://pinterest.com/pin/create/button/?url=${pageUrl}&media=${image}&description=${title}`;
window.open(pinterestUrl, '_blank', 'width=750,height=600');
});
}
if (buttonText.includes('share')) {
button.addEventListener('click', () => {
const pageUrl = encodeURIComponent(window.location.href);
const title = encodeURIComponent(document.querySelector('.recipe-title')?.textContent || 'Check out this recipe!');
const facebookUrl = `https://www.facebook.com/sharer/sharer.php?u=${pageUrl}"e=${title}`;
window.open(facebookUrl, '_blank', 'width=600,height=400');
});
}
if (buttonText.includes('send')) {
button.addEventListener('click', () => {
const title = document.querySelector('.recipe-title')?.textContent || 'Check out this recipe!';
const pageUrl = window.location.href;
const mailtoLink = `mailto:?subject=${encodeURIComponent(title)}&body=${encodeURIComponent('I thought you might like this recipe: ' + pageUrl)}`;
window.location.href = mailtoLink;
});
}
if (buttonText.includes('print')) {
button.addEventListener('click', () => {
window.print();
});
}
});
};
document.addEventListener('DOMContentLoaded', () => {
initScrollAnimations();
initSmoothScroll();
initMobileMenu();
setActiveNavLink();
initFAQ();
initAboutImageAnimation();
initRecipeShareButtons();
});