|
| 1 | +/* Jersey Girl's Public Adjusting LLC — Main Script */ |
| 2 | +(function () { |
| 3 | + 'use strict'; |
| 4 | + |
| 5 | + /* ── Burger / mobile nav ─────────────────────────────────── */ |
| 6 | + var burger = document.querySelector('.burger'); |
| 7 | + var menu = document.querySelector('.menu'); |
| 8 | + var topbar = document.querySelector('.topbar'); |
| 9 | + |
| 10 | + if (burger && menu) { |
| 11 | + burger.addEventListener('click', function () { |
| 12 | + var isOpen = menu.classList.toggle('is-open'); |
| 13 | + burger.setAttribute('aria-expanded', isOpen ? 'true' : 'false'); |
| 14 | + }); |
| 15 | + |
| 16 | + // Close on link click (smooth scroll / navigation) |
| 17 | + menu.querySelectorAll('a').forEach(function (link) { |
| 18 | + link.addEventListener('click', function () { |
| 19 | + menu.classList.remove('is-open'); |
| 20 | + burger.setAttribute('aria-expanded', 'false'); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + // Close on outside click |
| 25 | + document.addEventListener('click', function (e) { |
| 26 | + if (!topbar.contains(e.target)) { |
| 27 | + menu.classList.remove('is-open'); |
| 28 | + burger.setAttribute('aria-expanded', 'false'); |
| 29 | + } |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + /* ── Testimonial slider ──────────────────────────────────── */ |
| 34 | + var slides = document.querySelectorAll('.testimonial-slider .slide'); |
| 35 | + var dots = document.querySelectorAll('.testimonial-slider .dot'); |
| 36 | + var prevBtn = document.querySelector('.testimonial-slider .prev'); |
| 37 | + var nextBtn = document.querySelector('.testimonial-slider .next'); |
| 38 | + |
| 39 | + if (slides.length) { |
| 40 | + var current = 0; |
| 41 | + |
| 42 | + function goTo(idx) { |
| 43 | + slides[current].classList.remove('is-active'); |
| 44 | + if (dots[current]) dots[current].classList.remove('is-active'); |
| 45 | + current = (idx + slides.length) % slides.length; |
| 46 | + slides[current].classList.add('is-active'); |
| 47 | + if (dots[current]) dots[current].classList.add('is-active'); |
| 48 | + } |
| 49 | + |
| 50 | + if (prevBtn) prevBtn.addEventListener('click', function () { goTo(current - 1); }); |
| 51 | + if (nextBtn) nextBtn.addEventListener('click', function () { goTo(current + 1); }); |
| 52 | + |
| 53 | + dots.forEach(function (dot, i) { |
| 54 | + dot.addEventListener('click', function () { goTo(i); }); |
| 55 | + }); |
| 56 | + |
| 57 | + // Auto-advance every 6 s |
| 58 | + var autoPlay = setInterval(function () { goTo(current + 1); }, 6000); |
| 59 | + |
| 60 | + // Pause on hover |
| 61 | + var sliderEl = document.querySelector('.testimonial-slider'); |
| 62 | + if (sliderEl) { |
| 63 | + sliderEl.addEventListener('mouseenter', function () { clearInterval(autoPlay); }); |
| 64 | + sliderEl.addEventListener('mouseleave', function () { |
| 65 | + autoPlay = setInterval(function () { goTo(current + 1); }, 6000); |
| 66 | + }); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /* ── Active nav link ─────────────────────────────────────── */ |
| 71 | + var navLinks = document.querySelectorAll('.menu a'); |
| 72 | + var pageHref = window.location.pathname.split('/').pop() || 'index.html'; |
| 73 | + |
| 74 | + navLinks.forEach(function (link) { |
| 75 | + var href = link.getAttribute('href'); |
| 76 | + if (href === pageHref || (pageHref === '' && href === 'index.html')) { |
| 77 | + link.setAttribute('aria-current', 'page'); |
| 78 | + link.style.color = 'var(--navy)'; |
| 79 | + link.style.fontWeight = '700'; |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + /* ── Smooth anchor scroll ────────────────────────────────── */ |
| 84 | + document.querySelectorAll('a[href^="#"]').forEach(function (anchor) { |
| 85 | + anchor.addEventListener('click', function (e) { |
| 86 | + var target = document.querySelector(this.getAttribute('href')); |
| 87 | + if (target) { |
| 88 | + e.preventDefault(); |
| 89 | + var offset = 80; |
| 90 | + var top = target.getBoundingClientRect().top + window.scrollY - offset; |
| 91 | + window.scrollTo({ top: top, behavior: 'smooth' }); |
| 92 | + } |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + /* ── Contact form (index.html inline form) ───────────────── */ |
| 97 | + var form = document.getElementById('contactForm'); |
| 98 | + var formSuccess = document.getElementById('formSuccess'); |
| 99 | + |
| 100 | + function setFieldError(id, errId, msg) { |
| 101 | + var el = document.getElementById(id); |
| 102 | + var err = document.getElementById(errId); |
| 103 | + if (el) el.classList.toggle('invalid', !!msg); |
| 104 | + if (err) err.textContent = msg || ''; |
| 105 | + } |
| 106 | + |
| 107 | + function validEmail(v) { |
| 108 | + return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v); |
| 109 | + } |
| 110 | + |
| 111 | + if (form) { |
| 112 | + form.addEventListener('submit', function (e) { |
| 113 | + e.preventDefault(); |
| 114 | + var name = (document.getElementById('name') || {}).value || ''; |
| 115 | + var email = (document.getElementById('email') || {}).value || ''; |
| 116 | + var message = (document.getElementById('message') || {}).value || ''; |
| 117 | + name = name.trim(); email = email.trim(); message = message.trim(); |
| 118 | + var ok = true; |
| 119 | + |
| 120 | + if (!name) { |
| 121 | + setFieldError('name', 'nameError', 'Please enter your name.'); |
| 122 | + ok = false; |
| 123 | + } else { setFieldError('name', 'nameError', ''); } |
| 124 | + |
| 125 | + if (!email) { |
| 126 | + setFieldError('email', 'emailError', 'Please enter your email address.'); |
| 127 | + ok = false; |
| 128 | + } else if (!validEmail(email)) { |
| 129 | + setFieldError('email', 'emailError', 'Please enter a valid email address.'); |
| 130 | + ok = false; |
| 131 | + } else { setFieldError('email', 'emailError', ''); } |
| 132 | + |
| 133 | + if (!message) { |
| 134 | + setFieldError('message', 'messageError', 'Please enter a message.'); |
| 135 | + ok = false; |
| 136 | + } else { setFieldError('message', 'messageError', ''); } |
| 137 | + |
| 138 | + if (ok && formSuccess) { |
| 139 | + formSuccess.hidden = false; |
| 140 | + form.reset(); |
| 141 | + setTimeout(function () { formSuccess.hidden = true; }, 7000); |
| 142 | + } |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + /* ── Animated stat counters ──────────────────────────────── */ |
| 147 | + var statNums = document.querySelectorAll('[data-count]'); |
| 148 | + |
| 149 | + function animateCount(el) { |
| 150 | + var target = parseInt(el.getAttribute('data-count'), 10); |
| 151 | + var duration = 1600; |
| 152 | + var start = null; |
| 153 | + function step(ts) { |
| 154 | + if (!start) start = ts; |
| 155 | + var p = Math.min((ts - start) / duration, 1); |
| 156 | + var eased = 1 - Math.pow(1 - p, 3); |
| 157 | + el.textContent = Math.floor(eased * target).toLocaleString(); |
| 158 | + if (p < 1) requestAnimationFrame(step); |
| 159 | + else el.textContent = target.toLocaleString(); |
| 160 | + } |
| 161 | + requestAnimationFrame(step); |
| 162 | + } |
| 163 | + |
| 164 | + if ('IntersectionObserver' in window && statNums.length) { |
| 165 | + var io = new IntersectionObserver(function (entries) { |
| 166 | + entries.forEach(function (en) { |
| 167 | + if (en.isIntersecting) { animateCount(en.target); io.unobserve(en.target); } |
| 168 | + }); |
| 169 | + }, { threshold: 0.5 }); |
| 170 | + statNums.forEach(function (el) { io.observe(el); }); |
| 171 | + } |
| 172 | + |
| 173 | + /* ── Reveal cards on scroll ──────────────────────────────── */ |
| 174 | + var revealItems = document.querySelectorAll('[data-reveal]'); |
| 175 | + |
| 176 | + if ('IntersectionObserver' in window && revealItems.length) { |
| 177 | + var revealIO = new IntersectionObserver(function (entries) { |
| 178 | + entries.forEach(function (en) { |
| 179 | + if (en.isIntersecting) { |
| 180 | + en.target.classList.add('revealed'); |
| 181 | + revealIO.unobserve(en.target); |
| 182 | + } |
| 183 | + }); |
| 184 | + }, { threshold: 0.12 }); |
| 185 | + revealItems.forEach(function (el) { revealIO.observe(el); }); |
| 186 | + } else { |
| 187 | + revealItems.forEach(function (el) { el.classList.add('revealed'); }); |
| 188 | + } |
| 189 | + |
| 190 | +})(); |
0 commit comments