oscarviedma

Funcionalidad JS Invitacion Digital

May 14th, 2026
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.15 KB | None | 0 0
  1. <script>
  2. document.addEventListener('DOMContentLoaded', function () {
  3.  
  4.   // ---- Selectores ----
  5.   const curtain        = document.getElementById('ov-curtain');
  6.   const panelLeft      = document.querySelector('.ov-panel-left');
  7.   const panelRight     = document.querySelector('.ov-panel-right');
  8.   const playBtn        = document.getElementById('ov-play-btn');
  9.   const main           = document.querySelector('.ov-main');
  10.   const audio          = document.getElementById('bg-audio');
  11.   const audioIndicator = document.getElementById('audio-indicator');
  12.   const bars           = document.getElementById('bars');
  13.   const audioLabel     = document.getElementById('audio-label');
  14.   const ctaBtn         = document.querySelector('.ov-cta-btn');
  15.   const waOverlay      = document.getElementById('wa-overlay');
  16.   const waClose        = document.getElementById('wa-close');
  17.   const waName         = document.getElementById('wa-name');
  18.   const waSend         = document.getElementById('wa-send');
  19.   const waError        = document.getElementById('wa-error');
  20.  
  21.   const WA_NUMBER = '529512345678'; // NÚMERO WHATSAPP
  22.  
  23.   // ---- Confetti ----
  24.   const confColors = ['#FF6B9D','#FFD93D','#4ECDC4','#A29BFE','#FF9F43','#55EFC4'];
  25.  
  26.   function createConfetti() {
  27.     const container = document.getElementById('ov-confetti-bg');
  28.     if (!container) return;
  29.     for (let i = 0; i < 45; i++) {
  30.      const el = document.createElement('div');
  31.      el.className = 'ov-conf';
  32.      const size = 6 + Math.random() * 8;
  33.      el.style.cssText = [
  34.        'position:absolute',
  35.        'border-radius:' + (Math.random() > 0.5 ? '50%' : '2px'),
  36.         'width:' + size + 'px',
  37.         'height:' + (size * 0.5) + 'px',
  38.         'background:' + confColors[Math.floor(Math.random() * confColors.length)],
  39.         'left:' + (Math.random() * 100) + '%',
  40.         'top:' + (Math.random() * -20) + '%',
  41.         'opacity:0.6',
  42.         'animation:ov-fall ' + (4 + Math.random() * 5) + 's ' + (Math.random() * 5) + 's linear infinite',
  43.         'transform:rotate(' + (Math.random() * 360) + 'deg)'
  44.       ].join(';');
  45.       container.appendChild(el);
  46.     }
  47.   }
  48.  
  49.   // ---- Botón PLAY ----
  50.   if (playBtn) {
  51.     playBtn.style.cursor = 'pointer';
  52.     playBtn.addEventListener('click', function () {
  53.  
  54.       // Ocultar botón PLAY
  55.       playBtn.style.opacity = '0';
  56.       playBtn.style.transition = 'opacity 0.4s';
  57.       playBtn.style.pointerEvents = 'none';
  58.  
  59.       // Abrir paneles a los 300ms
  60.       setTimeout(function () {
  61.         if (panelLeft)  panelLeft.style.transform  = 'translateX(-100%)';
  62.         if (panelRight) panelRight.style.transform = 'translateX(100%)';
  63.       }, 300);
  64.  
  65.       // Bajar z-index mientras paneles aún se mueven
  66.       setTimeout(function () {
  67.         curtain.style.zIndex = '-1';
  68.         curtain.style.pointerEvents = 'none';
  69.       }, 800);
  70.  
  71.       // Fade in del main encadenado con la cortina
  72.       setTimeout(function () {
  73.         main.style.opacity = '1';
  74.         audioIndicator.classList.add('visible');
  75.         createConfetti();
  76.         tryPlayAudio();
  77.       }, 900);
  78.  
  79.     });
  80.   }
  81.  
  82.   // ---- Audio ----
  83.   function tryPlayAudio() {
  84.     audio.volume = 0;
  85.     audio.play()
  86.       .then(function () { fadeInAudio(); })
  87.       .catch(function () { audioLabel.textContent = 'Activar música'; });
  88.   }
  89.  
  90.   function fadeInAudio() {
  91.     let vol = 0;
  92.     const fade = setInterval(function () {
  93.       vol = Math.min(vol + 0.025, 0.5);
  94.       audio.volume = vol;
  95.       if (vol >= 0.5) clearInterval(fade);
  96.     }, 100);
  97.   }
  98.  
  99.   audioIndicator.addEventListener('click', function () {
  100.     if (audio.paused) {
  101.       audio.play().then(function () {
  102.         bars.classList.remove('paused');
  103.         audioLabel.textContent = 'Música';
  104.         fadeInAudio();
  105.       });
  106.     } else {
  107.       audio.pause();
  108.       bars.classList.add('paused');
  109.       audioLabel.textContent = 'Pausado';
  110.     }
  111.   });
  112.  
  113.   // ---- Botón CTA → modal WhatsApp ----
  114.   if (ctaBtn) {
  115.     ctaBtn.addEventListener('click', function (e) {
  116.       e.preventDefault();
  117.       waOverlay.classList.add('show');
  118.       waName.focus();
  119.     });
  120.   }
  121.  
  122.   // ---- Modal WhatsApp ----
  123.   waClose.addEventListener('click', function () {
  124.     waOverlay.classList.remove('show');
  125.     waName.value = '';
  126.     waError.textContent = '';
  127.   });
  128.  
  129.   waOverlay.addEventListener('click', function (e) {
  130.     if (e.target === waOverlay) {
  131.       waOverlay.classList.remove('show');
  132.       waName.value = '';
  133.       waError.textContent = '';
  134.     }
  135.   });
  136.  
  137.   waName.addEventListener('keydown', function (e) {
  138.     if (e.key === 'Enter') waSend.click();
  139.   });
  140.  
  141.   waSend.addEventListener('click', function () {
  142.     const nombre = waName.value.trim();
  143.     if (!nombre) {
  144.       waError.textContent = '¡Escribe tu nombre para continuar!';
  145.       waName.focus();
  146.       return;
  147.     }
  148.     waError.textContent = '';
  149.     const msg = encodeURIComponent(
  150.       '¡Hola! Soy *' + nombre + '* y confirmo mi asistencia a la fiesta de cumpleaños de Mateo. ¡Ahí estaré!'
  151.     );
  152.     window.open('https://wa.me/' + WA_NUMBER + '?text=' + msg, '_blank');
  153.     waOverlay.classList.remove('show');
  154.     waName.value = '';
  155.   });
  156.  
  157. });
  158. </script>
Advertisement
Add Comment
Please, Sign In to add comment