134 lines
4.9 KiB
JavaScript
134 lines
4.9 KiB
JavaScript
document.addEventListener('DOMContentLoaded', function () {
|
|
|
|
// Smooth scrolling for anchor links
|
|
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
|
|
anchor.addEventListener('click', function (e) {
|
|
e.preventDefault();
|
|
const targetEl = document.querySelector(this.getAttribute('href'));
|
|
if (targetEl) {
|
|
targetEl.scrollIntoView({
|
|
behavior: 'smooth'
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
// Activate scrollspy
|
|
const mainNav = document.body;
|
|
if (mainNav) {
|
|
const scrollSpy = new bootstrap.ScrollSpy(document.body, {
|
|
target: '#navbarNav',
|
|
offset: 100,
|
|
});
|
|
}
|
|
|
|
// Close mobile nav on item click
|
|
const navLinks = document.querySelectorAll('.navbar-nav .nav-link');
|
|
const navbarCollapse = document.querySelector('.navbar-collapse');
|
|
navLinks.forEach(link => {
|
|
link.addEventListener('click', () => {
|
|
if (navbarCollapse.classList.contains('show')) {
|
|
const bsCollapse = new bootstrap.Collapse(navbarCollapse, {
|
|
toggle: false
|
|
});
|
|
bsCollapse.hide();
|
|
}
|
|
});
|
|
});
|
|
|
|
|
|
// Contact Form Submission
|
|
const contactForm = document.getElementById('contactForm');
|
|
if (contactForm) {
|
|
contactForm.addEventListener('submit', function (e) {
|
|
e.preventDefault();
|
|
|
|
const form = e.target;
|
|
const formData = new FormData(form);
|
|
const submitButton = form.querySelector('button[type="submit"]');
|
|
const originalButtonText = submitButton.innerHTML;
|
|
|
|
submitButton.disabled = true;
|
|
submitButton.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Sending...';
|
|
|
|
fetch('contact.php', {
|
|
method: 'POST',
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
showToast('Success!', 'Your message has been sent successfully.', 'success');
|
|
form.reset();
|
|
} else {
|
|
showToast('Error!', data.error || 'An unknown error occurred.', 'danger');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
showToast('Error!', 'A network error occurred. Please try again.', 'danger');
|
|
})
|
|
.finally(() => {
|
|
submitButton.disabled = false;
|
|
submitButton.innerHTML = originalButtonText;
|
|
});
|
|
});
|
|
}
|
|
|
|
// Toast function
|
|
function showToast(title, message, type) {
|
|
const toastContainer = document.getElementById('toast-container');
|
|
if (!toastContainer) return;
|
|
|
|
const toastEl = document.createElement('div');
|
|
toastEl.className = `toast align-items-center text-white bg-${type} border-0`;
|
|
toastEl.role = 'alert';
|
|
toastEl.ariaLive = 'assertive';
|
|
toastEl.ariaAtomic = 'true';
|
|
|
|
toastEl.innerHTML = `
|
|
<div class="d-flex">
|
|
<div class="toast-body">
|
|
<strong>${title}</strong> ${message}
|
|
</div>
|
|
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
|
|
</div>
|
|
`;
|
|
|
|
toastContainer.appendChild(toastEl);
|
|
|
|
const toast = new bootstrap.Toast(toastEl, { delay: 5000 });
|
|
toast.show();
|
|
|
|
toastEl.addEventListener('hidden.bs.toast', () => {
|
|
toastEl.remove();
|
|
});
|
|
}
|
|
|
|
// Social Sharing
|
|
const shareButtons = document.querySelector('.share-buttons');
|
|
if (shareButtons) {
|
|
const postUrl = encodeURIComponent(window.location.href);
|
|
const postTitle = encodeURIComponent(document.title);
|
|
|
|
const twitterButton = shareButtons.querySelector('a[title="Share on Twitter"]');
|
|
if (twitterButton) {
|
|
twitterButton.href = `https://twitter.com/intent/tweet?url=${postUrl}&text=${postTitle}`;
|
|
twitterButton.target = '_blank';
|
|
twitterButton.rel = 'noopener noreferrer';
|
|
}
|
|
|
|
const facebookButton = shareButtons.querySelector('a[title="Share on Facebook"]');
|
|
if (facebookButton) {
|
|
facebookButton.href = `https://www.facebook.com/sharer/sharer.php?u=${postUrl}`;
|
|
facebookButton.target = '_blank';
|
|
facebookButton.rel = 'noopener noreferrer';
|
|
}
|
|
|
|
const linkedinButton = shareButtons.querySelector('a[title="Share on LinkedIn"]');
|
|
if (linkedinButton) {
|
|
linkedinButton.href = `https://www.linkedin.com/shareArticle?mini=true&url=${postUrl}&title=${postTitle}`;
|
|
linkedinButton.target = '_blank';
|
|
linkedinButton.rel = 'noopener noreferrer';
|
|
}
|
|
}
|
|
}); |