Spaces:
Running
Running
Download script.js from mfsobrin/consertaprint-website: direct link, hf CLI and curl.
- Browser
- Download file 11.1 kB
-
https://huggingface.co/spaces/mfsobrin/consertaprint-website/resolve/main/script.js
- Command line
-
hf download hf://spaces/mfsobrin/consertaprint-website/script.js
-
curl -L -o script.js https://huggingface.co/spaces/mfsobrin/consertaprint-website/resolve/main/script.js
11.1 kB
| // Consertaprint Website JavaScript | |
| document.addEventListener('DOMContentLoaded', function() { | |
| // Initialize all functionality | |
| initMobileMenu(); | |
| initSmoothScrolling(); | |
| initContactForm(); | |
| initAnimations(); | |
| initScrollEffects(); | |
| console.log('Consertaprint website initialized successfully!'); | |
| }); | |
| // Mobile Menu Functionality | |
| function initMobileMenu() { | |
| const mobileMenuButton = document.getElementById('mobile-menu-button'); | |
| const mobileMenu = document.getElementById('mobile-menu'); | |
| if (mobileMenuButton && mobileMenu) { | |
| mobileMenuButton.addEventListener('click', function() { | |
| mobileMenu.classList.toggle('hidden'); | |
| // Animate hamburger icon | |
| const icon = mobileMenuButton.querySelector('i'); | |
| if (mobileMenu.classList.contains('hidden')) { | |
| icon.setAttribute('data-feather', 'menu'); | |
| } else { | |
| icon.setAttribute('data-feather', 'x'); | |
| } | |
| feather.replace(); | |
| }); | |
| // Close mobile menu when clicking on links | |
| const mobileLinks = mobileMenu.querySelectorAll('a'); | |
| mobileLinks.forEach(link => { | |
| link.addEventListener('click', function() { | |
| mobileMenu.classList.add('hidden'); | |
| const icon = mobileMenuButton.querySelector('i'); | |
| icon.setAttribute('data-feather', 'menu'); | |
| feather.replace(); | |
| }); | |
| }); | |
| } | |
| } | |
| // Smooth Scrolling for Navigation Links | |
| function initSmoothScrolling() { | |
| const navLinks = document.querySelectorAll('a[href^="#"]'); | |
| navLinks.forEach(link => { | |
| link.addEventListener('click', function(e) { | |
| e.preventDefault(); | |
| const targetId = this.getAttribute('href'); | |
| const targetSection = document.querySelector(targetId); | |
| if (targetSection) { | |
| const navHeight = document.querySelector('nav').offsetHeight; | |
| const targetPosition = targetSection.offsetTop - navHeight; | |
| window.scrollTo({ | |
| top: targetPosition, | |
| behavior: 'smooth' | |
| }); | |
| } | |
| }); | |
| }); | |
| } | |
| // Contact Form Handling | |
| function initContactForm() { | |
| const contactForm = document.getElementById('contact-form'); | |
| if (contactForm) { | |
| contactForm.addEventListener('submit', function(e) { | |
| e.preventDefault(); | |
| // Get form data | |
| const formData = new FormData(this); | |
| const name = formData.get('name'); | |
| const email = formData.get('email'); | |
| const phone = formData.get('phone'); | |
| const service = formData.get('service'); | |
| const message = formData.get('message'); | |
| // Basic validation | |
| if (!name || !email) { | |
| showMessage('Por favor, preencha pelo menos nome e email.', 'error'); | |
| return; | |
| } | |
| // Email validation | |
| const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | |
| if (!emailRegex.test(email)) { | |
| showMessage('Por favor, insira um email válido.', 'error'); | |
| return; | |
| } | |
| // Show loading state | |
| const submitButton = this.querySelector('button[type="submit"]'); | |
| const originalText = submitButton.textContent; | |
| submitButton.classList.add('loading'); | |
| submitButton.textContent = 'Enviando...'; | |
| submitButton.disabled = true; | |
| // Simulate form submission (replace with actual form handling) | |
| setTimeout(() => { | |
| // Create WhatsApp message | |
| const whatsappMessage = createWhatsAppMessage(name, email, phone, service, message); | |
| const whatsappUrl = `https://wa.me/5565981020342?text=${encodeURIComponent(whatsappMessage)}`; | |
| // Reset form | |
| this.reset(); | |
| // Reset button | |
| submitButton.classList.remove('loading'); | |
| submitButton.textContent = originalText; | |
| submitButton.disabled = false; | |
| // Open WhatsApp | |
| window.open(whatsappUrl, '_blank'); | |
| showMessage('Redirecionando para o WhatsApp para finalizar o contato...', 'success'); | |
| }, 1500); | |
| }); | |
| } | |
| } | |
| // Create WhatsApp message from form data | |
| function createWhatsAppMessage(name, email, phone, service, message) { | |
| let whatsappMessage = `🎯 *NOVA SOLICITAÇÃO DE ORÇAMENTO*\n\n`; | |
| whatsappMessage += `👤 *Nome:* ${name}\n`; | |
| whatsappMessage += `📧 *Email:* ${email}\n`; | |
| if (phone) { | |
| whatsappMessage += `📱 *Telefone:* ${phone}\n`; | |
| } | |
| if (service) { | |
| whatsappMessage += `🔧 *Serviço:* ${getServiceName(service)}\n`; | |
| } | |
| if (message) { | |
| whatsappMessage += `💬 *Mensagem:* ${message}\n`; | |
| } | |
| whatsappMessage += `\n💼 *Empresa:* Consertaprint\n`; | |
| whatsappMessage += `📅 *Data:* ${new Date().toLocaleDateString('pt-BR')}`; | |
| return whatsappMessage; | |
| } | |
| // Get service name in Portuguese | |
| function getServiceName(service) { | |
| const services = { | |
| 'manutencao': 'Manutenção de Impressora', | |
| 'toners': 'Compra de Toners', | |
| 'locacao': 'Locação de Impressoras', | |
| 'orcamento': 'Orçamento Geral' | |
| }; | |
| return services[service] || service; | |
| } | |
| // Show message to user | |
| function showMessage(text, type) { | |
| // Remove existing messages | |
| const existingMessage = document.querySelector('.form-message'); | |
| if (existingMessage) { | |
| existingMessage.remove(); | |
| } | |
| // Create new message | |
| const messageDiv = document.createElement('div'); | |
| messageDiv.className = `form-message ${type === 'success' ? 'success-message' : 'error-message'}`; | |
| messageDiv.textContent = text; | |
| // Insert after form | |
| const form = document.getElementById('contact-form'); | |
| form.parentNode.insertBefore(messageDiv, form.nextSibling); | |
| // Remove message after 5 seconds | |
| setTimeout(() => { | |
| if (messageDiv.parentNode) { | |
| messageDiv.remove(); | |
| } | |
| }, 5000); | |
| } | |
| // Initialize scroll animations | |
| function initAnimations() { | |
| // Intersection Observer for fade-in animations | |
| const observerOptions = { | |
| threshold: 0.1, | |
| rootMargin: '0px 0px -50px 0px' | |
| }; | |
| const observer = new IntersectionObserver(function(entries) { | |
| entries.forEach(entry => { | |
| if (entry.isIntersecting) { | |
| entry.target.classList.add('animate-fade-in-up'); | |
| observer.unobserve(entry.target); | |
| } | |
| }); | |
| }, observerOptions); | |
| // Observe service cards, brand items, and other elements | |
| const animatedElements = document.querySelectorAll('.bg-gray-700, .bg-gray-800'); | |
| animatedElements.forEach(el => { | |
| observer.observe(el); | |
| }); | |
| } | |
| // Scroll effects | |
| function initScrollEffects() { | |
| let ticking = false; | |
| function updateScrollEffects() { | |
| const scrollY = window.scrollY; | |
| const nav = document.querySelector('nav'); | |
| // Add/remove scrolled class to navigation | |
| if (scrollY > 100) { | |
| nav.classList.add('bg-gray-800/95'); | |
| nav.classList.remove('bg-gray-800/90'); | |
| } else { | |
| nav.classList.add('bg-gray-800/90'); | |
| nav.classList.remove('bg-gray-800/95'); | |
| } | |
| ticking = false; | |
| } | |
| function requestScrollUpdate() { | |
| if (!ticking) { | |
| requestAnimationFrame(updateScrollEffects); | |
| ticking = true; | |
| } | |
| } | |
| window.addEventListener('scroll', requestScrollUpdate); | |
| } | |
| // Phone number formatting | |
| function formatPhoneNumber(input) { | |
| // Remove all non-numeric characters | |
| let value = input.value.replace(/\D/g, ''); | |
| // Apply Brazilian phone formatting | |
| if (value.length <= 10) { | |
| // Landline format: (XX) XXXX-XXXX | |
| value = value.replace(/(\d{2})(\d{4})(\d{4})/, '($1) $2-$3'); | |
| } else { | |
| // Mobile format: (XX) XXXXX-XXXX | |
| value = value.replace(/(\d{2})(\d{5})(\d{4})/, '($1) $2-$3'); | |
| } | |
| input.value = value; | |
| } | |
| // Add phone formatting to phone input | |
| document.addEventListener('DOMContentLoaded', function() { | |
| const phoneInput = document.getElementById('phone'); | |
| if (phoneInput) { | |
| phoneInput.addEventListener('input', function() { | |
| formatPhoneNumber(this); | |
| }); | |
| } | |
| }); | |
| // Utility function to handle external links | |
| function handleExternalLinks() { | |
| const externalLinks = document.querySelectorAll('a[target="_blank"]'); | |
| externalLinks.forEach(link => { | |
| link.addEventListener('click', function() { | |
| // Analytics tracking could go here | |
| console.log('External link clicked:', this.href); | |
| }); | |
| }); | |
| } | |
| // Initialize external links handler | |
| document.addEventListener('DOMContentLoaded', handleExternalLinks); | |
| // Add keyboard navigation support | |
| document.addEventListener('keydown', function(e) { | |
| // ESC key closes mobile menu | |
| if (e.key === 'Escape') { | |
| const mobileMenu = document.getElementById('mobile-menu'); | |
| if (mobileMenu && !mobileMenu.classList.contains('hidden')) { | |
| mobileMenu.classList.add('hidden'); | |
| const icon = document.querySelector('#mobile-menu-button i'); | |
| icon.setAttribute('data-feather', 'menu'); | |
| feather.replace(); | |
| } | |
| } | |
| }); | |
| // Performance optimization: Lazy load images when they come into view | |
| function initLazyLoading() { | |
| const images = document.querySelectorAll('img[data-src]'); | |
| const imageObserver = new IntersectionObserver((entries, observer) => { | |
| entries.forEach(entry => { | |
| if (entry.isIntersecting) { | |
| const img = entry.target; | |
| img.src = img.dataset.src; | |
| img.classList.remove('lazy'); | |
| imageObserver.unobserve(img); | |
| } | |
| }); | |
| }); | |
| images.forEach(img => imageObserver.observe(img)); | |
| } | |
| // Error handling | |
| window.addEventListener('error', function(e) { | |
| console.error('JavaScript error:', e.error); | |
| // In production, you might want to send this to an error tracking service | |
| }); | |
| // Service Worker registration for PWA capabilities (optional) | |
| if ('serviceWorker' in navigator) { | |
| window.addEventListener('load', function() { | |
| // navigator.registerServiceWorker('/sw.js') | |
| // .then(function(registration) { | |
| // console.log('SW registered: ', registration); | |
| // }) | |
| // .catch(function(registrationError) { | |
| // console.log('SW registration failed: ', registrationError); | |
| // }); | |
| }); | |
| } |