flowfix-plumbing / script.js
MendelKats's picture
create a logo on top, and make the website smoother overall
d434620 verified
Raw History Blame Contribute Delete
9.02 kB
// Intersection Observer for Fade-in Animations - Enhanced
const observerOptions = {
root: null,
rootMargin: '50px',
threshold: 0.15
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-fade-in-up');
entry.target.style.opacity = '1';
entry.target.style.transform = 'translateY(0)';
// Add staggered delay based on index if data attribute exists
const delay = entry.target.dataset.delay;
if (delay) {
entry.target.style.animationDelay = `${delay}ms`;
}
observer.unobserve(entry.target);
}
});
}, observerOptions);
// Smooth Scroll Handler with easing
const smoothScroll = (target, duration = 1000) => {
const targetPosition = target.getBoundingClientRect().top + window.pageYOffset - 80;
const startPosition = window.pageYOffset;
const distance = targetPosition - startPosition;
let startTime = null;
const animation = (currentTime) => {
if (startTime === null) startTime = currentTime;
const timeElapsed = currentTime - startTime;
const run = easeInOutCubic(timeElapsed, startPosition, distance, duration);
window.scrollTo(0, run);
if (timeElapsed < duration) requestAnimationFrame(animation);
};
const easeInOutCubic = (t, b, c, d) => {
t /= d/2;
if (t < 1) return c/2*t*t*t + b;
t -= 2;
return c/2*(t*t*t + 2) + b;
};
requestAnimationFrame(animation);
};
// Scroll Progress Bar
const updateScrollProgress = () => {
const scrollProgress = document.querySelector('.scroll-progress');
if (scrollProgress) {
const scrollTop = window.scrollY;
const docHeight = document.documentElement.scrollHeight - window.innerHeight;
const scrollPercent = (scrollTop / docHeight) * 100;
scrollProgress.style.width = scrollPercent + '%';
}
};
// Throttled scroll handler for performance
let ticking = false;
const handleScroll = () => {
if (!ticking) {
window.requestAnimationFrame(() => {
updateScrollProgress();
updateNavbar();
ticking = false;
});
ticking = true;
}
};
// Observe elements
document.addEventListener('DOMContentLoaded', () => {
// Initialize Feather Icons
if (typeof feather !== 'undefined') {
feather.replace();
}
// Create Scroll Progress Bar
const progressBar = document.createElement('div');
progressBar.className = 'scroll-progress';
progressBar.style.width = '0%';
document.body.appendChild(progressBar);
// Animate elements on scroll with stagger
const animateElements = document.querySelectorAll('.service-card, .review-card');
animateElements.forEach((el, index) => {
el.style.opacity = '0';
el.style.transform = 'translateY(30px)';
el.style.transition = `all 0.8s cubic-bezier(0.4, 0, 0.2, 1) ${index * 100}ms`;
el.dataset.delay = index * 100;
observer.observe(el);
});
// Smooth scroll for anchor links with custom easing
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const targetId = this.getAttribute('href');
const target = document.querySelector(targetId);
if (target) {
smoothScroll(target, 1200);
}
});
});
// Listen for scroll events
window.addEventListener('scroll', handleScroll, { passive: true });
// Sticky Navbar Background on Scroll - Smooth transition
const updateNavbar = () => {
const navbar = document.querySelector('site-navbar');
if (navbar) {
const scrollY = window.scrollY;
const shadowRoot = navbar.shadowRoot;
if (shadowRoot) {
const nav = shadowRoot.querySelector('nav');
if (nav) {
if (scrollY > 50) {
nav.style.backgroundColor = 'rgba(255, 255, 255, 0.95)';
nav.style.backdropFilter = 'blur(12px)';
nav.style.boxShadow = '0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06)';
nav.style.transition = 'all 0.4s cubic-bezier(0.4, 0, 0.2, 1)';
} else {
nav.style.backgroundColor = 'transparent';
nav.style.backdropFilter = 'blur(0px)';
nav.style.boxShadow = 'none';
}
}
}
}
};
// Preload images for smoother experience
const preloadImages = () => {
const images = document.querySelectorAll('img');
images.forEach(img => {
if (img.complete) {
img.classList.add('loaded');
} else {
img.addEventListener('load', () => {
img.classList.add('loaded');
});
}
});
};
preloadImages();
// Click to Call Tracking (optional analytics hook)
const phoneLinks = document.querySelectorAll('a[href^="tel:"]');
phoneLinks.forEach(link => {
link.addEventListener('click', () => {
console.log('Phone call initiated');
// Here you would typically send an analytics event
});
});
// Form Validation (if contact forms are added later)
const validateEmail = (email) => {
return email.match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/);
};
// Performance: Lazy load images with fade-in
const lazyImages = document.querySelectorAll('img[loading="lazy"]');
if ('IntersectionObserver' in window) {
const imageObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
const src = img.dataset.src;
if (src) {
img.src = src;
img.addEventListener('load', () => {
img.classList.add('loaded');
img.style.opacity = '1';
});
}
img.classList.remove('lazy');
imageObserver.unobserve(img);
}
});
}, {
rootMargin: '50px 0px',
threshold: 0.01
});
lazyImages.forEach(img => {
img.style.opacity = '0';
imageObserver.observe(img);
});
}
// Mouse parallax effect for hero section
const heroSection = document.querySelector('section:first-of-type');
if (heroSection && !window.matchMedia('(pointer: coarse)').matches) {
heroSection.addEventListener('mousemove', (e) => {
const { clientX, clientY } = e;
const { innerWidth, innerHeight } = window;
const xPos = (clientX / innerWidth - 0.5) * 20;
const yPos = (clientY / innerHeight - 0.5) * 20;
const floatingElements = heroSection.querySelectorAll('.animate-float');
floatingElements.forEach((el, index) => {
const factor = (index + 1) * 0.5;
el.style.transform = `translate(${xPos * factor}px, ${yPos * factor}px)`;
});
});
}
});
// Smooth mobile menu toggle
window.toggleMobileMenu = function() {
const mobileMenu = document.getElementById('mobile-menu');
if (mobileMenu) {
const isHidden = mobileMenu.classList.contains('hidden');
if (isHidden) {
mobileMenu.classList.remove('hidden');
mobileMenu.style.opacity = '0';
mobileMenu.style.transform = 'translateY(-10px)';
requestAnimationFrame(() => {
mobileMenu.style.transition = 'all 0.3s cubic-bezier(0.4, 0, 0.2, 1)';
mobileMenu.style.opacity = '1';
mobileMenu.style.transform = 'translateY(0)';
});
} else {
mobileMenu.style.opacity = '0';
mobileMenu.style.transform = 'translateY(-10px)';
setTimeout(() => {
mobileMenu.classList.add('hidden');
}, 300);
}
}
};
// Close mobile menu when clicking on a link with animation
document.addEventListener('click', (e) => {
if (e.target.closest('#mobile-menu a')) {
const mobileMenu = document.getElementById('mobile-menu');
if (mobileMenu) {
mobileMenu.style.opacity = '0';
mobileMenu.style.transform = 'translateY(-10px)';
setTimeout(() => {
mobileMenu.classList.add('hidden');
mobileMenu.classList.remove('flex');
}, 300);
}
}
});