Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

261
Vistas
Javascript Play Video on Mouse Hover (Multiple Videos)

I have the following JavaScript code that work great when I hover over a video. I like how the start and stop is broken into functions and it allows me to customize the hover play exactly how I want. However, this code only works on the first video on the page and I have several videos that are loaded on the page dynamically.

I would like to modify this code so that it will work on all videos on the page. I'm thinking that maybe the functions need to be called inside a loop? I'm not sure as I do not know JavaScript well, so any help on how to modify this code would would be much appreciated!

const video = document.querySelector("video");

function startPreview() {
  video.muted = true;
  video.currentTime = 5;
  video.playbackRate = 2.5;
  video.play();
}

function stopPreview() {
  video.currentTime = 5;
  video.playbackRate = 1;
  video.pause();
}

let previewTimeout = null;


video.addEventListener("mouseenter", () => {
  startPreview();
  previewTimeout = setTimeout(stopPreview, 3000);
});

video.addEventListener("mouseleave", () => {
  clearTimeout(previewTimeout);
  previewTimeout = null;
  stopPreview();
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can do event delegation to handle all videos. So:

document.body.addEventListener('mouseenter', (event) => {
  if (event.target.matches('video') {
    // start playing
  }
});

More on the matches() method on MDN.

You would also modify the start and stop methods to take the video element as a parameter:

function startPreview(video) {
  video.muted = true;
  video.currentTime = 5;
  video.playbackRate = 2.5;
  video.play();
}

And pass them the event.target.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda