-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
150 lines (132 loc) · 5.38 KB
/
Copy pathscript.js
File metadata and controls
150 lines (132 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Nav scroll + floating CTA
const nav = document.getElementById('nav');
const floatCta = document.getElementById('floatCta');
const onScroll = () => {
nav.classList.toggle('scrolled', window.scrollY > 40);
floatCta.classList.toggle('visible', window.scrollY > 320);
};
window.addEventListener('scroll', onScroll, { passive: true });
onScroll();
// Mobile menu
const burger = document.getElementById('burger');
const mobileMenu = document.getElementById('mobileMenu');
burger.addEventListener('click', () => mobileMenu.classList.toggle('open'));
mobileMenu.querySelectorAll('.nav__mobile-link').forEach(link => {
link.addEventListener('click', () => mobileMenu.classList.remove('open'));
});
// Scroll reveal
const reveals = document.querySelectorAll('.reveal');
const observer = new IntersectionObserver(
entries => entries.forEach(e => {
if (e.isIntersecting) { e.target.classList.add('visible'); observer.unobserve(e.target); }
}),
{ threshold: 0.1, rootMargin: '0px 0px -32px 0px' }
);
reveals.forEach((el, i) => {
el.style.transitionDelay = `${(i % 4) * 70}ms`;
observer.observe(el);
});
// Smooth scroll — guard against href="#" to prevent page-top jump
document.querySelectorAll('a[href^="#"]').forEach(a => {
a.addEventListener('click', e => {
const href = a.getAttribute('href');
if (!href || href === '#') return;
const target = document.querySelector(href);
if (!target) return;
e.preventDefault();
const navH = document.querySelector('.nav')?.offsetHeight || 72;
window.scrollTo({ top: target.getBoundingClientRect().top + window.scrollY - navH - 8, behavior: 'smooth' });
});
});
// Language switcher
let currentLang = localStorage.getItem('ib-lang') || 'de';
function applyLang(lang) {
currentLang = lang;
localStorage.setItem('ib-lang', lang);
document.documentElement.lang = lang;
const t = translations[lang];
document.querySelectorAll('[data-i18n]').forEach(el => {
const v = t[el.dataset.i18n];
if (v !== undefined) el.textContent = v;
});
document.querySelectorAll('[data-i18n-html]').forEach(el => {
const v = t[el.dataset.i18nHtml];
if (v !== undefined) el.innerHTML = v;
});
document.querySelectorAll('[data-i18n-placeholder]').forEach(el => {
const v = t[el.dataset.i18nPlaceholder];
if (v !== undefined) el.placeholder = v;
});
document.querySelectorAll('[data-i18n-aria]').forEach(el => {
const v = t[el.dataset.i18nAria];
if (v !== undefined) el.setAttribute('aria-label', v);
});
document.querySelectorAll('.lang-btn').forEach(btn => {
btn.classList.toggle('active', btn.dataset.lang === lang);
});
document.title = lang === 'en'
? 'IB Webdesign Switzerland — Professional Websites from CHF 499'
: 'IB Webdesign Schweiz — Professionelle Webseiten ab CHF 499';
const metaDesc = document.querySelector('meta[name="description"]');
if (metaDesc) metaDesc.content = lang === 'en'
? 'Professional websites for freelancers, associations and SMEs in Switzerland. Clear, fast and affordable. Get a free website proposal.'
: 'Professionelle Webseiten für Selbständige, Vereine und KMU in der Schweiz. Klar, schnell und bezahlbar. Kostenlosen Website-Vorschlag erhalten.';
}
document.getElementById('langToggle').querySelectorAll('.lang-btn').forEach(btn => {
btn.addEventListener('click', () => applyLang(btn.dataset.lang));
});
applyLang(currentLang);
// Contact form — Web3Forms AJAX
const form = document.getElementById('contactForm');
const submitBtn = document.getElementById('submitBtn');
const formSuccess = document.getElementById('formSuccess');
const fields = {
name: { el: document.getElementById('name'), validate: v => v.trim().length >= 2 },
email: { el: document.getElementById('email'), validate: v => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v) },
service: { el: document.getElementById('service'), validate: v => v !== '' },
message: { el: document.getElementById('message'), validate: v => v.trim().length >= 10 },
};
function validateField(key) {
const f = fields[key];
const ok = f.validate(f.el.value);
f.el.parentElement.classList.toggle('has-error', !ok);
f.el.classList.toggle('error', !ok);
return ok;
}
Object.keys(fields).forEach(key => {
fields[key].el.addEventListener('blur', () => validateField(key));
fields[key].el.addEventListener('input', () => {
if (fields[key].el.classList.contains('error')) validateField(key);
});
});
form.addEventListener('submit', async e => {
e.preventDefault();
const allValid = Object.keys(fields).map(validateField).every(Boolean);
if (!allValid) return;
const btnText = submitBtn.querySelector('.btn-text');
const btnLoading = submitBtn.querySelector('.btn-loading');
btnText.hidden = true;
btnLoading.hidden = false;
submitBtn.disabled = true;
try {
const res = await fetch('https://api.web3forms.com/submit', {
method: 'POST',
headers: { 'Accept': 'application/json' },
body: new FormData(form),
});
const data = await res.json();
if (data.success) {
form.reset();
submitBtn.hidden = true;
formSuccess.hidden = false;
formSuccess.scrollIntoView({ behavior: 'smooth', block: 'center' });
} else {
throw new Error(data.message);
}
} catch {
btnText.hidden = false;
btnLoading.hidden = true;
submitBtn.disabled = false;
alert('Etwas ist schiefgelaufen. Bitte schreiben Sie direkt an info@ib-webdesign.ch');
}
});