| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- // CONFIGURATION NAVBAR
- const header = document.querySelector(".main-header");
- const topTop = document.querySelector(".to_top");
- window.addEventListener("scroll", () => {
- const scrollPos = window.scrollY;
- if (scrollPos > 10) {
- header.classList.add("scrolled");
- topTop.classList.add("active");
- } else {
- header.classList.remove("scrolled");
- topTop.classList.remove("active");
- }
- })
- const navItems = document.querySelectorAll(".main-header > .nav-links > .nav-link");
- const menuBtn = document.getElementById("menu-btn");
- navItems.forEach(item => {
- item.addEventListener('click', function() {
- menuBtn.checked = false
- });
- });
- // CONFIGURATION FORM
- const form = document.getElementById("contactForm");
- const fields = {
- nombre: false,
- email: false,
- mensaje: false
- }
- const expresiones = {
- nombre: /^.{4,30}$/,
- email: /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/
- }
- const loader = document.querySelector(".contact-form-loader");
- const inputs = document.querySelectorAll("input:not([type='submit']), textarea");
- const inputSubmit = document.querySelector("input[type='submit']")
- const validateForm = (e) => {
- switch (e.target.name) {
- case "nombre":
- validateField(expresiones.nombre, e.target, 'nombre');
- break;
- case "email":
- validateField(expresiones.email, e.target, 'email');
- break;
- case "mensaje":
- if (e.target.value.length === 0) {
- document.querySelector("textarea[name='mensaje'] + .formulario__input-error").classList.remove('none')
- fields.mensaje = false;
- } else {
- document.querySelector("textarea[name='mensaje'] + .formulario__input-error").classList.add('none')
- fields.mensaje = true;
- }
- break;
- }
- }
- const validateField = (expresion, input, field) => {
- if (expresion.test(input.value)) {
- document.querySelector(`input[name='${field}']`).classList.remove('input__border-error')
- document.querySelector(`input[name='${field}'] + .formulario__input-error`).classList.add('none')
- fields[field] = true;
- } else {
- document.querySelector(`input[name='${field}']`).classList.add('input__border-error')
- document.querySelector(`input[name='${field}'] + .formulario__input-error`).classList.remove('none')
- fields[field] = false;
- }
- }
- inputs.forEach((input) => {
- input.addEventListener('keyup', validateForm);
- input.addEventListener('blur', validateForm);
- });
- form.addEventListener('submit', (e) => {
- e.preventDefault();
- if (fields.nombre && fields.email && fields.mensaje) {
- inputSubmit.disabled = true;
- inputSubmit.value = "Enviando...";
- const serviceID = 'default_service';
- const templateID = 'template_ab3seev';
- emailjs.sendForm(serviceID, templateID, document.getElementById("contactForm"))
- .then(() => {
- inputSubmit.value = 'Enviar';
- document.querySelector(".contact-form-response").classList.remove('none');
- form.reset();
- }, (err) => {
- inputSubmit.value = 'Enviar';
- // console.log(JSON.stringify(err));
- });
- }
- });
|