-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
88 lines (74 loc) · 2.81 KB
/
scripts.js
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
// Preloader
document.addEventListener("DOMContentLoaded", function() {
setTimeout(function() {
document.getElementById('preloader').style.display = 'none';
}, 1500); // Adjust timeout as needed
});
// Mobile Menu Toggle
document.getElementById('menu-toggle').addEventListener('click', function() {
const navLinks = document.querySelector('.nav-links');
navLinks.classList.toggle('active');
this.classList.toggle('active');
// ARIA attributes for accessibility
const isActive = navLinks.classList.contains('active');
this.setAttribute('aria-expanded', isActive);
navLinks.setAttribute('aria-hidden', !isActive);
});
// Smooth Scroll for Navigation Links
document.querySelectorAll('.nav-links a').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
document.querySelector(this.getAttribute('href')).scrollIntoView({
behavior: 'smooth',
block: 'start' // Scroll to the top of the section
});
});
});
// Form Validation and Submission
document.getElementById('contact-form').addEventListener('submit', function(e) {
e.preventDefault();
const name = document.getElementById('name').value.trim();
const email = document.getElementById('email').value.trim();
const message = document.getElementById('message').value.trim();
// Basic form validation with regex for email
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (name === "" || email === "" || message === "") {
alert('Please fill in all fields.');
return;
}
if (!emailRegex.test(email)) {
alert('Please enter a valid email address.');
return;
}
// Here you would usually send the form data to a server
// For demo purposes, we'll just log the data
console.log({
name: name,
email: email,
message: message
});
// Form submission success animation
const form = document.getElementById('contact-form');
form.innerHTML = '<p class="success-message">Thank you for your message!</p>';
// Clear form fields after animation
setTimeout(() => {
form.reset();
}, 2000); // Delay to allow the animation to be seen
});
// Smooth Scroll to Top Button (Optional)
const scrollToTopButton = document.getElementById('scroll-to-top');
if (scrollToTopButton) {
scrollToTopButton.addEventListener('click', function() {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
window.addEventListener('scroll', function() {
if (window.scrollY > 300) {
scrollToTopButton.classList.add('visible');
} else {
scrollToTopButton.classList.remove('visible');
}
});
}