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

259
Views
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 answers
Answer question

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 Report
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!