-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
85 lines (68 loc) · 2.74 KB
/
script.js
File metadata and controls
85 lines (68 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
const menuIcon = document.querySelector ('#menu-icon');
const navbar = document.querySelector('.navbar');
menuIcon.onclick = () => {
menuIcon.classList.toggle('bx-x');
navbar.classList.toggle('active');
};
// scroll section
const sections = document.querySelectorAll('section');
const navLinks = document.querySelectorAll('header nav a');
const header = document.querySelector('header');
window.onscroll = () => {
const top = window.scrollY;
sections.forEach(sec => {
const offset = sec.offsetTop - 100;
const height = sec.offsetHeight;
const id = sec.getAttribute('id');
if (top >= offset && top < offset + height) {
// Active navbar links
navLinks.forEach(link => {
link.classList.remove('active');
});
// document.querySelector('header nav a[href*=' + id + ']').classList.add('active');
document.querySelector('header nav a[href*="' + id + '"]').classList.add('active');
// Active section for animation on scroll
sec.classList.add('show-animate');
} else {
sec.classList.remove('show-animate');
}
});
// Sticky header
header.classList.toggle('sticky', top > 100);
// Remove toggle icon and navbar when clicking navbar links (scroll)
menuIcon.classList.remove('bx-x');
navbar.classList.remove('active');
// Animation for footer on scroll
const footer = document.querySelector('footer');
footer.classList.toggle('show-animate', window.innerHeight + top >= document.scrollingElement.scrollHeight);
};
function submitForm() {
// Get form data
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
const message = document.getElementById('message').value;
// Prepare data for sending
const formData = {
name: name,
email: email,
message: message
};
// Send data to the server using XMLHttpRequest
const xhr = new XMLHttpRequest();
xhr.open('POST', '/api/submit-form', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
// Handle successful response from the server
const response = JSON.parse(xhr.responseText);
console.log('Success:', response);
// You can update the UI or show a success message here
} else if (xhr.readyState === 4) {
// Handle errors during the request
console.error('Error:', xhr.status, xhr.statusText);
// You can update the UI or show an error message here
}
};
// Convert formData to JSON and send the request
xhr.send(JSON.stringify(formData));
}