gravity-ev-website / script.js
midhunnr's picture
**“Redesign my company website into a world-class digital experience for Gravity.ev, a premium and well-established EV charging-point operator across India.
df1d4df verified
Raw History Blame Contribute Delete
11.1 kB
// Gravity EV Website JavaScript
// Wait for DOM to be fully loaded
document.addEventListener('DOMContentLoaded', function() {
// Initialize all components
initMobileMenu();
initCounterAnimation();
initScrollAnimations();
initNavigation();
initFloatingButton();
initFormHandling();
initParallaxEffects();
});
// Mobile menu functionality
function initMobileMenu() {
const mobileMenuButton = document.getElementById('mobile-menu-button');
const nav = document.querySelector('nav');
if (mobileMenuButton) {
mobileMenuButton.addEventListener('click', function() {
// Create mobile menu if it doesn't exist
let mobileMenu = document.getElementById('mobile-menu');
if (!mobileMenu) {
mobileMenu = document.createElement('div');
mobileMenu.id = 'mobile-menu';
mobileMenu.className = 'md:hidden absolute top-16 left-0 right-0 bg-white shadow-lg border-b border-gray-100 z-40';
mobileMenu.innerHTML = `
<div class="px-4 py-6 space-y-4">
<a href="#home" class="block text-gray-800 hover:text-gravity-blue transition-colors">Home</a>
<a href="#solutions" class="block text-gray-600 hover:text-gravity-blue transition-colors">Solutions</a>
<a href="#network" class="block text-gray-600 hover:text-gravity-blue transition-colors">Network</a>
<a href="#partners" class="block text-gray-600 hover:text-gravity-blue transition-colors">Partners</a>
<a href="#contact" class="block text-gray-600 hover:text-gravity-blue transition-colors">Contact</a>
<button class="w-full bg-gravity-blue text-white px-6 py-3 rounded-full hover:bg-gravity-blue-dark transition-all duration-300 mt-4">
Find Charger
</button>
</div>
`;
nav.appendChild(mobileMenu);
}
// Toggle mobile menu visibility
mobileMenu.classList.toggle('hidden');
});
}
}
// Counter animation
function initCounterAnimation() {
const counters = document.querySelectorAll('.counter');
const observerOptions = {
threshold: 0.5,
rootMargin: '0px 0px -100px 0px'
};
const counterObserver = new IntersectionObserver(function(entries) {
entries.forEach(entry => {
if (entry.isIntersecting) {
animateCounter(entry.target);
counterObserver.unobserve(entry.target);
}
});
}, observerOptions);
counters.forEach(counter => {
counterObserver.observe(counter);
});
}
function animateCounter(element) {
const target = parseInt(element.dataset.target);
const duration = 2000;
const step = target / (duration / 16);
let current = 0;
const timer = setInterval(() => {
current += step;
if (current >= target) {
current = target;
clearInterval(timer);
}
if (target >= 1000) {
element.textContent = Math.floor(current).toLocaleString() + '+';
} else {
element.textContent = Math.floor(current);
}
}, 16);
}
// Scroll animations
function initScrollAnimations() {
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const scrollObserver = new IntersectionObserver(function(entries) {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.style.opacity = '1';
entry.target.style.transform = 'translateY(0)';
}
});
}, observerOptions);
// Observe elements for scroll animations
const animatedElements = document.querySelectorAll('.card-hover, .partner-logo');
animatedElements.forEach(element => {
element.style.opacity = '0';
element.style.transform = 'translateY(30px)';
element.style.transition = 'all 0.6s ease-out';
scrollObserver.observe(element);
});
}
// Navigation scroll effects
function initNavigation() {
const nav = document.querySelector('nav');
let lastScrollTop = 0;
window.addEventListener('scroll', function() {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
// Add scrolled class when not at top
if (scrollTop > 100) {
nav.classList.add('nav-scrolled');
} else {
nav.classList.remove('nav-scrolled');
}
// Hide/show navigation on scroll
if (scrollTop > lastScrollTop && scrollTop > 500) {
nav.style.transform = 'translateY(-100%)';
} else {
nav.style.transform = 'translateY(0)';
}
lastScrollTop = scrollTop;
});
}
// Floating action button
function initFloatingButton() {
const floatingButton = document.querySelector('.fixed.bottom-8.right-8 button');
if (floatingButton) {
floatingButton.addEventListener('click', function() {
// Smooth scroll to top
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
// Hide/show based on scroll position
window.addEventListener('scroll', function() {
if (window.scrollY > 500) {
floatingButton.style.opacity = '1';
floatingButton.style.transform = 'scale(1)';
} else {
floatingButton.style.opacity = '0.7';
floatingButton.style.transform = 'scale(0.9)';
}
});
}
}
// Form handling
function initFormHandling() {
const form = document.querySelector('form');
if (form) {
form.addEventListener('submit', function(e) {
e.preventDefault();
// Get form data
const formData = new FormData(form);
const data = Object.fromEntries(formData);
// Show loading state
const submitButton = form.querySelector('button[type="submit"]');
const originalText = submitButton.textContent;
submitButton.textContent = 'Submitting...';
submitButton.disabled = true;
// Simulate form submission
setTimeout(() => {
submitButton.textContent = 'Submitted Successfully!';
submitButton.classList.add('bg-green-500');
submitButton.classList.remove('bg-gravity-blue');
// Reset form
setTimeout(() => {
form.reset();
submitButton.textContent = originalText;
submitButton.disabled = false;
submitButton.classList.remove('bg-green-500');
submitButton.classList.add('bg-gravity-blue');
}, 3000);
}, 2000);
});
}
}
// Parallax effects
function initParallaxEffects() {
window.addEventListener('scroll', function() {
const scrolled = window.pageYOffset;
const parallaxElements = document.querySelectorAll('.floating');
parallaxElements.forEach(element => {
const speed = 0.5;
const yPos = -(scrolled * speed);
element.style.transform = `translateY(${yPos}px)`;
});
});
}
// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
// Interactive hover effects for cards
document.querySelectorAll('.card-hover').forEach(card => {
card.addEventListener('mouseenter', function() {
this.style.transform = 'translateY(-8px) scale(1.02)';
});
card.addEventListener('mouseleave', function() {
this.style.transform = 'translateY(0) scale(1)';
});
});
// Add pulse animation to CTA buttons
document.querySelectorAll('.bg-gravity-blue').forEach(button => {
button.addEventListener('mouseenter', function() {
this.style.animation = 'pulse-blue 1s infinite';
});
button.addEventListener('mouseleave', function() {
this.style.animation = 'none';
});
});
// Loading screen (optional)
window.addEventListener('load', function() {
document.body.classList.add('loaded');
// Trigger entrance animations
const heroElements = document.querySelectorAll('#home > div > div');
heroElements.forEach((element, index) => {
setTimeout(() => {
element.style.opacity = '1';
element.style.transform = 'translateY(0)';
}, index * 200);
});
});
// Error handling
window.addEventListener('error', function(e) {
console.error('JavaScript error:', e.error);
// Could implement error reporting here
});
// Performance monitoring
if ('performance' in window) {
window.addEventListener('load', function() {
setTimeout(() => {
const perfData = performance.getEntriesByType('navigation')[0];
console.log('Page load time:', perfData.loadEventEnd - perfData.loadEventStart, 'ms');
}, 0);
});
}
// Accessibility improvements
document.addEventListener('keydown', function(e) {
// Close mobile menu on Escape key
if (e.key === 'Escape') {
const mobileMenu = document.getElementById('mobile-menu');
if (mobileMenu && !mobileMenu.classList.contains('hidden')) {
mobileMenu.classList.add('hidden');
}
}
});
// Add focus indicators for keyboard navigation
document.addEventListener('keydown', function(e) {
if (e.key === 'Tab') {
document.body.classList.add('keyboard-navigation');
}
});
document.addEventListener('mousedown', function() {
document.body.classList.remove('keyboard-navigation');
});
// Service Worker registration (for PWA capabilities)
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
// Uncomment below to register service worker
// navigator.serviceWorker.register('/sw.js')
// .then(registration => console.log('SW registered'))
// .catch(error => console.log('SW registration failed'));
});
}
// Theme persistence (for future dark mode implementation)
const theme = localStorage.getItem('theme') || 'light';
document.documentElement.setAttribute('data-theme', theme);
// Add CSS class for keyboard navigation
const style = document.createElement('style');
style.textContent = `
.keyboard-navigation *:focus {
outline: 2px solid #0A4DFF !important;
outline-offset: 2px !important;
}
`;
document.head.appendChild(style);