Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Uso del observador de intersección para iniciar y detener la animación de JavaScript

Estoy trabajando en una animación de escritura de JavaScript, sin embargo, no quiero recibir un impacto en el rendimiento mientras la animación no está en la pantalla, por lo tanto, solo quiero que se anime cuando está en la pantalla usando Intersection Observer.

Puedo iniciar correctamente la animación con Intersection Observer, pero tengo problemas para encontrar la forma de pausarla cuando está fuera de la pantalla.

Aquí está mi código:

 let typingobserver = new IntersectionObserver((elements) => { elements.forEach(element => { if (element.isIntersecting) { console.log ("on screen!!"); typequeries('.target'); } else { console.log ("not on screen!!"); } }); }); let typingobserverelements = document.querySelector('.target'); typingobserver.observe(typingobserverelements); async function typequeries(element) { element = document.querySelector(element); queries = JSON.parse(element.getAttribute('data-queries')); let i = 0; while(true) { await type(queries[i], element); await wait(2000); await (element.innerHTML = ""); i++; if(i >= queries.length) {i = 0;} } } async function type(query, element) { const characters = query.split(""); let i = 0; while (i < characters.length) { await wait(Math.floor(Math.random() * 150) + 50); element.append(characters[i]); i++ } return; } function wait(ms) { console.log (ms); return new Promise(resolve => setTimeout(resolve, ms)); }
 <span class="target" data-queries='["typed text", "is fun", "to read"]'></span>

¿Cómo puedo iniciar la animación cuando se observa con Intersection Observer y luego pausarla cuando está fuera de la pantalla?

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!