dbeck22's picture
Project: Beck-Publishing – Business & Portfolio Website
950bce1 verified
// Global JavaScript for Beck-Publishing
document.addEventListener('DOMContentLoaded', function() {
// Initialize animations
initAnimations();
// Initialize form handling
initForms();
// Initialize scroll effects
initScrollEffects();
// Initialize service worker for PWA (optional)
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js')
.then(() => console.log('Service Worker Registered'))
.catch(err => console.log('Service Worker Registration Failed: ', err));
}
});
function initAnimations() {
// Intersection Observer for scroll animations
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-fade-in-up');
observer.unobserve(entry.target);
}
});
}, observerOptions);
// Observe elements with animation class
document.querySelectorAll('.animate-on-scroll').forEach(el => {
observer.observe(el);
});
}
function initForms() {
// Contact form handling
const contactForm = document.getElementById('contactForm');
if (contactForm) {
contactForm.addEventListener('submit', function(e) {
e.preventDefault();
const submitBtn = this.querySelector('button[type="submit"]');
const originalText = submitBtn.textContent;
// Show loading state
submitBtn.disabled = true;
submitBtn.innerHTML = '<div class="loading-spinner"></div> Processing...';
// Simulate form submission (replace with actual API call)
setTimeout(() => {
alert('Thank you for your message! I\'ll get back to you soon.');
contactForm.reset();
submitBtn.disabled = false;
submitBtn.textContent = originalText;
}, 2000);
});
}
}
function initScrollEffects() {
// Navbar scroll effect
let lastScrollY = window.scrollY;
const navbar = document.querySelector('custom-navbar');
window.addEventListener('scroll', () => {
if (!navbar) return;
const currentScrollY = window.scrollY;
if (currentScrollY > lastScrollY && currentScrollY > 100) {
navbar.style.transform = 'translateY(-100%)';
} else {
navbar.style.transform = 'translateY(0)';
}
lastScrollY = currentScrollY;
// Add shadow when scrolled
if (currentScrollY > 50) {
navbar.style.boxShadow = '0 4px 20px rgba(0, 0, 0, 0.1)';
} else {
navbar.style.boxShadow = 'none';
}
});
}
// Utility function for smooth scrolling
function smoothScrollTo(target) {
const element = document.querySelector(target);
if (element) {
element.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
}
// Debounce function for performance
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Format phone number
function formatPhoneNumber(phone) {
const cleaned = ('' + phone).replace(/\D/g, '');
const match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/);
if (match) {
return '(' + match[1] + ') ' + match[2] + '-' + match[3];
}
return phone;
}
// Email validation
function isValidEmail(email) {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}